I have a project at work in Spring 5 (version 5.1.6.RELEASE) and I want to write a test with Arquillian. I have used Arquillian with JavaEE (EJB) in the past and it is the first time that I use it with Spring.
I have found this repository https://github.com/arquillian/arquillian-extension-spring and I have added the relevant dependencies.
All the Arquillian relevant dependencies I have in pom.xml are the below:
<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>
The arquillian.xml is as per below (with the necessary modifications in the property values):
<?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>
I have created the below class in order to create the necessary 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;
}
}
I have added a demo test just to run Arquillian as per below:
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);
}
}
However I get the below error:
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
Could you please help me with this error? Why I get NoSuchMethodError, since I have added the pom dependencies in the JavaArchive (in MyDeployment.java class I provided above)?