Does anyone know how to add a test resource (i.e. one that is intended only for testing and not added to the run () method of the application)?
Here is an example:
public class MyTest { @ClassRule public static final DropwizardAppRule<TestConfiguration> RULE = new DropwizardAppRule<TestConfiguration>(MyApp.class, "my-app-config.yaml"); @BeforeClass public static void setUpBeforeClass() throws Exception { MyTest.RULE.getEnvironment().jersey().register(new JustForTestingResource()); } @Test public final void testTestResource() { Client client = new Client(); ClientResponse response = client.resource( String.format("http://localhost:%d/rest/v1/test", RULE.getLocalPort())) .get(ClientResponse.class); assertThat(response.getStatus(), is(200)); } }
and
public class JustForTestingRessource { @GET @Path("test") @Produces(MediaType.APPLICATION_JSON) public Response getInTestResource() { return Response.status(Status.OK).type(MediaType.TEXT_PLAIN).entity("get @Path(\"test\") is ok").build(); } }
My problem is that the added resource has not been added, and I get a resource not found with error 404. It seems that I am registering a new resource after publishing the resources, and there isn’t any dropwizard after starting the update.
I don’t want to extend my application class, and I don’t want to embed test code in real application code. Does anyone know how to register a test resource without registering it in the run () method of the application?
This works, but a new class is needed:
public class TestService extends MyService{ @Override public void run( TestConfigurationconfiguration, Environment environment) throws ClassNotFoundException { environment.jersey().register(new JustForTestingRessource()); super.run(configuration,environment); } }
Call JUnit as you already know:
@ClassRule public static DropwizardAppRule<TestConfiguration> RULE = new DropwizardAppRule<TestConfiguration>(TestService.class, "my-app-config.yaml");
source share