1

I am trying to create unit test with scout context and I can't find proper tutorial or example for it.

When I create test with ScoutClientTestRunner, I get error

java.lang.Exception: Client session class is not set. Either set the default client session using 'ScoutClientTestRunner.setDefaultClientSessionClass' or annotate your test class and/or method with 'ClientTest'

I try to set client session class like this :

@Before
public void setClassSession() throws Exception {

  ScoutClientTestRunner.setDefaultClientSessionClass(ClientSession.class)         
}

and

@BeforeClass
public void setClassSession() throws Exception {

  ScoutClientTestRunner.setDefaultClientSessionClass(ClientSession.class);
}

I try to add @ClientTest to the class and to all methods but I still get same error.

How to set client session in tests if you use ScoutClientTestRunner ?

Marko Zadravec
  • 8,298
  • 10
  • 55
  • 97
  • I do not think that we have a lot of documentation out there. You can check the [Scout demo application Minifig Creator](http://wiki.eclipse.org/Scout/Demo#Minifig_Application) and the related talk I gave ([slides](https://wiki.eclipse.org/Scout/Overview/Slides)). I will try to find the time to write an exhaustive answer here. – Jmini Mar 09 '15 at 09:12

1 Answers1

1

The ScoutClientTestRunner ensures that the JUnit tests are executed having all the Scout Context (OSGi and so on) available. Your attempts with @Before or @BeforeClass are too late. You need to provide the Scout Context initialization parameters before that. As the exception message says, you have 2 possibilities:

(1) @ClientTest annotation

You can annotate test classes or methods with @ClientTest using the clientSessionClass parameter:

@RunWith(ScoutClientTestRunner.class)
@ClientTest(clientSessionClass = ClientSession.class)
public class DesktopFormTest {

  @Test
  public void test1() throws Exception {
    //Do something requiring a scout context:
    //for example instantiate a DesktopForm.
  }
}

If necessary you can also do it at method level:

@RunWith(ScoutClientTestRunner.class)
public class DesktopFormTest {

  @Test
  @ClientTest(clientSessionClass = Client1Session.class)
  public void test1() throws Exception {
    //client session is an instance of Client1Session.
  }

  @Test
  @ClientTest(clientSessionClass = Client2Session.class)
  public void test2() throws Exception {
    //client session is an instance of Client2Session.
  }
}

(2) Defining a TestEnvironment

When the test is run (directly or using maven-tycho), a lookup for a fully qualified class org.eclipse.scout.testing.client.runner.CustomClientTestEnvironment is done.

Run tests from the IDE

The CustomClientTestEnvironment class should implement org.eclipse.scout.testing.client.runner.IClientTestEnvironment

The method setupGlobalEnvironment() is called once and can be used to define the default client session with ScoutClientTestRunner.setDefaultClientSessionClass(..). This method can also be used to register required services.

Here an example:

package org.eclipse.scout.testing.client.runner; // <= can not be changed.

// add imports

public class CustomClientTestEnvironment implements IClientTestEnvironment {

  @Override
  public void setupGlobalEnvironment() {
    //Set client session:
    ScoutClientTestRunner.setDefaultClientSessionClass(ClientSession.class);
  }

  @Override
  public void setupInstanceEnvironment() {
  }
}

Of course (1) and (2) are compatible. The second mechanism defines only the default and ClientSession configured with (1) will override the default.

Jmini
  • 9,189
  • 2
  • 55
  • 77
  • I have some new question http://stackoverflow.com/questions/28958144/scout-eclipse-scoutservertestrunner-on-client-tests – Marko Zadravec Mar 10 '15 at 07:25
  • I try (2) and I get error : java.lang.reflect.UndeclaredThrowableException at com.sun.proxy.$Proxy10.getAllCodeTypeClasses(Unknown Source) at org.eclipse.scout.rt.client.services.common.code.CodeServiceClientProxy.getAllCodeTypeClasses(CodeServiceClientProxy.java:351) at org.eclipse.scout.rt.client.services.common.code.CodeServiceClientProxy.getAllCodeTypes(CodeServiceClientProxy.java:372) at org.eclipse.scout.rt.shared.services.common.code.CODES.getAllCodeTypes(CODES.java:97) ..... And non of tests are extecuted – Marko Zadravec Mar 10 '15 at 10:09
  • 1
    As I explained it [here](http://stackoverflow.com/a/28982173/91497), you need to define your strategy for the server communication (start it or mock it). I usually register a default `ICodeService` (priority 500) that do nothing in the `setupGlobalEnvironment` with this line: `TestingUtility.registerServices(Activator.getDefault().getBundle(), 500, new TestingCodeService(Collections.> emptyList()));` If I need a specific CodeType for a test, I register a second service with an higher prio (1000) in my test class (and I unregister it after my test). – Jmini Mar 11 '15 at 09:09