public class MockRouter extends Object
In this section we are going to see how to run unit and integration tests in Jooby.
We do offer two programming models:
You don't need much for MVC routes, because a route got binded to a method of some class. So it is usually very easy and simple to mock and run unit tests against a MVC route.
We can't say the same for script routes, because a route is represented by a lambda and there is no easy or simple way to get access to the lambda object.
We do provide a MockRouter which simplify unit tests for script routes:
public class MyApp extends Jooby {
{
get("/test", () -> "Hello unit tests!");
}
}
A unit test for this route, looks like:
@Test
public void simpleTest() {
String result = new MockRouter(new MyApp())
.get("/test");
assertEquals("Hello unit tests!", result);
}
Just create a new instance of MockRouter with your application and call one of the HTTP method, like get, post, etc...
You're free to choose the mock library of your choice. Here is an example using EasyMock:
{
get("/mock", req -> {
return req.path();
});
}
A test with EasyMock looks like:
@Test
public void shouldGetRequestPath() {
Request req = EasyMock.createMock(Request.class);
expect(req.path()).andReturn("/mypath");
EasyMock.replay(req);
String result = new MockRouter(new MyApp(), req)
.get("/mock");
assertEquals("/mypath", result);
EasyMock.verify(req);
}
You can mock a Response object in the same way:
{
get("/mock", (req, rsp) -> {
rsp.send("OK");
});
}
A test with EasyMock looks like:
@Test
public void shouldUseResponseSend() {
Request req = EasyMock.createMock(Request.class);
Response rsp = EasyMock.createMock(Response.class);
rsp.send("OK");
EasyMock.replay(req, rsp);
String result = new MockRouter(new MyApp(), req, rsp)
.get("/mock");
assertEquals("OK", result);
EasyMock.verify(req, rsp);
}
What about external dependencies? It works in a similar way:
{
get("/", () -> {
HelloService service = require(HelloService.class);
return service.salute();
});
}
@Test
public void shouldMockExternalDependencies() {
HelloService service = EasyMock.createMock(HelloService.class);
expect(service.salute()).andReturn("Hola!");
EasyMock.replay(service);
String result = new MockRouter(new MyApp())
.set(service)
.get("/");
assertEquals("Hola!", result);
EasyMock.verify(service);
}
The set(Object) call push and register an external dependency (usually a mock). This make it possible to resolve services from require calls.
Mock of promises are possible too:
{
get("/", promise(deferred -> {
deferred.resolve("OK");
}));
}
@Test
public void shouldMockPromises() {
String result = new MockRouter(new MyApp())
.get("/");
assertEquals("OK", result);
}
Previous test works for deferred routes:
{
get("/", deferred(() -> {
return "OK";
}));
}
| Constructor and Description |
|---|
MockRouter(Jooby app) |
MockRouter(Jooby app, Request req) |
MockRouter(Jooby app, Request req, Response rsp) |
| Modifier and Type | Method and Description |
|---|---|
<T> T |
delete(String path) |
<T> T |
execute(String method, String path) |
<T> T |
get(String path) |
<T> T |
patch(String path) |
<T> T |
post(String path) |
<T> T |
put(String path) |
MockRouter |
set(Object dependency) |
MockRouter |
set(String name, Object object) |
public MockRouter(Jooby app)
public MockRouter set(Object dependency)
public MockRouter set(String name, Object object)
Copyright © 2019. All rights reserved.