Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » scout » [neon] Problems with server unit test -> @BeforeClass called too late
[neon] Problems with server unit test -> @BeforeClass called too late [message #1755123] Tue, 28 February 2017 09:36 Go to next message
Urs Beeli is currently offline Urs BeeliFriend
Messages: 557
Registered: October 2012
Location: Bern, Switzerland
Senior Member
I've been migrating some of my server unit tests. However, I have stumbled across a situation I cannot seem to solve.

My setup is as follows:

In my productive code, I have a MyServerPlatformListener which implements IPlatformListener. In its stateChanged() method, it calls some other classes that I need to mock for my tests.

In my unit test (which is annotated with @RunWith(ServerTestRunner.class) I have both a static setup method annotated with @BeforeClass and a normal setup method annotated with @Before. The static setup method mocks those classes used by the PlatformListener.

However, it seems that the PlatformTestRunner and/or ServerTestRunner are setup in a way which causes the Platform to be started before the @BeforeClass method is called. This means that my mocked bean is not yet available which causes the PlatformListener to throw an exception which then causes the Platform to be set to PlatformInvalid which aborts the test before any of the setup methods are called.

I have tried extending MyServerPlatformListener with a TestMyPLatformListener which has a @Replace annotation, in it I have overwritten the stateChanged method to either keep the original from being called or to setup the mocks before passing the call to the original. However, it seems that this platform listener is not even picked up by the Bean Manager.

So, in effect, I am unable to mock the classes referenced by the PlatformListeners for my tests.

Is this a bug in the test setup nobody has stumbled across yet? Or am I doing something wrong? How can I get around this issue?
Re: [neon] Problems with server unit test -> @BeforeClass called too late [message #1755132 is a reply to message #1755123] Tue, 28 February 2017 10:34 Go to previous messageGo to next message
Urs Beeli is currently offline Urs BeeliFriend
Messages: 557
Registered: October 2012
Location: Bern, Switzerland
Senior Member
The same seems to be true with the ClientTestRunner. It also instantiates the Session passed to @RunWithClientSession() before calling the @BeforeClass method.

At least there it is easy to supply our own session, however, some of our code requires the "real" session object, so providing a fake session that does not extend the real session won't work and the real session needs certain beans that we want to mock first.
Re: [neon] Problems with server unit test -> @BeforeClass called too late [message #1755242 is a reply to message #1755123] Wed, 01 March 2017 14:12 Go to previous message
Paolo Bazzi is currently offline Paolo BazziFriend
Messages: 11
Registered: January 2017
Junior Member
Hi Urs

Quote:
I have tried extending MyServerPlatformListener with a TestMyPLatformListener which has a @Replace annotation, in it I have overwritten the stateChanged method to either keep the original from being called or to setup the mocks before passing the call to the original. However, it seems that this platform listener is not even picked up by the Bean Manager.


Do you have a src\test\resources\META-INF\scout.xml file in your test project containing the following content?

<?xml version="1.0" encoding="UTF-8"?>
<scout>
</scout>


This file acts as marker for the beanmanager to pick up all classes in the project.

In addition you may modify the platform used to execute the test like in the following snippet. Implementing the createBeanManager() you may setup a custom bean manager containing the mocked beans you want (if you don't call the super method, you wont have any default beans)

@RunWithNewPlatform(platform = MockedPlatform.class)
@RunWith(PlatformTestRunner.class)
public class AbstractDataServiceTest {

  public static class MockedPlatform extends DefaultPlatform {
    @Override
    protected BeanManagerImplementor createBeanManager() {
      BeanManagerImplementor beanManager = new BeanManagerImplementor();
      JPAService jpaService = Mockito.mock(JPAService.class);
      beanManager.registerBean(new BeanMetaData(JPAService.class, jpaService));
      return beanManager;
    }
  }


Cheers Paolo
Previous Topic:Container Menu with Children & Single/MultiSelection
Next Topic:[mars] docx4j - how to use
Goto Forum:
  


Current Time: Fri May 26 05:57:58 GMT 2017

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

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