Dropwizard Unit/Integration Testing Framework #9613
caspianb
started this conversation in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Link: https://github.com/caspianb/dropwizard-testing
https://central.sonatype.com/artifact/com.logicalbias/dropwizard-testing
I recently (first half of this year) started at a company which uses DropWizard for a lot of their services. I had never used DropWizard before, but I have many years of Spring Boot experience; including writing auto-configuration libraries for things like multi-tenant security, database access, kafka, and so on at previous company.
There are many things that I find I like about DropWizard, but I also find that I miss some of the niceties that the Spring Boot world provides. Especially when it comes to writing "integration" tests - and by integration tests, I mean unit tests where I would define the unit as the API of the service. So you might still use an embedded database or kafka while mocking out things like web clients which call other services.
In SpringBoot, you can spin up the application, start up an embedded database, embedded kafka, and mock out services just by popping a few annotations on the test class. I looked into the DropwizardAppExtension and have done much the same thing against DropWizard testing.
Here's a (working) example of a test in a sandbox project using custom annotations I have created (these annotations can also be placed on an interface or parent class and will be inherited).
The first thing to notice is
@DropwizardTestwhich, at its core, is just an annotation wrapper around thedropwizard-testingDropwizardAppExtension class. It uses an annotation based approach to create it and inject it into the test. It also provides a@MockBeanand@Importannotation which will inject mocks (Mockito) or test classes into the HK2 context. Assuming the service uses HK2 instead of manually 'newing' the dependencies, this means the running test application will execute against these imported or mocked classes allowing the test to target mock specific spots inside the running application.Building off that, I also created a dynamo testing module which uses the above hook points to inject database clients into the running application which target an embedded or dockerized container (the same could be done for JDBC, Mongo, or whatever other 3rd party dependencies you might want to mock/containerize out in the test application -- such as Kafka as the @KafkaTest annotation shows above to start up an embedded kafka service and inject the connection properties back into the test application).
I do wonder if the base
@DropwizardTestannotation,@MockBean,@Importare something that might be considered for dropwizard-testing or should I look at releasing my own standalone testing library built on top of it? Would there be any interest in such a tool?Beta Was this translation helpful? Give feedback.
All reactions