-
Notifications
You must be signed in to change notification settings - Fork 95
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Integration Test using mocked beans #92
Comments
The way I did it at a couple of companies that has worked fairly well is extend 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);
}
}
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 text was updated successfully, but these errors were encountered: