Comments (6)
👍
from dropwizard-guice.
👍
from dropwizard-guice.
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.
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:
- https://github.com/HubSpot/dropwizard-guice/blob/master/src/test/java/com/hubspot/dropwizard/guice/objects/TestApplication.java
- https://github.com/HubSpot/dropwizard-guice/blob/master/src/test/java/com/hubspot/dropwizard/guice/InjectedIntegrationTest.java
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.
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.
@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)
- Can not register jersey client in guice
- Binding annotations on providers don't seem to work HOT 1
- AutoConfig should ignore anonymous class HOT 1
- Dropwizard Not Starting Due to HK2 Failure HOT 4
- dropwizrd-guice not working with Dropwizard-core 1.1.0
- LinkageError ... attempted duplicate class definition for name: "org/glassfish/hk2/api/ServiceLocatorState"
- jersey2-guice is defunct HOT 1
- DropWizard 0.9.1 support? HOT 10
- Are there plans to add DropWizard 1.0.0 support? HOT 5
- No License inside Jar file HOT 4
- Getting a warning at startup: "It appears jersey2-guice-spi is either not present or in conflict with some other Jar" HOT 14
- com.google.inject.OutOfScopeException on inject javax.servlet.http.HttpServletRequest HOT 11
- Binding annotations aren't being consistently honored HOT 3
- Integration Test using mocked beans HOT 1
- @BeanParam + @QueryParam don't work with Dropwizard-guice HOT 6
- when i add a filter to jersey , then Jersey error HOT 5
- Compatiblity with Dropwizard 1.0? HOT 27
- AutoConfig not honouring Guice Singleton annotation on resource classes HOT 1
- How exactly HelloWorldConfiguration and Environment get injected in the example HOT 1
- Deprecated API WARNing in v1.0.0.2 HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from dropwizard-guice.