📃Junit5
DropwizardExtensionsSupport annotation and DropwizardAppExtension extension are two things we need to add to our JUnit5 test class. This annotation and extension will help in starting the application before any tests start and will stop it after being complete.
The app's environment, configuration, and object are also made known to itself with the help of DropwizardAppExtension so that the tests can query them.
Sometimes we might find the dropwizard-client module extra just for testing purposes; in this case, we can call the client method on the extension by accessing a Jersey HTTP client.
It will return a client. The extension manages this client, and it is also reused during the tests.
Here we have an example👇:
@ExtendWith(DropwizardExtensionsSupport.class)
class AcceptLoginCoding Ninjas Studio {
private static DropwizardAppExtension<TestConfiguration> E = new DropwizardAppExtension<>(
Coding Ninjas StudioApp.class,
ResourceHelpers.resourceFilePath("code-studio-config.yaml")
);
@Test
void loginHandlerRedirectsAfterPost() {
Client client = E.client();
Response r = client.target(
String.format("http://localhost:%d/login", E.getLocalPort()))
.request()
.post(Entity.json(loginForm()));
assertThat(r.getStatus()).isEqualTo(302);
}
}

You can also try this code with Online Java Compiler
Run Code
📃JUnit4
For JUnit4 test classes, everything remains the same as that in JUnit5 test classes except for the DropwizardAppRule. Just like in JUnit5, where the annotation and the extension help start the application before any tests start and will stop it after completion, the same work is done by DropwizardAppRule in JUnit4. The app's environment, configuration, and object are also made known to itself with the help of DropwizardAppRule so that the tests can query them.
Here also, if we find the dropwizard-client module extra just for testing purposes, in this case, we can call the client method on the extension by accessing a Jersey HTTP client.
It will return a client. The extension manages this client, and it is also reused during the tests.
For example👇:
public class AcceptLoginCoding Ninjas Studio {
@ClassRule
public static final DropwizardAppRule<TestConfiguration> R =
new DropwizardAppRule<>(Coding Ninjas Studio.class, ResourceHelpers.resourceFilePath("code-studio-config.yaml"));
@Test
public void loginHandlerRedirectsAfterPost() {
Client client = R.client();
Response res = client.target(
String.format("http://localhost:%d/login", R.getLocalPort()))
.request()
.post(Entity.json(loginForm()));
assertThat(res.getStatus()).isEqualTo(302);
}
}

You can also try this code with Online Java Compiler
Run Code
📃Non-JUnit
We also have an option of creating an instance in our tests so that we can manually test the application by starting and stopping the application while testing. This is done by creating a DropwizardTestSupport instance.
Like in JUnit5 and JUnit4, the app's environment, configuration, and object are also made known to itself with the help of DropwizardTestSupport so that the tests can query them.
Let’s see the example below👇:
public class AcceptLoginCoding Ninjas Studio {
public static final DropwizardTestSupport<TestConfiguration> S =
new DropwizardTestSupport<TestConfiguration>(Coding Ninjas Studio.class,
ResourceHelpers.resourceFilePath("code-studio-config.yaml"),
ConfigOverride.config("server.applicationConnectors[0].port", "0") // Optional, if not using a separate testing-specific
//configuration file, use a randomly selected port
);
@BeforeAll
public void beforeClass() {
S.before();
}
@AfterAll
public void afterClass() {
S.after();
}
@Test
public void loginHandlerRedirectsAfterPost() {
Client client = new JerseyClientBuilder(S.getEnvironment()).build("test_client");
Response res = client.target(
String.format("http://localhost:%d/login", S.getLocalPort()))
.request()
.post(Entity.json(loginForm()));
assertThat(res.getStatus()).isEqualTo(302);
}
}

You can also try this code with Online Java Compiler
Run Code
Frequently Asked Questions
What is Dropwizard?
Dropwizard is an open source Java Framework. It is helpful in developing RESTful backends.
What are the components of Dropwizard?
Dropwizard has the Jetty HTTP library that embeds a HTTP server to the project. If you are building a RESTful web application, dropwizard has Jersey, that provides best performance and features. For dealing with JSON data format dropwizard has Jackson.
What annotation and extension are present in JUnit5?
DropwizardExtensionsSupport annotation and DropwizardAppExtension extension are present in JUnit5.
How can we manually test an application in Dropwizard?
Manual testing of an application in dropwizard is done by creating a DropwizardTestSupport instance.
Conclusion
This article was an insight into dropwizard integration testing. If dropwizard is new to you have a look at these articles like dropwizard internals, dropwizard-testing representation, and dropwizard-testing resources.
Learn DSA and web technologies on our website. Study core subjects like DBMS, Software engineering, and computer networks. Do not stop here! Expand your knowledge base by learning topics like computer vision, big data, machine learning, and deep learning. Also, you can always try our paid courses to upskill yourself.
You can also read about the interview experiences of different candidates here.
Happy learning!