我正在尝试在dropwizard应用程序中为我的Dao类编写单元测试。
应用程序主类的初始化方法如下。
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);
}
Guice的module
课程如下:
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);
}
}
Dao
类如下:
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();
}
然而,当我尝试从我的测试类创建一个Guice Injector时,我收到以下错误: com.google.inject.CreationException:Guice创建错误:
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)
测试类的代码如下:
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());
}
}
}
感谢任何帮助/建议。
答案 0 :(得分:2)
看起来您的测试可能没有提供环境; Guice抱怨它不知道如何创建一个。
您可能需要为测试创建一个Module,它提供Environment和ContentAnalysisModule的任何其他依赖项。