Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse Scout » Mocking the SharedVariablesMap of the ClientSession in UnitTests
Mocking the SharedVariablesMap of the ClientSession in UnitTests [message #1694021] Thu, 30 April 2015 07:23 Go to next message
Urs Beeli is currently offline Urs BeeliFriend
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 Go to previous messageGo to next message
Jeremie Bresson is currently offline Jeremie BressonFriend
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.

Re: Mocking the SharedVariablesMap of the ClientSession in UnitTests [message #1694422 is a reply to message #1694292] Tue, 05 May 2015 08:59 Go to previous messageGo to next message
Urs Beeli is currently offline Urs BeeliFriend
Messages: 573
Registered: October 2012
Location: Bern, Switzerland
Senior Member
Thanks Jeremy, I think for simplicity's sake, I'll try your first approach.
Re: Mocking the SharedVariablesMap of the ClientSession in UnitTests [message #1694541 is a reply to message #1694422] Wed, 06 May 2015 07:12 Go to previous messageGo to next message
Urs Beeli is currently offline Urs BeeliFriend
Messages: 573
Registered: October 2012
Location: Bern, Switzerland
Senior Member
I had to change
protected <T> T getSharedContextVariable(String name, Class<T> type) {

to
public <T> T getSharedContextVariable(String name, Class<T> type) {

but it works like a charm. Thanks a lot for this suggestion!
Re: Mocking the SharedVariablesMap of the ClientSession in UnitTests [message #1694592 is a reply to message #1694541] Wed, 06 May 2015 13:47 Go to previous messageGo to next message
Jeremie Bresson is currently offline Jeremie BressonFriend
Messages: 1252
Registered: October 2011
Senior Member
I am not sure to know why you needed to change the visibility of this method in your TestingClientSession...

I assume your productive code never call this method (that is protected in your ClientSession). Or have you changed the visibility also in ClientSession as well?
Re: Mocking the SharedVariablesMap of the ClientSession in UnitTests [message #1695254 is a reply to message #1694592] Wed, 13 May 2015 06:04 Go to previous message
Urs Beeli is currently offline Urs BeeliFriend
Messages: 573
Registered: October 2012
Location: Bern, Switzerland
Senior Member
Jeremie Bresson wrote on Wed, 06 May 2015 15:47
Or have you changed the visibility also in ClientSession as well?


Yes, we did. I hadn't realised it was a change in our session. Sorry for the confusion.
Previous Topic:Invalidate Browser/RAP Session
Next Topic:[Blog Post] Documentation Platform for Eclipse Scout
Goto Forum:
  


Current Time: Mon Apr 15 21:53:50 GMT 2024

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

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

Back to the top