GithubHelp home page GithubHelp logo

Request: Testing Example about dropwizard-guice HOT 6 OPEN

hubspot avatar hubspot commented on August 24, 2024
Request: Testing Example

from dropwizard-guice.

Comments (6)

also avatar also commented on August 24, 2024

👍

from dropwizard-guice.

lookfirst avatar lookfirst commented on August 24, 2024

👍

from dropwizard-guice.

scho avatar scho commented on August 24, 2024

My answer might be late, but nonetheless I'd like to share how I have used guice in my integration tests.
First of all I use the DropwizardAppRule. I think that part is pretty straight forward.

My guice modules are MyModule and MyTestModule.

My application class looks like this:

public class MyApplication extends Application<MyConfiguration> {

   private final Module module;

   private GuiceBundle<MyConfiguration> guiceBundle;

   public MyApplication() {
      this(new MyModule());
   }

   public MyApplication(Module module) {
      this.module = module;
   }

   // initialize [...]

   protected GuiceBundle<MyConfiguration> getGuiceBundle() {
      return guiceBundle;
   }
}

My test application class looks like this:

public class MyTestApplication extends MyApplication {

   public MyTestApplication() {
      super(Modules.override(new MyModule()).with(new MyTestModule()));
   }

   public <T> T getInstance(Class<T> type) {
      return getGuiceBundle().getInjector().getInstance(type);
   }
}

In your tests you use the app rule as follows:

@ClassRule
public static final DropwizardAppRule<MyConfiguration> APP_RULE = new DropwizardAppRule<>(MyTestApplication.class, ResourceHelpers.resourceFilePath("configuration-example.json"));

This approach lets you override anything for testing. E.g. you can replace the database with a in memory implementation in the MyTestModule. You can even get any instance created by guice with:

APP_RULE.getApplication().getInstance(SomeService.class);

I hope this helps. If you have any questions, please ask.
I use dropwizard 0.8.0.
If people are interested in putting this into the documentation, I can update the wiki/send a PR.

from dropwizard-guice.

mrserverless avatar mrserverless commented on August 24, 2024

Personally I prefer to use Constructor Injections so that my tests can use Mock objects or just new one up. That way I'm testing my code rather than testing guice.

If you want to do Integration tests, it's exactly what @scho has mentioned. Also you can have a look at the test cases that we've added for 0.8.0 release:

If you want to do Service and Resource tests and inject in your member variables, you can do this pretty standard guice approach for junit:

public testMyGuice {
    @Inject
    Service testService;

    @Before
    public void setUpInjector() {
        injector = Guice.createInjector(MyTestModule());
        injector.injectMembers(this);
    }

    @Test
    public myServiceShouldInject() {
        assertThat(testService).isNotNull()
    }
}

from dropwizard-guice.

patricioe avatar patricioe commented on August 24, 2024

In this case, do you have the need to pass the test environment information (perhaps represented by TestConfiguration) to your MyTestModule() at all?

from dropwizard-guice.

mrserverless avatar mrserverless commented on August 24, 2024

@patricioe it depends on if you need Environment or TestConfiguration (they are different things btw, TestConfiguration has nothing to do with Environment).

No, you don't need to pass it into MyTestModule(). If you need to inject in Environment or TestConfiguration, all you need to do is:

injector = Guice.createInjector(
    new DropwizardEnvironmentModule<>(TestConfiguration.class), 
    MyTestModule()
)

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.