Integration Test using mocked beans
Opened this issue · 1 comments
We have a following scenario,
OurServiceApplication is adding the below bundle during the initialize(Bootstrap...)
GuiceBundle guiceBundle = GuiceBundle.newBuilder()
.addModule(new OurModule())...
OurModule has the bean injection of DatabaseSource etc.
We are writing the integration test as below
DropwizardAppRule RULE = new DropwizardAppRule<>(
OurServiceApplication.class, "our.yml")
Now we want to inject in-memory datasource, can u pls let us know how to inject it?
Please note that TestModule has the InMemoryDatasource beans..
The way I did it at a couple of companies that has worked fairly well is extend io.dropwizard.testing.DropwizardTestSupport
so you can override com.alianza.dropwizard.testing.AlzDropwizardTestSupport#newApplication
. The new DropwizardTestSupport
should take your TestModule
and use that and your Dropwizard Application to spin up the server. Here's a couple of snippets:
public class Main extends Application<BasicExampleConfig> {
private final Module appModule;
public Main() {
appModule = new BasicExampleModule();//Guice module used for normal runtime
}
@VisibleForTesting
public Main(Module... overrideModules) {
appModule = Modules.override(new BasicExampleModule()).with(overrideModules);
}
public static void main(String... args) throws Exception {
new Main().run(args);
}
@Override
public String getName() {
return "basic-example";
}
@Override
public void initialize(final Bootstrap<BasicExampleConfig> bootstrap) {
super.initialize(bootstrap);
GuiceBundle<BasicExampleConfig> bundle = GuiceBundle.newBuilder()
.addModule(appModule)
//other GuiceBundle builder options
.build();
bootstrap.addBundle(bundle);
}
//other methods omitted
}
public class YourDropwizardTestSupport<C extends Configuration> extends DropwizardTestSupport {
private final Module externalMocksModule;
public YourDropwizardTestSupport(Class<? extends Application<C>> applicationClass, @Nullable String configPath, Module externalMocksModule) {
super(applicationClass, configPath);
this.externalMocksModule = requireNonNull(externalMocksModule);
}
@Override
public Application<C> newApplication() {
try {
//this is way fancier than you need but it finds the constructor in the Application class that takes a Guice Module and uses that to spin up your server for integratoin tests
return (Application<C>) applicationClass.getConstructor(Module[].class).newInstance(new Object[]{new Module[] {externalMocksModule}});
} catch (Exception e) {
throw propagate(e);
}
}
}