Eclipse Scout客户端单元使用ScoutClientTestRunner进行测试

时间:2015-03-09 08:16:09

标签: unit-testing junit client eclipse-scout

我正在尝试使用侦察上下文创建单元测试,但我找不到合适的教程或示例。

当我使用ScoutClientTestRunner创建测试时,出现错误

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'

我尝试像这样设置客户端会话类:

@Before
public void setClassSession() throws Exception {

  ScoutClientTestRunner.setDefaultClientSessionClass(ClientSession.class)         
}

@BeforeClass
public void setClassSession() throws Exception {

  ScoutClientTestRunner.setDefaultClientSessionClass(ClientSession.class);
}

我尝试将@ClientTest添加到类和所有方法中,但我仍然会遇到相同的错误。

如果使用ScoutClientTestRunner

,如何在测试中设置客户端会话

1 个答案:

答案 0 :(得分:1)

ScoutClientTestRunner确保执行JUnit测试,并且可以使用所有Scout Context(OSGi等)。 您对@Before@BeforeClass的尝试为时已晚。您需要在此之前提供Scout Context初始化参数。正如异常消息所述,您有两种可能性:

(1)@ClientTest注释

您可以使用@ClientTest参数使用clientSessionClass注释测试类或方法:

@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.
  }
}

如有必要,您也可以在方法级别执行此操作:

@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)定义TestEnvironment

运行测试(直接或使用maven-tycho)时,将完成对完全限定类org.eclipse.scout.testing.client.runner.CustomClientTestEnvironment的查找。

Run tests from the IDE

CustomClientTestEnvironment课程应实施org.eclipse.scout.testing.client.runner.IClientTestEnvironment

方法setupGlobalEnvironment()被调用一次,可用于定义ScoutClientTestRunner.setDefaultClientSessionClass(..)的默认客户端会话。此方法也可用于注册所需的服务。

这是一个例子:

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() {
  }
}

当然(1)和(2)兼容。第二种机制仅定义默认值,而配置了(1)的ClientSession将覆盖默认值。