whiterook6
whiterook6

Reputation: 3544

Unit testing views in dropwizard

I'm trying to test that a view returned from a drowpizard resource is assembled correctly. Specifically, the view to add a user has a field that allows a password checker to reject an attempt to build a user if it fails to meet certain rules. The view works great when deploying and running the webapp, and specifying a bad password, but when I try to unit test it, it throws a web application exception saying there's no message body writer for the view.

My unit test is pretty plain:

@Test
public void testBadPassword(){
    ClientResponse response = null;
    try {
         response=client().resource("/users").post(ClientResponse.class, createUserParameters);
         Status status = response.getClientResponseStatus();
         assertEquals(Status.SEE_OTHER, status);
    } finally {
        if (response != null) {
            response.close();
        }
    }
}

I get back a server 500 error, which has buried within the following:

javax.ws.rs.WebApplicationException: com.sun.jersey.api.MessageException: A message body writer for Java class com.seeker.stm.resources.views.AddUserView, and Java type class com.seeker.stm.resources.views.AddUserView, and MIME media type text/html was not found
    at com.sun.jersey.spi.container.ContainerResponse.write(ContainerResponse.java:285)
    at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1448)

The resource dealing with the request and generating the views and responses looks like this:

@Path("/users")
public class UserResource {

    public Object addUser(@FormParam("username") String username,
        @FormParam("password") String password,
        @FormParam("email") String email){
    Set<String> passwordErrors = passwordChecker.checkValidity(password, username, email);
    if (!passwordErrors.isEmpty()) {            
        return new AddUserView(userDAO.getAllActive(), passwordErrors);
    }

    // ...
}

I can understand the exception (the writers it knows about, like StringProvider and ByteArrayProvider, don't work for a response or a view or something) but I'd more like to know how to properly test a resource that can return either a response code, or a view.

Upvotes: 2

Views: 2968

Answers (2)

user3437938
user3437938

Reputation: 1

I'm using dropwizard 0.8.0-rc3 and my views are freemarker template based. I tried the above and kept getting exceptions. What finally got it working for me was calling

.addProvider(new ViewMessageBodyWriter(new MetricRegistry(), Collections.singleton(new FreemarkerViewRenderer())))

on my ResourceTestRule.builder().

Upvotes: 0

Michael Fairley
Michael Fairley

Reputation: 13218

You need to add addProvider(ViewMessageBodyWriter.class); to your setUpResources.

ViewMessageBodyWriter is added as a provider by ViewBundle, which is why it works in your service (where you probably have bootstrap.addBundle(new ViewBundle());).

Upvotes: 4

Related Questions