Qualcuno sa come aggiungere una risorsa di test (ad esempio una che è solo a scopo di test e non aggiunta nel metodo run() dell'app)?Test integrato Dropwizard con TestResource
Ecco un esempio:
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));
}
}
e
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();
}
}
Il mio problema è che la risorsa aggiunto non viene aggiunto e ottengo risorsa non trovato 404 risposta di errore. Sembra che io stia registrando la nuova risorsa dopo la pubblicazione delle risorse e non ci sia alcun aggiornamento all'interno di Dropwizard dopo l'avvio.
Non desidero estendere la mia classe di applicazione e non desidero inserire il codice di test nel mio codice applicazione reale. Qualcuno sa come registrare la risorsa di test senza registrarla nel metodo run() dell'applicazione?
Questo funziona, ma è necessaria una nuova classe:
public class TestService extends MyService{
@Override
public void run(
TestConfigurationconfiguration,
Environment environment) throws ClassNotFoundException
{
environment.jersey().register(new JustForTestingRessource());
super.run(configuration,environment);
}
}
Chiamata in JUnit come già noto:
@ClassRule
public static DropwizardAppRule<TestConfiguration> RULE =
new DropwizardAppRule<TestConfiguration>(TestService.class, "my-app-config.yaml");
Qualcosa di simile RULE.addRessource (...) sarebbe nice ... – user3280180