Created
January 24, 2015 08:52
-
-
Save javaeeeee/19467583527917969a76 to your computer and use it in GitHub Desktop.
A jUnit test for Dropwizard/Jersey Resource
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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