public abstract class UnitTest
extends java.lang.Object
| Modifier and Type | Method and Description |
|---|---|
static ExecHarness |
execHarness()
Creates a new execution harness, for unit testing code that produces a promise.
|
static HandlingResult |
handle(ratpack.func.Action<? super ratpack.handling.Chain> chainAction,
ratpack.func.Action<? super RequestFixture> requestFixtureAction)
Unit test a
Handler chain. |
static HandlingResult |
handle(ratpack.handling.Handler handler,
ratpack.func.Action<? super RequestFixture> action)
Unit test a single
Handler. |
static RequestFixture |
requestFixture()
Create a request fixture, for unit testing of
handlers. |
public static HandlingResult handle(ratpack.handling.Handler handler, ratpack.func.Action<? super RequestFixture> action) throws HandlerTimeoutException
Handler.
import ratpack.handling.Handler;
import ratpack.handling.Context;
import ratpack.test.handling.HandlingResult;
import ratpack.test.handling.RequestFixture;
import ratpack.test.handling.RequestFixtureAction;
import ratpack.test.UnitTest;
public class Example {
public static class MyHandler implements Handler {
public void handle(Context context) {
String outputHeaderValue = context.getRequest().getHeaders().get("input-value") + ":bar";
context.getResponse().getHeaders().set("output-value", outputHeaderValue);
context.render("received: " + context.getRequest().getPath());
}
}
public static void main(String[] args) {
HandlingResult result = UnitTest.handle(new MyHandler(), new RequestFixtureAction() {
public void execute() {
header("input-value", "foo");
uri("some/path");
}
});
assert result.rendered(String.class).equals("received: some/path");
assert result.getHeaders().get("output-value").equals("foo:bar");
}
}
handler - The handler to invokeaction - The configuration of the context for the handlerHandlerTimeoutException - if the handler takes more than RequestFixture.timeout(int) seconds to send a response or call next() on the contexthandle(Action, Action),
RequestFixtureActionpublic static HandlingResult handle(ratpack.func.Action<? super ratpack.handling.Chain> chainAction, ratpack.func.Action<? super RequestFixture> requestFixtureAction) throws HandlerTimeoutException
Handler chain.
import ratpack.handling.Context;
import ratpack.handling.Handler;
import ratpack.handling.ChainAction;
import ratpack.test.handling.HandlingResult;
import ratpack.test.handling.RequestFixtureAction;
import ratpack.test.UnitTest;
public class Example {
public static class MyHandlers extends ChainAction {
protected void execute() {
handler(new Handler() {
public void handle(Context context) {
String outputHeaderValue = context.getRequest().getHeaders().get("input-value") + ":bar";
context.getResponse().getHeaders().set("output-value", outputHeaderValue);
context.next();
}
});
handler(new Handler() {
public void handle(Context context) {
context.render("received: " + context.getRequest().getPath());
}
});
}
}
public static void main(String[] args) {
HandlingResult result = UnitTest.handle(new MyHandlers(), new RequestFixtureAction() {
public void execute() {
header("input-value", "foo");
uri("some/path");
}
});
assert result.rendered(String.class).equals("received: some/path");
assert result.getHeaders().get("output-value").equals("foo:bar");
}
}
chainAction - the definition of a handler chain to testrequestFixtureAction - the configuration of the request fixtureHandlerTimeoutException - if the handler takes more than RequestFixture.timeout(int) seconds to send a response or call next() on the contexthandle(Handler, Action),
RequestFixtureActionpublic static RequestFixture requestFixture()
handlers.handle(Handler, Action),
handle(Action, Action)public static ExecHarness execHarness()
import ratpack.func.Action;
import ratpack.func.Function;
import ratpack.exec.ExecControl;
import ratpack.exec.Execution;
import ratpack.exec.Promise;
import ratpack.exec.Fulfiller;
import ratpack.test.UnitTest;
import ratpack.test.exec.ExecHarness;
import javax.inject.Inject;
public class Example {
// An async callback based API
static class AsyncApi {
static interface Callback<T> {
void receive(T value);
}
public <T> void returnAsync(final T value, final Callback<? super T> callback) {
new Thread() {
public void run() {
callback.receive(value);
}
}.run();
}
}
// Our service class that wraps the raw async API
// In the real app this is created by the DI container (e.g. Guice)
static class AsyncService {
private final ExecControl execControl;
private final AsyncApi asyncApi = new AsyncApi();
@Inject
public AsyncService(ExecControl execControl) {
this.execControl = execControl;
}
// Our method under test
public <T> Promise<T> promise(final T value) {
return execControl.promise(new Action<Fulfiller<T>>() {
public void execute(final Fulfiller<T> fulfiller) {
asyncApi.returnAsync(value, new AsyncApi.Callback<T>() {
public void receive(T returnedValue) {
fulfiller.success(returnedValue);
}
});
}
});
}
}
// Our test
public static void main(String[] args) throws Throwable {
// the harness must be close()'d when finished with to free resources
try(ExecHarness harness = UnitTest.execHarness()) {
// set up the code under test using the exec control from the harness
final AsyncService service = new AsyncService(harness.getControl());
// exercise the async code using the harness, blocking until the promised value is available
String value = harness.execute(new Function<Execution, Promise<String>>() {
public Promise<String> apply(Execution execution) {
// execute the code under test
return service.promise("foo");
}
});
assert value == "foo";
}
}
}