I am trying to code a unit test for my Dao class in a dropwizard application.
The initialize method of the application's main class is as follows.
public void initialize(Bootstrap<ContentAnalysisConfiguration> bootstrap) {
GuiceBundle.Builder<ContentAnalysisConfiguration> guiceBundleBuilder = GuiceBundle.newBuilder();
guiceBundle = guiceBundleBuilder
.addModule(new ContentAnalysisModule())
.setConfigClass(ContentAnalysisConfiguration.class)
.enableAutoConfig(getClass().getPackage().getName())
.build();
bootstrap.addBundle(guiceBundle);
}
The module
class for Guice is as follows:
public class ContentAnalysisModule extends AbstractModule {
private DBI jdbi;
@Provides
public DBI prepareJdbi(Environment environment, ContentAnalysisConfiguration configuration) throws ClassNotFoundException{
// setup DB access including DAOs
// implementing a singleton pattern here but avoiding
// Guice to initialize DB connection too early
if (jdbi == null) {
final DBIFactory factory = new DBIFactory();
jdbi = factory.build(environment, configuration.getDatabase(), "mysql");
}
return jdbi;
}
@Provides
public AdRequestDao prepareAdRequestDao(DBI jdbi) {
return jdbi.onDemand(AdRequestDao.class);
}
@Override
protected void configure() {
bind(Crawler.class).to(JsoupCrawler.class);
bind(Detector.class).to(DictionaryBasedDetector.class);
}
}
The Dao
class is as follows:
public interface AdRequestDao {
@Mapper(AdRequestMapper.class)
@SqlQuery("select * from ref_requests where DATE(updated_at) = :date order by updated_at")
public List<AdRequest> getRefUrlByDate(@Bind("date") String date);
void close();
}
However when I try to create a Guice Injector from my test class I am getting the following error: com.google.inject.CreationException: Guice creation errors:
1) Could not find a suitable constructor in io.dropwizard.setup.Environment. Classes must have either one (and only one) constructor annotated with @Inject or a zero-argument constructor that is not private.
at io.dropwizard.setup.Environment.class(Environment.java:53)
at com.flipkart.flap.modules.ContentAnalysisModule.prepareJdbi(ContentAnalysisModule.java:38)
1 error
at com.google.inject.internal.Errors.throwCreationExceptionIfErrorsExist(Errors.java:435)
at com.google.inject.internal.InternalInjectorCreator.initializeStatically(InternalInjectorCreator.java:154)
at com.google.inject.internal.InternalInjectorCreator.build(InternalInjectorCreator.java:106)
at com.google.inject.Guice.createInjector(Guice.java:95)
at com.google.inject.Guice.createInjector(Guice.java:72)
at com.google.inject.Guice.createInjector(Guice.java:62)
at com.flipkart.flap.dao.AdRequestDaoTest.createdao(AdRequestDaoTest.java:23)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:78)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
The code for the test class is as follows:
public class AdRequestDaoTest {
private AdRequestDao adRequestDao;
@Before
public void createdao () {
System.out.println("At Before");
Injector myInjector = Guice.createInjector(new ContentAnalysisModule());
this.adRequestDao = myInjector.getInstance(AdRequestDao.class);
}
@Test
public void testGetRefUrlByDate() {
//this.adRequestDao = adRequestDao;
String date = Utils.getCurrentDate();
List<AdRequest> requests = adRequestDao.getRefUrlByDate(date);
for(AdRequest request : requests) {
assertEquals(15417220,request.getAffiliateId());
assertEquals("http://popmp3.in/video/list/5367690",request.getRefUrl());
assertEquals("2015-08-12 06:00:30",request.getCreatedAt());
assertEquals("2015-08-17 16:44:20",request.getUpdatedAt());
assertEquals(12,request.getNoOfRequests());
}
}
}
Any help/suggestion is appreciated.
It looks like your test may not be providing an Environment; Guice is complaining that it doesn't know how to create one.
You may need to create a Module for your test that provides Environment and any other dependencies of ContentAnalysisModule.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.