[neon] Problems with server unit test -> @BeforeClass called too late [message #1755123] |
Tue, 28 February 2017 09:36 |
Urs Beeli Messages: 573 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 #1755242 is a reply to message #1755123] |
Wed, 01 March 2017 14:12 |
Paolo Bazzi Messages: 33 Registered: January 2017 Location: Switzerland |
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
Eclipse Scout Homepage | Documentation | GitHub
|
|
|
Powered by
FUDForum. Page generated in 0.03990 seconds