Photo by Nolan Issac on Unsplash

Testing Dropwizard Embedded with JUnit5

With minimal changes to your unit test code, you can start reaping the benefits of JUnit5 for your Dropwizard-based applications!

It has been a bit over a year since JUnit5 was released. After almost a decade of using JUnit4, the industry is moving slowly to this new version.

Here at HomeAway, we use Dropwizard as our Java application framework. One of the great features that Dropwizard provides is a slim version of the framework that runs for each test that uses it. For this type of testing, the Dropwizard framework provides libraries invoked with classrules. Unfortunately, classrule is one of the APIs that was removed from JUnit5. This causes problems when migrating from JUnit4 or earlier.

The good news is that, starting with Dropwizard 1.3.0, the test tools have been revamped to use JUnit5's new extension model. Unfortunately, there is no documentation on how to update existing tests, but it is actually a simple process.

The old DropwizardAppRule has been replaced with DropwizardAppExtension, which starts a whole dropwizard application in a very similar fashion.

This is what a typical embedded test would look like using the classrule approach (JUnit4):

To migrate this test to JUnit5, you only need to do the following refactoring:

These code snippets don’t show the package changes and the setup of the POM, but this is the basis of how you can start an embedded Dropwizard app with JUnit5. There are several other extensions that are well documented with the class rule approach in the Dropwizard testing doc which are easily translated using this approach.