Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse Scout » Mock beans in unit tests
Mock beans in unit tests [message #1835829] Mon, 14 December 2020 13:44 Go to next message
Andreas ForsterFriend
Messages: 4
Registered: December 2020
Junior Member
Hi all,

I've started on a scout 10.0 project and am wondering how to mock Beans in a unit test, specifically AccessSupport in the server context.

The following code leads to the exception "org.eclipse.scout.rt.platform.exception.PlatformException: The platform is in an invalid state.":

@RunWith(ServerTestRunner.class)
public class ServiceTest {
  @Before
  public void init() {
    BeanTestingHelper beanTestingHelper = new BeanTestingHelper();
    AccessSupport accessSupport = mock(AccessSupport.class);
    beanTestingHelper.registerBean(new BeanMetaData(AccessSupport.class, accessSupport));


Any ideas?
Re: Mock beans in unit tests [message #1835832 is a reply to message #1835829] Mon, 14 December 2020 14:13 Go to previous messageGo to next message
Patrick Baenziger is currently offline Patrick BaenzigerFriend
Messages: 96
Registered: September 2011
Member
Hello Andreas

First: Welcome!
You already found the BeanTestingHelper, which is great.

The most likely issue is that you're not running with a ServerSession (and possibly, a subject), but your test scenario is requiring one behind the scenes.
Check if you need to add the
@RunWithServerSession(ServerSession.class)
annotation - and replace the value inside the parenthesis with a reference to your own session class.
Depending on your app, you'll need to add a Subject annotation as well.

You can see an example for such a unit test and the annotations in the contacts application.

But, there are multiple things that could be going wrong here which are hard to diagnose - if my suggestion doesn't solve your Issue, please post the complete stacktrace of the error.


A small advice:
You can access the helper either via
BEANS.get(BeanTestingHelper.class)
or via the static get() method. It is best practice to always access beans or helpers like this, so these accesses will properly respect any overriding classes you register, like you do in this test.
Also don't forget to keep the returned reference from the call to registerBean to the registered bean and to de-register it after the test (@After/@AfterClass).
Re: Mock beans in unit tests [message #1835850 is a reply to message #1835832] Tue, 15 December 2020 07:21 Go to previous message
Andreas ForsterFriend
Messages: 4
Registered: December 2020
Junior Member
Thanks for your help.

Looking at the complete callstack I could see that an IPlatformListener failed, which is why the platform was in an invalid state.
Previous Topic:Popup anchor as table cell
Next Topic:How to build (and use) ScoutRT from source?
Goto Forum:
  


Current Time: Sat Nov 02 15:56:34 GMT 2024

Powered by FUDForum. Page generated in 0.03675 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top