Sample Dropwizard Resource Test

I’m prototyping a dropwizard app, and it wasn’t clear in the docs how to write resource tests that handle authentication.  This example uses OAuth authentication.  I also found it annoying that there were no import statements in the documents.  Anyway, I hope this helps.  There are probably more efficient ways to do this, so I’ll update this post as I find out more.

package com.yourcompany.resource;

import static org.fest.assertions.api.Assertions.assertThat;
import static org.mockito.Matchers.anyObject;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.util.LinkedList;
import java.util.List;


import org.eclipse.jetty.server.Authentication.User;
import org.junit.Before;
import org.junit.Test;

import com.yourcompany.dao.SampleDao;
import com.yourcompany.dto.SampleItem;
import com.yourcompany.dto.Sample;
import com.yourcompany.oauth.OAuthAuthenticator;
import com.sun.jersey.api.client.ClientHandlerException;
import com.sun.jersey.api.client.ClientRequest;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.filter.ClientFilter;
import com.yammer.dropwizard.auth.oauth.OAuthProvider;
import com.yammer.dropwizard.testing.ResourceTest;

public class SampleResourceTest extends ResourceTest {
 private final Sample sample = new Sample();
 private final SampleDao dao = mock(SampleDao.class);

 protected void setUpResources() {
 List<SampleItem> sampleItems = new LinkedList<SampleItem>();


 addResource(new SampleResource(dao));
 addProvider(new OAuthProvider<User>(new OAuthAuthenticator(), "CLIENT_SECRET"));

 public void testBefore() {
 client().addFilter(new ClientFilter() {
 private List<Object> authorizations;

 public ClientResponse handle(ClientRequest request) throws ClientHandlerException {
 if (authorizations == null) {
 authorizations = new LinkedList<Object>();
 authorizations.add("Bearer {test token}");
 request.getHeaders().put("Authorization", authorizations);
 ClientResponse response = getNext().handle(request);

 return response;

 public void testFetchSample() throws Exception {
 Sample retrievedSample = client().resource(String.format("/sample-service/sample/id/", "id")).get(Sample.class);

 public void testPostSample() throws Exception {
 Sample retrievedSample = client().resource("/sample-service/sample").type(MediaType.APPLICATION_JSON_TYPE).post(Favorites.class, favorites);

 private void compareSample(Sample retrievedSample) {
 // other assertions

I created a TestOAuthAuthenticator that I pass into my test as a provider. This authenticator has pre-defined tokens and users for tests.


One thought on “Sample Dropwizard Resource Test

  1. Hello admin, i found this post on 21 spot in google’s search results.
    You should reduce your bounce rate in order to rank
    in google. This is major ranking factor nowadays.
    There is very handy wordpress plugin which can help you.
    Just search in google for:
    Lilas’s Bounce Plugin

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s