Mocking the SharedVariablesMap of the ClientSession in UnitTests [message #1694021] |
Thu, 30 April 2015 07:23 |
Urs Beeli Messages: 573 Registered: October 2012 Location: Bern, Switzerland |
Senior Member |
|
|
In our production code we are placing some information on the CoreSession's sharedVariableMap:
OurServerSession.get().setSharedContextVariable(DEBIC_ENTITIES, Set.class, debics);
and are then accessing it on the client:
Set<DebitorencodeEntity> debitorencodes = OurClientSession.get().getSharedContextVariable(AngebotSharedVariableNames.DEBIC_ENTITIES, Set.class);
This works well in productive code.
However, we have written a number of JUnit tests for our code. They are annotated with
@RunWith(ScoutClientTestRunner.class)
and we have the "magic" class org.eclipse.scout.testing.client.runner.CustomClientTestEnvironment in which we are setting our ClientSession to be used using
ScoutClientTestRunner.setDefaultClientSessionClass(OurClientSession.class);
Among other things we want to test one of the methods which evaluates a number of criteria, among others the content of the shared variable.
We have been unable to "mock" the content of the shared variables. The ClientSession does not offer the setSharedContextVariable() method and the getSharedVariableMap() method returns a copy of the map, so we can't get that and then put into it during test setup. I've seen the method updateSharedVariableMap() but that is private.
Is there any way to "inject" certain entries into the client session's sharedContextVariableMap for testing?
[Updated on: Thu, 30 April 2015 07:25] Report message to a moderator
|
|
|
Re: Mocking the SharedVariablesMap of the ClientSession in UnitTests [message #1694292 is a reply to message #1694021] |
Mon, 04 May 2015 08:24 |
Jeremie Bresson Messages: 1252 Registered: October 2011 |
Senior Member |
|
|
I guess that you are using SharedVariableMap in your ClientSession like this:
public class ClientSession extends AbstractClientSession {
//...
public String getVariableOne() {
return getSharedContextVariable("VariableOne", String.class);
}
public Long getPersonNr() {
return getSharedContextVariable("personNr", Long.class);
}
//...
}
I think you have 2 solutions:
1/ Use a TestingClientSession
You can create an additional class: TestingClientSession (in the same bundle as CustomClientTestEnvironment).
This class extends ClientSession.
This is how the TestingClientSession could look like:
public class TestingClientSession extends ClientSession {
private Map<String, Object> m_testingVariableMap;
@Override
protected <T> T getSharedContextVariable(String name, Class<T> type) {
if (m_testingVariableMap == null) {
return super.getSharedContextVariable(name, type);
}
Object o = m_testingVariableMap.get(name);
return TypeCastUtility.castValue(o, type);
}
public void setTestingVariableMap(Map<String, Object> map) {
m_testingVariableMap = map;
}
public static TestingClientSession get() {
return ClientJob.getCurrentSession(TestingClientSession.class);
}
}
In your CustomClientTestEnvironment you define this new session as the session used in your application.
In this new class you can override getSharedContextVariable() and hold a second map for your tests.
At the beginning of your test, you can set the values.
TestingClientSession.get().setTestingVariableMap(..)
At the end of your test (maybe the tearDown() function anotated with @After) I recommend you to reset the map value to null.
I didn't test my code, but I think you get the idea. Do not hesitate to continue the discussion if I missed something.
2/ Faking SharedContextChangedNotification
The second option I can imagine is to fake the SharedContextChangedNotification coming normally from the server, when the ServerSession is initialized.
If you try to do so, you will obtain a setup of your tests that is closer to the real application.
I never did it and I have limited knowledge of Scout Notifications. If you are interested in this solution, please continue the discussion. We will try to figure out something.
|
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.02342 seconds