Skip to main content



      Home
Home » Eclipse Projects » Eclipse Scout » Mock beans in unit tests
Mock beans in unit tests [message #1835829] Mon, 14 December 2020 08:44 Go to next message
Eclipse UserFriend
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 09:13 Go to previous messageGo to next message
Eclipse UserFriend
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 02:21 Go to previous message
Eclipse UserFriend
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: Tue Jul 15 16:24:44 EDT 2025

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

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

Back to the top