Simplifying Route testing
For AEROGEAR-778 <
https://issues.jboss.org/browse/AEROGEAR-778> we have
been looking into making it easier to test routes. We have been using
Mockito which works nicely, but there was a bit of duplicated code spread
across different tests and also the test were not that easy to read.
Below are examples of one of the tests before and after, hopefully the
after will be easier to read.
Before
@Test
public void testRestRouteWithPathParam() throws Exception {
final RoutingModule routingModule = new AbstractRoutingModule() {
@Override
public void configuration() {
route()
.from("/car/{id}").roles("admin")
.on(GET)
.produces(mockJsp(), mockJson())
.to(SampleController.class).find(param("id"));
}
};
final Routes routes = routingModule.build();
when(request.getMethod()).thenReturn(RequestMethod.GET.toString());
when(request.getServletContext()).thenReturn(servletContext);
when(servletContext.getContextPath()).thenReturn("/abc");
when(request.getRequestURI()).thenReturn("/abc/car/3");
when(request.getHeader("Accept")).thenReturn("application/json");
when(jsonResponder.accepts("application/json")).thenReturn(true);
when(jsonResponder.mediaType()).thenReturn(mockJson());
final Route route = routes.routeFor(GET, "/car/{id}",
acceptHeaders(JSON.getMediaType()));
router.process(new RouteContext(route, request, response, routes));
verify(jsonResponder).respond(anyObject(), any(RouteContext.class));
}
<
https://gist.github.com/danbev/4983976#after>After
@Test
public void testRestRouteWithPathParam() throws Exception {
final RouteTester routeTester = RouteTester.from(new
AbstractRoutingModule() {
@Override
public void configuration() {
route()
.from("/car/{id}").roles("admin")
.on(GET)
.produces(JSP, JSON)
.to(SampleController.class).find(param("id"));
}
});
routeTester.acceptHeader(JSON).processGetRequest("/car/3");
verify(routeTester.<SampleController>getController()).find("3");
verify(routeTester.jsonResponder()).respond(any(),
any(RouteContext.class));
}