Marko Zadravec
Marko Zadravec

Reputation: 8730

Eclipse Scout client unit tests with ScoutClientTestRunner

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 ?

Upvotes: 1

Views: 184

Answers (1)

Jmini
Jmini

Reputation: 9497

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.

Upvotes: 1

Related Questions