Repository navigation
Testing
An Oriel application can be tested without opening a window or a browser. oriel-server publishes a test-jar that serves the application on a real port and calls it the way its page does.
<dependency>
<groupId>com.github.unigrid-project.oriel</groupId>
<artifactId>oriel-server</artifactId>
<version>0.0.1</version>
<type>test-jar</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.jqwik</groupId>
<artifactId>jqwik</artifactId>
<scope>test</scope>
</dependency>It comes from JitPack like the rest of Oriel; see Getting started. ServedTest starts and stops the server around each test with jqwik's lifecycle, so the tests are jqwik @Examples. The test-jar brings no logging setup, so the application's own logback-test.xml applies.
| Helper | What it does |
|---|---|
ServedTest |
Serves routes() on a real port for one test. It has id(), token(), templates() and base(). anonymous() gives a caller without the session, and admitted() gives one that has been through the session handshake |
Client |
A caller that keeps its own cookies: get(path), post(path), submit(path, form) for a form body, and header(name, value) for a copy that sends one more header |
RecordingWindow |
A WindowControl that remembers every command in the words the page uses, such as "minimise" or "resize/start/left". picking(path) makes its dialogs answer with that file, and await(count, timeout) waits for commands that arrive on the server's threads |
Page, Status
|
Small views Oriel's own tests use, over templates/oriel-test/
|
Override routes() with the application's own routes, then call it as the page would. From Oriel's own ActionHandlerTest, which binds the Ledger shown on Actions:
public class ActionHandlerTest extends ServedTest {
private final Ledger ledger = new Ledger();
@Override
protected Handler routes() {
return Routes.create(id(), templates(), token(), WindowControl.NONE, Actions.of(ledger), new Page("Ledger"));
}
@Example
public void shouldCallTheAnnotatedMethod() throws Exception {
assertEquals(204, admitted().post("/action/lock-ledger").statusCode());
}
@Example
public void shouldHandTheFormToTheMethod() throws Exception {
final HttpResponse<String> response = admitted()
.submit("/action/unlock-ledger", "passphrase=open+sesame%21");
assertEquals(200, response.statusCode());
assertEquals(List.of("open sesame!"), ledger.unlocked());
}
@Example
public void shouldRenderTheViewThatWasReturned() throws Exception {
final HttpResponse<String> response = admitted()
.submit("/action/unlock-ledger", "passphrase=x");
assertTrue(response.body().contains("Ledger is unlocked"), response.body());
}
@Example
public void shouldNotActForAnUnknownCaller() throws Exception {
assertEquals(403, anonymous().post("/action/lock-ledger").statusCode());
}
}The controller is made by hand with Actions.of(…), so no CDI container is needed. The other status codes are worth testing too: 404 for an unknown action, and 500 for one that fails; see Actions.
Give the routes a RecordingWindow and check what reached it. From Oriel's own WindowHandlerTest:
private final RecordingWindow window = new RecordingWindow();
@Override
protected Handler routes() {
return Routes.create(id(), templates(), token(), window, Actions.of(), new Page("Ledger"));
}
@Example
public void shouldAnswerWithTheFileTheHostChose() throws Exception {
final Path ledger = Path.of("/mnt/backup/ledger.csv");
window.picking(ledger);
final HttpResponse<String> response = admitted().post("/window/choose-file?title=Pick+a+ledger");
assertEquals(200, response.statusCode());
assertEquals(ledger.toString(), response.body());
assertEquals(List.of("choose-file:Pick a ledger"), window.commands());
}A view is a plain object, and Templates.render(view) turns it into HTML without a server:
final String html = new Templates(false).render(new Status("alice")); // "Ledger is unlocked by alice"They exercise the server, the actions and the templates, but not the scripts running in Chromium or the native window. For those, drive the real window through -Doriel.remote-debugging-port, for example with Playwright.