public class UnitTest extends Object
| Constructor and Description |
|---|
UnitTest() |
| Modifier and Type | Method and Description |
|---|---|
static InvocationBuilder |
invocationBuilder()
Create an invocation builder, for unit testing a
Handler. |
static Invocation |
invoke(Handler handler,
Action<? super InvocationBuilder> action)
Unit test a
Handler. |
public static Invocation invoke(Handler handler, Action<? super InvocationBuilder> action) throws InvocationTimeoutException
Handler.
Example:
import ratpack.handling.*;
import ratpack.util.Action;
import ratpack.test.handling.Invocation;
import ratpack.test.handling.InvocationBuilder;
import static ratpack.test.UnitTest.invoke;
public 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());
}
}
// The following code unit tests MyHandler, typically it would be written inside a test framework.
Invocation invocation = invoke(new MyHandler(), new Action<InvocationBuilder>() {
public void execute(InvocationBuilder builder) {
builder.header("input-value", "foo");
builder.uri("some/path");
}
});
assert invocation.rendered(String).equals("received: some/path");
assert invocation.headers.get("output-value").equals("foo:bar");
handler - The handler to invokeaction - The configuration of the context for the handlerInvocationTimeoutException - if the handler takes more than InvocationBuilder.timeout(int) seconds to send a response or call next() on the contextpublic static InvocationBuilder invocationBuilder()
Handler.invoke(ratpack.handling.Handler, ratpack.util.Action)