简体   繁体   中英

Unit testing CDI with CDI-Unit Jglue

we want to use Jglue framework for unit testing our CDI application. We use gradle for building.

We have prepared something like this:

1) Class to test:

@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) Interface:

public interface RateTypeFactory {

public RateType getInstance(String name, String description);

}

3) Gradle settings (we followed getting started 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) Test class:

@RunWith(CdiRunner.class)
public class RateTypeFactoryTest {

@Inject RateTypeFactory rateTypeFactory;

@Test
public void testGetInstance() {
    RateType rateType = rateTypeFactory.getInstance();
    assertNotNull(rateType);
}

}

When we run this simple test, we always get this exception:

com.etnetera.projects.ticketing.model.factory.RateTypeFactoryTest > testGetInstance FAILED
java.lang.NoSuchMethodError

1 test completed, 1 failed
:test FAILED

In /build/reports/tests/index.html there is:

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)

Please does anybody know how to avoid this exception? I can add some more details if needed.

This is usually caused by an unsupported version of weld being used. However, I have just released CDI-Unit 2.2.1 which fixes a couple of issues with Gradle builds. Please can you give it a go?

It seems you are invoking a method on the interface without the parameters. Instead of RateType rateType = rateTypeFactory.getInstance(); you should probably call RateType rateType = rateTypeFactory.getInstance("name", "description");

Maybe I missed something gradle-related?

I have encountered a similar problem (with an error message a bit different). I googled a lot around this problem but did not found much about this, so I concluded that it was specific to my project. This lead me to suspect a version mismatch between the dependencies of the project.

I was using weld-servlet: 2.4.0.Final as a compile dependency and cdi-unit: 3.1.4 as a test dependency. And I noticed that cdi-unit had a dependency on weld-se-core: 2.3.2.Final . So I added weld-se-core to my dependency management with the same version as my weld-servlet artifact and it solved the problem.

For further reference, here is an extract of my pom.xml file

<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>

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM