Kann jemand eine Testressource hinzufügen (d. H. Eine, die nur für Testzwecke und nicht in run() -Methode der App hinzugefügt)? HierDropwizard Integrated Testing mit TestResource
ein Beispiel:
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));
}
}
und
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();
}
}
Mein Problem ist, dass die zugegebene Ressource hinzugefügt wird und ich Ressource nicht 404 Fehlerreaktion gefunden. Es scheint, dass ich die neue Ressource nach der Veröffentlichung von Ressourcen registriere und es gibt keine Aktualisierung in DropWizard nach dem Start.
Ich möchte nicht meine Application-Klasse erweitern und ich möchte Testcode nicht in meinen realen Anwendungscode einfügen. Weiß jemand, wie man die Testressource registriert, ohne sie in der Methode run() der Anwendung zu registrieren?
Dies funktioniert, aber eine neue Klasse benötigt wird:
public class TestService extends MyService{
@Override
public void run(
TestConfigurationconfiguration,
Environment environment) throws ClassNotFoundException
{
environment.jersey().register(new JustForTestingRessource());
super.run(configuration,environment);
}
}
Anruf in JUnit wie bereits bekannt:
@ClassRule
public static DropwizardAppRule<TestConfiguration> RULE =
new DropwizardAppRule<TestConfiguration>(TestService.class, "my-app-config.yaml");
So etwas RULE.addRessource (...) wäre nette ... – user3280180