使用Spring运行Arquillian测试时出现NoSuchMethodError

时间:2019-05-02 13:37:16

标签: java spring jboss-arquillian

我有一个项目正在春季5(版本5.1.6.RELEASE)中工作,我想与Arquillian一起编写测试。我过去曾将Arquillian与JavaEE(EJB)一起使用,这是我第一次将其与Spring一起使用。

我找到了这个存储库https://github.com/arquillian/arquillian-extension-spring,并添加了相关的依赖项。

我在pom.xml中具有的所有Arquillian相关依赖项如下:

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.8.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-core</artifactId>
            <version>2.21.0</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.jboss.arquillian.extension</groupId>
            <artifactId>arquillian-service-integration-spring</artifactId>
            <version>${arquillian.spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.arquillian.extension</groupId>
            <artifactId>arquillian-service-integration-spring-inject</artifactId>
            <version>${arquillian.spring.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.jboss.arquillian.extension</groupId>
            <artifactId>arquillian-service-integration-spring-javaconfig</artifactId>
            <version>${arquillian.spring.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.jboss.shrinkwrap.resolver</groupId>
            <artifactId>shrinkwrap-resolver-impl-maven</artifactId>
            <version>3.1.3</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.jboss.arquillian.junit</groupId>
            <artifactId>arquillian-junit-container</artifactId>
            <version>1.4.1.Final</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.wildfly.arquillian</groupId>
            <artifactId>wildfly-arquillian-container-managed</artifactId>
            <version>2.1.1.Final</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.wildfly.arquillian</groupId>
            <artifactId>wildfly-arquillian-container-domain-managed</artifactId>
            <version>2.1.1.Final</version>
            <scope>test</scope>
        </dependency>

arquillian.xml如下(在属性值中进行了必要的修改):

<?xml version="1.0"?>
<arquillian xmlns="http://jboss.org/schema/arquillian" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="
        http://jboss.org/schema/arquillian
        http://jboss.org/schema/arquillian/arquillian_1_0.xsd">

    <!-- Sets the protocol which is how Arquillian talks and executes the tests inside the container -->
    <defaultProtocol type="Servlet 3.0"/>

    <container qualifier="wildfly-managed" default="true">
        <configuration>
            <property name="managementAddress">IP</property>
            <property name="managementPort">port</property>
            <property name="username">username</property>
            <property name="wildflyHome">path/to/wildfly</property>
            <property name="password">password</property>
        </configuration>
    </container>
    <extension qualifier="spring">
        <property name="customContextClass">org.springframework.context.support.ClassPathXmlApplicationContext
        </property>

        <property name="customAnnotationContextClass">
            org.springframework.context.annotation.AnnotationConfigApplicationContext
        </property>
    </extension>
</arquillian>

我创建了以下类以创建必需的jar:

import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.jboss.shrinkwrap.resolver.api.maven.Maven;

public class MyDeployment {

    public static JavaArchive getJar() {
        JavaArchive jar = ShrinkWrap.create(JavaArchive.class).addPackages(true, "package.name");
        JavaArchive[] dependencies = Maven.configureResolver().workOffline().loadPomFromFile("pom.xml").
                importRuntimeDependencies().resolve().withTransitivity().as(JavaArchive.class);

        for(JavaArchive lib: dependencies) {
            jar = jar.merge(lib);
        }
        return jar;
    }
}

我添加了一个演示测试,仅用于按以下方式运行Arquillian:

import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.arquillian.spring.integration.test.annotation.SpringAnnotationConfiguration;
import org.jboss.shrinkwrap.api.Archive;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;

@RunWith(Arquillian.class)
//@SpringAnnotationConfiguration(classes = {BackendAppConfig.class, DatabaseConfig.class})
public class ArquillianExampleTest {

    @Deployment
    public static Archive<?> createTestArchive() {
        return MyDeployment.getJar();
    }

    @Test
    public void testAddNewInteraction() {
        String hello = "Hello";
        Assert.assertEquals("Hello", hello);
    }

}

但是我得到以下错误:

Exception in thread "main" java.lang.reflect.InvocationTargetException
    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 com.intellij.rt.execution.CommandLineWrapper.main(CommandLineWrapper.java:66)
Caused by: java.lang.NoSuchMethodError: org.junit.runners.model.TestClass.getAnnotatedFields()Ljava/util/List;
    at org.jboss.arquillian.junit.State.hasAnyArquillianRule(State.java:93)
    at org.jboss.arquillian.junit.Arquillian.run(Arquillian.java:84)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
    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)
    ... 5 more

您能帮我解决这个错误吗?为什么会出现NoSuchMethodError,因为我已经在JavaArchive中添加了pom依赖项(在上面提供的MyDeployment.java类中)?

0 个答案:

没有答案