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 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)
,
RequestFixtureAction
public static HandlingResult handle(Action<? super Chain> chainAction, 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)
,
RequestFixtureAction
public static RequestFixture requestFixture()
handlers
.handle(Handler, Action)
,
handle(Action, Action)