Table of contents
1.
🙋Introduction
2.
Integration Testing🧑‍💻
3.
📃Junit5
4.
📃JUnit4
5.
📃Non-JUnit
6.
Frequently Asked Questions
6.1.
What is Dropwizard?
6.2.
What are the components of Dropwizard?
6.3.
What annotation and extension are present in JUnit5?
6.4.
How can we manually test an application in Dropwizard?
7.
Conclusion
Last Updated: Mar 27, 2024
Medium

Dropwizard-Integration Testing

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

🙋Introduction

Dropwizard is an open-source Java Framework. It helps develop RESTful backends. What is a web application without HTTP? Dropwizard has the Jetty HTTP library that embeds an HTTP server to the project. Dropwizard provides the facility of running the web applications on Jetty server. Jetty provides servlet container and a web serve. If you are building a RESTful web application, dropwizard has Jersey(RESTful Framework), which provides the best performance and features. For dealing with JSON data format, dropwizard has Jackson(JSON library for JVM). Before proceeding have a look at Dropwizard Configuration Resources and Dropwizard Internals.

This article is an insight into integration testing in Dropwizard.  

Integration Testing🧑‍💻

Whenever we write an application, we want to check how it will perform when it is hit with an HTTP request. To do this, we start an application and review the entire application with an actual request. In dropwizard, we have a dropwizard-testing module that provides all the helper classes we need to perform integration testing. Read about Dropwizard Testing Representations and Dropwizard Testing Resources before proceeding.

integration testing

There's also an optional dropwizard-client module that provides some extra helpers that help in integration testing, such as custom JerseyClientBuilder. It is helpful because it is aware of our application’s environment.

📃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 DBMSSoftware engineering, and computer networks. Do not stop here! Expand your knowledge base by learning topics like  computer visionbig datamachine 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!

Live masterclass