Skip to content
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

Open
sandeep-chekuri opened this issue Sep 13, 2016 · 1 comment
Open

Integration Test using mocked beans #92

sandeep-chekuri opened this issue Sep 13, 2016 · 1 comment

Comments

@sandeep-chekuri
Copy link

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..

@heldeen
Copy link

heldeen commented Oct 5, 2016

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);
        }
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants