Dropwizard: How to stop service programmatically

Neo picture Neo · Apr 13, 2013 · Viewed 8.4k times · Source

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.

Answer

LiorH picture LiorH · Apr 14, 2013

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:

Class MyTest {

    @ClassRule
    public static TestRule testRule = new DropwizardServiceRule<MyConfiguration>(MyService.class,
                    Resources.getResource("service.yml").getPath()));

    @Test
    public void someTest(){
    ....