public abstract class UnitTest extends Object
Modifier and Type | Method and Description |
---|---|
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)