Unit Testing in RAP 3.0

March 4, 2015 | 2 min Read

When you test components of a RAP application, you have to simulate the environment that RAP UI code normally runs in. Thanks to a new API, this becomes easier in RAP 3.0.

In an earlier post, my colleague Johannes explained how to write unit tests for RAP 2.x. This approach required more boilerplate code to set up and tear down the RAP environment in every single test case. To make it worse, this setup code had to use internals. The class Fixture had been created to test RAP itself and thus had a lot of methods that are not relevant for application development. Hence, it was explicitly excluded from the public API.

In RAP 3.0, we decided to clean up and provide real API. We moved all internal classes out of the public package and replaced it with a simple JUnit Rule. Instead of calling Fixture.setUp() and Fixture.tearDown(), you only need to include the following line in your JUnit test cases:

@Rule
public TestContext context = new TestContext();

This will simulate a new test context (including UI thread, UISession, ApplicationContext etc.) for every test method and also enable event propagation (there’s no need for calling Fixture.fakePhase( PROCESS_ACTION ) anymore). TestContext is contained in the bundle org.eclipse.rap.rwt.testfixture.

When you write tests for custom widgets, you may need to inject mocked RemoteObjects. To do so, you can replace the Connection with your own implementation like so:

context.replaceConnection(new Connection() {
  public RemoteObject createRemoteObject(String remoteType) {
    return mockRemoteObject;
  }
});

RAP 3.0 will be released together with Eclipse Mars in June 2015. The TestContext is already available in the latest stable build.

Ralf Sternberg

Ralf Sternberg

Ralf is a software engineer with a history as Eclipse committer and project lead.

In recent years, he devoted himself to JavaScript technologies and helped pulling off …