In addition to the test controller for my application specifics, I wrote another one for controlling dropwizard. Its a while since I did this, but I think I probably just took the code from the AppRule and adapted it to my needs. I found this better than the AppRule because I can decide when to call it, not just use it as a test rule, and of course, add my own features to it:
public class DropwizardTestController<C extends Configuration> {
private final Class<? extends Application<C>> applicationClass;
private final String configPath;
private C configuration;
private Application<C> application;
private Environment environment;
private Server jettyServer;
public DropwizardTestController(Class<? extends Application<C>> applicationClass, String configPath) {
this.applicationClass = applicationClass;
this.configPath = configPath;
}
public void start() {
startIfRequired();
}
public void stop() throws Exception {
jettyServer.stop();
}
public C getConfiguration() {
return configuration;
}
public int getLocalPort() {
return ((ServerConnector) jettyServer.getConnectors()[0]).getLocalPort();
}
public int getAdminPort() {
return ((ServerConnector) jettyServer.getConnectors()[1]).getLocalPort();
}
public <A extends Application<C>> A getApplication() {
return (A) application;
}
public Environment getEnvironment() {
return environment;
}
protected Application<C> newApplication() {
try {
return applicationClass.newInstance();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private void startIfRequired() {
if (jettyServer != null) {
return;
}
try {
application = newApplication();
final Bootstrap<C> bootstrap =
new Bootstrap<C>(application) {
/** {@inheritDoc} */
public void run(C configuration, Environment environment) throws Exception {
environment.lifecycle().addServerLifecycleListener(new ServerLifecycleListener() {
/** {@inheritDoc} */
public void serverStarted(Server server) {
jettyServer = server;
}
});
DropwizardTestController.this.configuration = configuration;
DropwizardTestController.this.environment = environment;
super.run(configuration, environment);
}
};
application.initialize(bootstrap);
final ServerCommand<C> command = new ServerCommand<>(application);
ImmutableMap.Builder<String, Object> file = ImmutableMap.builder();
if (!Strings.isNullOrEmpty(configPath)) {
file.put("file", configPath);
}
final Namespace namespace = new Namespace(file.build());
command.run(bootstrap, namespace);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}