public abstract class UnitTest extends 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(Action<? super Chain> chainAction,
Action<? super RequestFixture> requestFixtureAction)
Unit test a
Handler chain. |
static HandlingResult |
handle(Handler handler,
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(Handler handler, Action<? super RequestFixture> action) throws Exception
Handler
.
import ratpack.handling.Context;
import ratpack.handling.Handler;
import ratpack.test.UnitTest;
import ratpack.test.handling.HandlingResult;
public class Example {
public static class MyHandler implements Handler {
public void handle(Context ctx) throws Exception {
String outputHeaderValue = ctx.getRequest().getHeaders().get("input-value") + ":bar";
ctx.getResponse().getHeaders().set("output-value", outputHeaderValue);
ctx.render("received: " + ctx.getRequest().getPath());
}
}
public static void main(String[] args) throws Exception {
HandlingResult result = UnitTest.handle(new MyHandler(), fixture ->
fixture.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 contextException
- any thrown by action
handle(Action, Action)
public static HandlingResult handle(Action<? super Chain> chainAction, Action<? super RequestFixture> requestFixtureAction) throws Exception
Handler
chain.
import ratpack.func.Action;
import ratpack.handling.Chain;
import ratpack.test.UnitTest;
import ratpack.test.handling.HandlingResult;
public class Example {
public static class MyHandlers implements Action<Chain> {
public void execute(Chain chain) throws Exception {
chain.handler(ctx -> {
String outputHeaderValue = ctx.getRequest().getHeaders().get("input-value") + ":bar";
ctx.getResponse().getHeaders().set("output-value", outputHeaderValue);
ctx.next();
});
chain.handler(ctx -> {
ctx.render("received: " + ctx.getRequest().getPath());
});
}
}
public static void main(String[] args) throws Exception {
HandlingResult result = UnitTest.handle(new MyHandlers(), fixture ->
fixture.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 contextException
- any thrown by chainAction
or requestFixtureAction
handle(Handler, Action)
public static RequestFixture requestFixture()
handlers
.handle(ratpack.handling.Handler, ratpack.func.Action)
,
handle(ratpack.func.Action, ratpack.func.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 ratpack.test.exec.ExecResult; 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 ExecResult<String> result = harness.execute(new Function<Execution, Promise<String>>() { public Promise<String> apply(Execution execution) { // execute the code under test return service.promise("foo"); } }); assert result.getValue() == "foo"; } } }