GithubHelp home page GithubHelp logo

Comments (1)

heldeen avatar heldeen commented on August 24, 2024

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

from dropwizard-guice.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.