Integrated Dropwizard Testing with TestResource

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"); 
+6
source share
4 answers

Edit: delete the previous answer because it did not solve your problem the way you wanted to do it.

I burst into the startup code of the environment and realized why the registration of the controller did not make it available, because the pier is already running. If you stop the berth, register your controller and return the berth again, your resource will be available and you can use it in your test.

 @BeforeClass public static void setUpBeforeClass() throws Exception { MyTest.RULE.environment.applicationContext.stop() MyTest.RULE.environment.jersey().register(new JustForTestingResource()) MyTest.RULE.environment.applicationContext.start() } 
+5
source

You can test the resource itself in a jersey container without running a full dw instance.

Check out Testing Resources .

 import static org.fest.assertions.api.Assertions.assertThat; import static org.mockito.Mockito.*; public class PersonResourceTest { private static final PeopleStore dao = mock(PeopleStore.class); @ClassRule public static final ResourceTestRule resources = ResourceTestRule.builder() .addResource(new PersonResource(dao)) .build(); private final Person person = new Person("blah", " blah@example.com "); @Before public void setup() { when(dao.fetchPerson(eq("blah"))).thenReturn(person); // we have to reset the mock after each test because of the // @ClassRule, or use a @Rule as mentioned below. reset(dao); } @Test public void testGetPerson() { assertThat(resources.client().resource("/person/blah").get(Person.class)) .isEqualTo(person); verify(dao).fetchPerson("blah"); } } 
+2
source

I had a similar problem with @ClassRule, maybe this can help someone ..
In my test (Groovy), calling RULE.getApplication () or getEnvironment () from the @BeforeClass method returned null:

 def setupSpec() { RULE.application.run() } 

shown

 java.lang.NullPointerException: Cannot invoke method run() on null object 

those. RULE.testSupport had both a zero application and an environment.

I found out that calling RULE.testSupport.before () before running () resolves the error:

 def setupSpec() { RULE.testSupport.before() RULE.application.run() } 

And then the @AfterClass method:

 def cleanupSpec() { RULE.testSupport.after() } 

Or just use @Rule instead of @ClassRule and call

 def setup() { RULE.application.run() } 

inside the @Before method instead of @BeforeClass.
Although it seems strange, there may be another better solution.

0
source

Public class TestMain extends Main {

 public static void main(String ... args) throws Exception { new TestMain().run(args); } @Override public void initialize(Bootstrap<AppConfiguration> bootstrap) { super.initialize(bootstrap); bootstrap.addBundle( new MigrationsBundle<AppConfiguration>() { @Override public DataSourceFactory getDataSourceFactory( AppConfiguration configuration) { return configuration.getDataSourceFactory(); } }); } 

}

0
source

Source: https://habr.com/ru/post/976778/


All Articles