我们正在使用dagger 2.0绑定我们应用中的模块。虽然我们使用guice 3.0设置测试,但到目前为止所有工作都正常,直到我在使用的类中添加了新的方法绑定,并且作为参数我使用:
Optional<Set<DataGroup>> dataGroups
我的代码中的类似于:
@Inject
public void setDataGroups(@Named(DATA_GROUPS) Optional<Set<DataGroup>>
dataGroups) {
this.dataGroups = dataGroups;
}
在我使用guice的测试模块中,我用这种方式绑定这些数据组:
Optional<Set<DataGroup>> dataGroups = Optional.of((new HashSet<DataGroup>());
bind(new TypeLiteral<Optional<Set<DataGroup>>>() {
}).toInstance(dataGroups);
它不起作用。通过运行测试我得到:
com.google.inject.internal.util.$ComputationException: java.lang.ArrayIndexOutOfBoundsException
如果我通过提供List接口(当然还有更改dataGroups字段的声明)在代码中实现我的setDataGroups()方法,这很奇怪,而且一切正常:
@Inject
public void setDataGroups(@Named(DATA_GROUPS) Optional<List<DataGroup>>
dataGroups) {
this.dataGroups = dataGroups;
}
和guice中的测试模块中的绑定:
Optional<List<DataGroup>> dataGroups = Optional.of(new ArrayList<DataGroup>());
bind(new TypeLiteral<Optional<List<DataGroup>>>() {
}).toInstance(dataGroups);
所以我想知道绑定Set和List之间是否有什么特别之处,因为我找不到任何线索为什么设置Set接口在guice中不起作用并且打破测试模块
我尝试使用guice 4.0,因为我读到了类似的问题,但是他们触摸lambdas而不是使用HashSet实例绑定Set接口。
提前致谢。
以下全部例外:
com.google.inject.internal.util.$ComputationException: java.lang.ArrayIndexOutOfBoundsException: 75777
at com.google.inject.internal.util.$MapMaker$StrategyImpl.compute(MapMaker.java:553)
at com.google.inject.internal.util.$MapMaker$StrategyImpl.compute(MapMaker.java:419)
at com.google.inject.internal.util.$CustomConcurrentHashMap$ComputingImpl.get(CustomConcurrentHashMap.java:2041)
at com.google.inject.internal.util.$StackTraceElements.forMember(StackTraceElements.java:53)
at com.google.inject.internal.Errors.formatInjectionPoint(Errors.java:716)
at com.google.inject.internal.Errors.formatSource(Errors.java:678)
at com.google.inject.internal.Errors.format(Errors.java:555)
at com.google.inject.CreationException.getMessage(CreationException.java:48)
at java.lang.Throwable.getLocalizedMessage(Throwable.java:391)
at java.lang.Throwable.toString(Throwable.java:480)
at java.lang.Throwable.<init>(Throwable.java:311)
at java.lang.Exception.<init>(Exception.java:102)
at java.lang.RuntimeException.<init>(RuntimeException.java:96)
at org.jboss.arquillian.core.spi.InvocationException.<init>(InvocationException.java:33)
at org.jboss.arquillian.core.impl.ObserverImpl.invoke(ObserverImpl.java:107)
at org.jboss.arquillian.core.impl.EventContextImpl.invokeObservers(EventContextImpl.java:99)
at org.jboss.arquillian.core.impl.EventContextImpl.proceed(EventContextImpl.java:81)
at org.jboss.arquillian.test.impl.TestContextHandler.createSuiteContext(TestContextHandler.java:73)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.jboss.arquillian.core.impl.ObserverImpl.invoke(ObserverImpl.java:94)
at org.jboss.arquillian.core.impl.EventContextImpl.proceed(EventContextImpl.java:88)
at org.jboss.arquillian.test.impl.TestContextHandler.createClassContext(TestContextHandler.java:92)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.jboss.arquillian.core.impl.ObserverImpl.invoke(ObserverImpl.java:94)
at org.jboss.arquillian.core.impl.EventContextImpl.proceed(EventContextImpl.java:88)
at org.jboss.arquillian.core.impl.ManagerImpl.fire(ManagerImpl.java:145)
at org.jboss.arquillian.core.impl.ManagerImpl.fire(ManagerImpl.java:116)
at org.jboss.arquillian.test.impl.EventTestRunnerAdaptor.beforeClass(EventTestRunnerAdaptor.java:87)
at org.jboss.arquillian.junit.Arquillian$2.evaluate(Arquillian.java:201)
at org.jboss.arquillian.junit.Arquillian.multiExecute(Arquillian.java:426)
at org.jboss.arquillian.junit.Arquillian.access$200(Arquillian.java:54)
at org.jboss.arquillian.junit.Arquillian$3.evaluate(Arquillian.java:218)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.jboss.arquillian.junit.Arquillian.run(Arquillian.java:166)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: java.lang.ArrayIndexOutOfBoundsException: 75777
at com.google.inject.internal.asm.$ClassReader.<init>(Unknown Source)
at com.google.inject.internal.asm.$ClassReader.<init>(Unknown Source)
at com.google.inject.internal.asm.$ClassReader.<init>(Unknown Source)
at com.google.inject.internal.util.$LineNumbers.<init>(LineNumbers.java:62)
at com.google.inject.internal.util.$StackTraceElements$1.apply(StackTraceElements.java:36)
at com.google.inject.internal.util.$StackTraceElements$1.apply(StackTraceElements.java:33)
at com.google.inject.internal.util.$MapMaker$StrategyImpl.compute(MapMaker.java:549)
... 44 more
Test ignored.
Disconnected from the target VM, address: '127.0.0.1:54693', transport: 'socket'
Process finished with exit code -1