Skip to content

Instantly share code, notes, and snippets.

@javaeeeee
Created January 24, 2015 08:52
Show Gist options
  • Select an option

  • Save javaeeeee/19467583527917969a76 to your computer and use it in GitHub Desktop.

Select an option

Save javaeeeee/19467583527917969a76 to your computer and use it in GitHub Desktop.
A jUnit test for Dropwizard/Jersey Resource
public class HelloResourceTest {
@Rule
public ResourceTestRule resource = ResourceTestRule.builder()
.addResource(new HelloResource()).build();
@Test
public void testGetGreeting() {
String expected = "Hello world!";
//Obtain client from @Rule.
Client client = resource.client();
//Get WebTarget from client using URI of root resource.
WebTarget helloTarget = client.target("http://localhost:8080/hello");
//To invoke response we use Invocation.Builder
//and specify the media type of representation asked from resource.
Invocation.Builder builder = helloTarget.request(MediaType.TEXT_PLAIN);
//Obtain response.
Response response = builder.get();
//Do assertions.
assertEquals(Response.Status.OK, response.getStatusInfo());
String actual = response.readEntity(String.class);
assertEquals(expected, actual);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment