To start the service, I know one uses new MyService().run(args)
. How to stop it?
I need to start and stop programmatically for setUp()
and tearDown()
in my tests.
To start the service, I know one uses new MyService().run(args)
. How to stop it?
I need to start and stop programmatically for setUp()
and tearDown()
in my tests.
You can start the service in new thread, once the test ends the service will shutdown automatically.
However starting in dropwizard 0.6.2 the dropwizard-testing module contains a junit rule exactly for this use case (see here).
Usage of this rule will look something like this:
Or you use this java feature in your main/constructor ...:
Keep the
environment
variable around and add the following method to your application:Now you can call
myService.stop()
to stop the server.Thanks @LiorH for this great suggestion.
Here is a complete test class using the DropwizardServiceRule in dropwizard-0.6.2.
First create a service configuration for testing:
testing-server.yml
and place it in the test's class path (ex.src\test\resources
). This way you can set different ports for the test service to use:A simple test class that checks if there is a resource at the location "/request" looks like this:
I have also added the import in case you do not know what implementation to choose.
At the end of the tests, the server will shutdown gracefully, so you do not need to worry about it.
you can try using stop() method of org.eclipse.jetty.server.Server, which is internally used by Dropwizard.