我们想使用 Jglue 框架对我们的 CDI 应用程序进行单元测试。我们使用 gradle 进行构建。
我们准备了这样的东西:
1) 测试类:
@Default
public class RateTypeHibernateFactory implements RateTypeFactory {
@Override
public RateType getInstance(String name, String description) {
RateType rateType = getInstance();
rateType.setName(name);
rateType.setDescription(description);
return rateType;
}
}
2)接口:
public interface RateTypeFactory {
public RateType getInstance(String name, String description);
}
3)Gradle设置(我们跟着入门http://jglue.org/cdi-unit/)
dependencies {
testCompile (group: 'junit', name: 'junit', version: '4.8.2')
testCompile (group: 'org.jglue.cdi-unit', name: 'cdi-unit', version: '2.2.0')
}
4) 测试类:
@RunWith(CdiRunner.class)
public class RateTypeFactoryTest {
@Inject RateTypeFactory rateTypeFactory;
@Test
public void testGetInstance() {
RateType rateType = rateTypeFactory.getInstance();
assertNotNull(rateType);
}
}
当我们运行这个简单的测试时,我们总是会得到这个异常:
com.etnetera.projects.ticketing.model.factory.RateTypeFactoryTest > testGetInstance FAILED
java.lang.NoSuchMethodError
1 test completed, 1 failed
:test FAILED
在 /build/reports/tests/index.html 中有:
java.lang.NoSuchMethodError: org.jboss.weld.metadata.BeansXmlImpl.<init>(Ljava/util/List;Ljava/util/List;Ljava/util/List;Ljava/util/List;Lorg/jboss/weld/bootstrap/spi/Scanning;Ljava/net/URL;Lorg/jboss/weld/bootstrap/spi/BeanDiscoveryMode;Ljava/lang/String;)V
at org.jglue.cdiunit.internal.WeldTestUrlDeployment.<init>(WeldTestUrlDeployment.java:80)
at org.jglue.cdiunit.CdiRunner$1.createDeployment(CdiRunner.java:71)
at org.jboss.weld.environment.se.Weld.initialize(Weld.java:137)
at org.jglue.cdiunit.CdiRunner.createTest(CdiRunner.java:82)
at org.junit.runners.BlockJUnit4ClassRunner$1.runReflectiveCall(BlockJUnit4ClassRunner.java:258)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:255)
at org.jglue.cdiunit.CdiRunner.methodBlock(CdiRunner.java:113)
at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.runTestClass(JUnitTestClassExecuter.java:55)
at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.execute(JUnitTestClassExecuter.java:42)
at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassProcessor.processTestClass(JUnitTestClassProcessor.java:71)
at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:49)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
at org.gradle.messaging.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:32)
at org.gradle.messaging.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:93)
at $Proxy2.processTestClass(Unknown Source)
at org.gradle.api.internal.tasks.testing.worker.TestWorker.processTestClass(TestWorker.java:103)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
at org.gradle.messaging.remote.internal.hub.MessageHub$Handler.run(MessageHub.java:355)
at org.gradle.internal.concurrent.DefaultExecutorFactory$StoppableExecutorImpl$1.run(DefaultExecutorFactory.java:66)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662)
请问有人知道如何避免这个例外吗?如果需要,我可以添加更多细节。