
io.dropwizard.testing.junit.DropwizardAppRule Maven / Gradle / Ivy
package io.dropwizard.testing.junit;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.dropwizard.Application;
import io.dropwizard.Configuration;
import io.dropwizard.lifecycle.Managed;
import io.dropwizard.setup.Environment;
import io.dropwizard.testing.ConfigOverride;
import io.dropwizard.testing.DropwizardTestSupport;
import org.junit.rules.ExternalResource;
import javax.annotation.Nullable;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicInteger;
/**
* A JUnit rule for starting and stopping your application at the start and end of a test class.
*
* By default, the {@link Application} will be constructed using reflection to invoke the nullary
* constructor. If your application does not provide a public nullary constructor, you will need to
* override the {@link #newApplication()} method to provide your application instance(s).
*
*
*
* Using DropwizardAppRule at the suite level can speed up test runs, as the application is only started and stopped
* once for the entire suite:
*
*
*
* @RunWith(Suite.class)
* @SuiteClasses({FooTest.class, BarTest.class})
* public class MySuite {
* @ClassRule
* public static final DropwizardAppRule<MyConfig> DROPWIZARD = new DropwizardAppRule<>(...);
* }
*
*
*
* If the same instance of DropwizardAppRule is reused at the suite- and class-level, then the application will be
* started and stopped once, regardless of whether the entire suite or a single test is executed.
*
*
*
* public class FooTest {
* @ClassRule public static final DropwizardAppRule<MyConfig> DROPWIZARD = MySuite.DROPWIZARD;
*
* public void testFoo() { ... }
* }
*
* public class BarTest {
* @ClassRule public static final DropwizardAppRule<MyConfig> DROPWIZARD = MySuite.DROPWIZARD;
*
* public void testBar() { ... }
* }
*
*
*
*
*
*
* @param the configuration type
*/
public class DropwizardAppRule extends ExternalResource {
private final DropwizardTestSupport testSupport;
private final AtomicInteger recursiveCallCount = new AtomicInteger(0);
public DropwizardAppRule(Class extends Application> applicationClass) {
this(applicationClass, (String) null);
}
public DropwizardAppRule(Class extends Application> applicationClass,
@Nullable String configPath,
ConfigOverride... configOverrides) {
this(applicationClass, configPath, Optional.empty(), configOverrides);
}
public DropwizardAppRule(Class extends Application> applicationClass, String configPath,
Optional customPropertyPrefix, ConfigOverride... configOverrides) {
this(new DropwizardTestSupport<>(applicationClass, configPath, customPropertyPrefix,
configOverrides));
}
/**
* Alternate constructor that allows specifying exact Configuration object to
* use, instead of reading a resource and binding it as Configuration object.
*
* @since 0.9
*/
public DropwizardAppRule(Class extends Application> applicationClass,
C configuration) {
this(new DropwizardTestSupport<>(applicationClass, configuration));
}
public DropwizardAppRule(DropwizardTestSupport testSupport) {
this.testSupport = testSupport;
}
public DropwizardAppRule addListener(final ServiceListener listener) {
this.testSupport.addListener(new DropwizardTestSupport.ServiceListener() {
@Override
public void onRun(C configuration, Environment environment, DropwizardTestSupport rule) throws Exception {
listener.onRun(configuration, environment, DropwizardAppRule.this);
}
@Override
public void onStop(DropwizardTestSupport rule) throws Exception {
listener.onStop(DropwizardAppRule.this);
}
});
return this;
}
public DropwizardAppRule manage(final Managed managed) {
return addListener(new ServiceListener() {
@Override
public void onRun(C configuration, Environment environment, DropwizardAppRule rule) throws Exception {
environment.lifecycle().manage(managed);
}
});
}
@Override
protected void before() {
if (recursiveCallCount.getAndIncrement() == 0) {
testSupport.before();
}
}
@Override
protected void after() {
if (recursiveCallCount.decrementAndGet() == 0) {
testSupport.after();
}
}
public C getConfiguration() {
return testSupport.getConfiguration();
}
public int getLocalPort() {
return testSupport.getLocalPort();
}
public int getPort(int connectorIndex) {
return testSupport.getPort(connectorIndex);
}
public int getAdminPort() {
return testSupport.getAdminPort();
}
public Application newApplication() {
return testSupport.newApplication();
}
public > A getApplication() {
return testSupport.getApplication();
}
public Environment getEnvironment() {
return testSupport.getEnvironment();
}
public ObjectMapper getObjectMapper() {
return testSupport.getObjectMapper();
}
public abstract static class ServiceListener {
public void onRun(T configuration, Environment environment, DropwizardAppRule rule) throws Exception {
// Default NOP
}
public void onStop(DropwizardAppRule rule) throws Exception {
// Default NOP
}
}
public DropwizardTestSupport getTestSupport() {
return testSupport;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy