[aerogear-dev] [ag-controller] Simplifying Route testing

Matthias Wessendorf matzew at apache.org
Tue Feb 19 06:32:03 EST 2013


that looks pretty lean!

nice clean ups!

-M

On Tue, Feb 19, 2013 at 9:44 AM, Daniel Bevenius
<daniel.bevenius at gmail.com> wrote:
> Simplifying Route testing
>
> For 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));
>     }
>
> 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));
>     }
>
>
> _______________________________________________
> aerogear-dev mailing list
> aerogear-dev at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/aerogear-dev



-- 
Matthias Wessendorf

blog: http://matthiaswessendorf.wordpress.com/
sessions: http://www.slideshare.net/mwessendorf
twitter: http://twitter.com/mwessendorf


More information about the aerogear-dev mailing list