3

我们想使用 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)

请问有人知道如何避免这个例外吗?如果需要,我可以添加更多细节。

4

3 回答 3

1

这通常是由正在使用的不受支持的焊接版本引起的。但是,我刚刚发布了 CDI-Unit 2.2.1,它修复了 Gradle 构建的几个问题。请问你可以试一试吗?

于 2013-11-08T11:09:17.743 回答
1

我遇到了类似的问题(错误消息有点不同)。我在这个问题上搜索了很多,但没有找到太多关于这个的东西,所以我得出结论,它是特定于我的项目的。这让我怀疑项目的依赖项之间的版本不匹配。

weld-servlet: 2.4.0.Final用作compile依赖项和cdi-unit: 3.1.4依赖test项。我注意到它cdi-unit依赖于weld-se-core: 2.3.2.Final. 所以我weld-se-core用与我的工件相同的版本添加到我的依赖管理中weld-servlet,它解决了这个问题。

为了进一步参考,这是我的pom.xml文件的摘录

<properties>
    <weld.version>2.4.0.Final</weld.version>
    <cdi-unit.version>3.1.4</cdi-unit.version>
    (...)
</properties>

<dependencyManagement>
    <dependencies>
        <!-- CDI -->
        <dependency>
            <groupId>org.jboss.weld.servlet</groupId>
            <artifactId>weld-servlet</artifactId>
            <version>${weld.version}</version>
        </dependency>
        <!-- Tests -->
        <dependency>
            <groupId>org.jglue.cdi-unit</groupId>
            <artifactId>cdi-unit</artifactId>
            <version>${cdi-unit.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.jboss.weld.se</groupId>
            <artifactId>weld-se-core</artifactId>
            <version>${weld.version}</version>
            <scope>test</scope>
        </dependency>
        (...)
    </dependencies>
</dependencyManagement>

<dependencies>
    <!-- CDI -->
    <dependency>
        <groupId>org.jboss.weld.servlet</groupId>
        <artifactId>weld-servlet</artifactId>
    </dependency>
    <!-- Tests -->
    <dependency>
        <groupId>org.jglue.cdi-unit</groupId>
        <artifactId>cdi-unit</artifactId>
    </dependency>
    (...)
</dependencies>
于 2016-10-21T08:57:15.147 回答
0

似乎您正在调用没有参数的接口上的方法。而不是 RateType rateType = rateTypeFactory.getInstance(); 你应该打电话 RateType rateType = rateTypeFactory.getInstance("name", "description");

也许我错过了一些与 gradle 相关的东西?

于 2015-10-22T14:20:55.317 回答