我正在尝试使用arquillian和wildfly-14.0.1.Final作为托管容器进行集成测试。一切正常,容器正在启动,存档已部署。不幸的是,Test-Class中的注入无法正常工作。在测试类中,每个注入点均为空。
我已经尝试过在单例会话bean(@Singleton @Startup
)上的容器中进行注入,并且一切正常。因此,我认为像bean.xml这样的常见嫌疑犯不是问题。问题开始于通过arquillian运行的测试类。
我应该明确指出的一件事是,我使用JUnit 5运行所有测试。对于Arquillian部分,我使用junit-vintage-engine。
这里是我的设置的简要概述,并以一个最小的示例尝试做些什么。我将保留一些测试不需要的依赖项,以便我们可以识别类路径上的一些错误。
我希望有人可以将我推向正确的方向,因为我现在不知道怎么了。
感谢@ll
pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<arquillian-version>1.4.1.Final</arquillian-version>
<junit-vintage-version>4.12.0</junit-vintage-version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>26.0-jre</version>
</dependency>
<!-- jee -->
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.6</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.8.7</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.8.7</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.7</version>
</dependency>
<!-- test -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.2.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>${junit-vintage-version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.2.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>${mockito-version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>${mockito-version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-reflect</artifactId>
<version>1.7.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.arquillian</groupId>
<artifactId>arquillian-bom</artifactId>
<version>${arquillian-version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.jboss.shrinkwrap.resolver</groupId>
<artifactId>shrinkwrap-resolver-bom</artifactId>
<version>3.1.3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.jboss.shrinkwrap.resolver</groupId>
<artifactId>shrinkwrap-resolver-depchain</artifactId>
<type>pom</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.wildfly</groupId>
<artifactId>wildfly-arquillian-container-managed</artifactId>
<version>8.2.1.Final</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.junit</groupId>
<artifactId>arquillian-junit-container</artifactId>
<version>${arquillian-version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.protocol</groupId>
<artifactId>arquillian-protocol-servlet</artifactId>
<version>${arquillian-version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-common</artifactId>
<version>2.27</version>
<scope>test</scope>
</dependency>
<!-- end testing -->
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<groupId>org.sonarsource.scanner.maven</groupId>
<artifactId>sonar-maven-plugin</artifactId>
<!-- Version 3.3 has the error 'Unable to execute SonarQube: Fail to get bootstrap index from server'
with SonarQube 5.6.7 -->
<version>3.2</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.22.0</version>
<configuration>
<reportsDirectory>${project.build.directory}/surefire-reports</reportsDirectory>
</configuration>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.2</version>
<executions>
<execution>
<id>default-prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>default-report</id>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
<profiles>
<profile>
<id>enable-jacoco</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>@{argLine} -Dfile.encoding=${project.build.sourceEncoding}</argLine>
</configuration>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>arq-wildfly-managed</id>
<dependencies>
<dependency>
<groupId>org.wildfly</groupId>
<artifactId>wildfly-arquillian-container-managed</artifactId>
<version>8.2.1.Final</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<!-- Fork every test because it will launch a separate AS instance -->
<forkMode>always</forkMode>
<systemPropertyVariables>
<!--<arquillian.launch>wildfly-managed</arquillian.launch>-->
<java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager>
<persistenceUnit>UNIT_NAME</persistenceUnit>
<dataSource>java:jboss/ds/testingDS</dataSource>
<jboss.home>${user.dir}\project\test\wildfly-14.0.1.Final\</jboss.home>
</systemPropertyVariables>
<includes>
<include>%regex[.*DatabaseBaseTest.*]</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
最低测试等级:
import javax.inject.Inject;
/**
* @author David Witte (msg DAVID GmbH)
* @since 17.08.2018 16:51
*/
@RunWith (Arquillian.class)
public class DatabaseBaseTest {
private static final Logger LOGGER = LoggerFactory.getLogger(DatabaseBaseTest.class);
@Deployment (testable = false)
public static WebArchive createDeployment() {
return ShrinkWrap
.create(WebArchive.class)
.addAsManifestResource("beans.xml", "beans.xml")
.addAsWebInfResource("web.xml", "web.xml ")
.addClass(SimpleBeanTest.class)
.addClass(SimpleSingleton.class)
.addClass(TestApplication.class)
.addClass(TestRessource.class);
}
@Inject
public SimpleBeanTest simpleBeanTest;
//This test runs fine!
@Test
@RunAsClient
public void testRessource(@ArquillianResource URL baseURL) {
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(baseURL.toString() + "test/ressource");
LOGGER.info("Ressource URI is: " + baseURL.toString() + "test/ressource");
try {
CloseableHttpResponse response = httpclient.execute(httpGet);
//Works! Test does not fail!
Assert.assertEquals(200, response.getStatusLine().getStatusCode());
}
catch (IOException e) {
LOGGER.error("could not execute request", e);
}
}
//This test fails with NPE!
@Test
public void firstTestWithArquillian() throws Exception {
//Here NPE!
simpleBeanTest.doTestMessage();
Assert.assertNotNull(simpleBeanTest);
}
}
SimpleBeanTest.java:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.enterprise.context.RequestScoped;
import javax.inject.Named;
/**
* TODO: Describe this file!
*
* @author David Witte (msg DAVID GmbH)
* @since 01.12.2018 17:06
*/
@Named
@RequestScoped
public class SimpleBeanTest {
private static final Logger LOGGER = LoggerFactory.getLogger(SimpleBeanTest.class);
@PostConstruct
public void init() {
LOGGER.info("Here I am! I'm running in the Container!");
}
public void doTestMessage() {
LOGGER.info("Hello world!");
}
@PreDestroy
public void destruct() {
LOGGER.info("Bye, I'm shutting down now!");
}
}
SimpleSingleton.java:
import javax.annotation.PostConstruct;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import javax.inject.Inject;
/**
* TODO: Describe this file!
*
* @author David Witte (msg DAVID GmbH)
* @since 02.12.2018 04:26
*/
@Singleton
@Startup
public class SimpleSingleton {
//Injection works fine here!
@Inject
SimpleBeanTest simpleBeanTest;
@PostConstruct
public void init() {
//No problem after bean creation injection works!
simpleBeanTest.doTestMessage();
}
}
TestRessource.java:
import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;
/**
* TODO: Describe this file!
*
* @author David Witte (msg DAVID GmbH)
* @since 02.12.2018 16:29
*/
@Path ("ressource")
public class TestRessource {
//Works fine here!
@Inject
SimpleBeanTest simpleBeanTest;
@GET
public Response doGet() {
simpleBeanTest.doTestMessage();
if (null != simpleBeanTest) {
return Response.ok().build();
}
else {
return Response.serverError().build();
}
}
}
答案 0 :(得分:0)
我认为问题在于--timeout
以“作为客户端”模式(http://arquillian.org/arquillian-core/#client-mode)运行测试。
您这里需要的是“混合”模式(http://arquillian.org/arquillian-core/#mixed-mode)。
为此,您必须将 testable 设置为csoteam@netconf:~/netconf-console$ netconf-console --host=172.16.2.2 --user=admin --password=admin --port=830 --commit --timeout=600
Operation failed: TimeoutExpiredError - ncclient timed out while waiting for an rpc reply.
###DIDN'T HELP, STILL AFTER 1 MIN IT GOT TIMED OUT, I WAS EXPECTING 10 MINUTES
csoteam@netconf:~/netconf-console$ netconf-console --host=172.16.2.2 --user=admin --password=admin --port=830 --timeout=120 --commit
Operation failed: TimeoutExpiredError - ncclient timed out while waiting for an rpc reply.
###DIDN'T HELP, STILL AFTER 1 MIN IT GOT TIMED OUT, I WAS EXPECTING 2 MINUTES
csoteam@netconf:~/netconf-console$ netconf-console --host=172.16.2.2 --user=admin --password=admin --port=830 --timeout 120 --commit
Operation failed: TimeoutExpiredError - ncclient timed out while waiting for an rpc reply.
###DIDN'T HELP, STILL AFTER 1 MIN IT GOT TIMED OUT, I WAS EXPECTING 2 MINUTES
csoteam@netconf:~/netconf-console$ netconf-console --host=172.16.2.2 --user=admin --password=admin --port=830 --timeout 12 --commit
Operation failed: TimeoutExpiredError - ncclient timed out while waiting for an rpc reply.
###DIDN'T HELP, STILL AFTER 1 MIN IT GOT TIMED OUT, I WAS EXPECTING 12 SECONDS
csoteam@netconf:~/netconf-console$ netconf-console --host=172.16.2.2 --user=admin --pass=admin --port=830 --timeout=600
netconf> edit-config osv6-basic-config.xml --db=candidate
<ok xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0"/>
netconf> commit
Operation failed: TimeoutExpiredError - ncclient timed out while waiting for an rpc reply.
netconf>
###AGAIN DIDN'T HELP
Have you tried this option? What is the correct syntax? How could I change the timeout?
。