我正在尝试在我的Web项目中设置Arquillian,它使用Jboss Seam 2,Hibernate / JPa等。我更新了我的pom.xml,插入了以下代码:
<dependencyManagement>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.jboss.arquillian/arquillian-bom -->
<dependency>
<groupId>org.jboss.arquillian</groupId>
<artifactId>arquillian-bom</artifactId>
<version>1.1.13.Final</version>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
...
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
<version>7.0.42</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<version>7.0.42</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-logging-juli</artifactId>
<version>7.0.42</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jdt.core.compiler</groupId>
<artifactId>ecj</artifactId>
<version>3.7</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.container</groupId>
<artifactId>arquillian-tomcat-embedded-7</artifactId>
<version>1.0.0</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.jboss.shrinkwrap.resolver/shrinkwrap-resolver-impl-maven -->
<dependency>
<groupId>org.jboss.shrinkwrap.resolver</groupId>
<artifactId>shrinkwrap-resolver-impl-maven</artifactId>
<version>2.2.6</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.jboss.arquillian.junit/arquillian-junit-container -->
<dependency>
<groupId>org.jboss.arquillian.junit</groupId>
<artifactId>arquillian-junit-container</artifactId>
<version>1.1.13.Final</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
所以,在我的pom.xml中添加依赖项之后的第二步创建了一个Arquillian Test类,请参阅:
package com.projectx;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.seam.annotations.In;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.Test;
import org.junit.runner.RunWith;
import com.projectx.MyService;
@RunWith(Arquillian.class)
public class MyServiceTest {
@Deployment
public static JavaArchive createTestArchive() {
JavaArchive jar = ShrinkWrap.create(JavaArchive.class).addPackages(true, "com.projectx");
return jar;
}
@In
private MyService myService;
@Test
public void validateValue() {
ReturnBean returnBean = myService.validateNow(null);
}
}
最后我尝试运行我的Test类,我注意到“myService”对象为null,Inject Point不起作用,所以我得到了NullPointerException。
我的镜头是我没有使用ShrinkWrap在JavaArchive中插入所有必需的东西。反正是否像maven一样创造了我的arquillian战争?
修改1 : 我在pom.xml中添加了以下依赖项:
<dependency>
<groupId>org.jboss.arquillian.extension</groupId>
<artifactId>arquillian-seam2</artifactId>
<version>1.0.0.Beta1</version>
</dependency>
并且更改了此依赖项的版本,因为与arquillian-seam2的兼容性:
<dependency>
<groupId>org.jboss.arquillian.junit</groupId>
<artifactId>arquillian-junit-container</artifactId>
<version>1.1.1.Final</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.arquillian</groupId>
<artifactId>arquillian-bom</artifactId>
<version>1.1.1.Final</version>
<type>pom</type>
</dependency>
但是当我尝试开始测试时,我收到了以下错误:
GRAVE: Exception sending context initialized event to listener instance of class org.jboss.seam.servlet.SeamListener
java.lang.IllegalStateException: duplicate factory for: org.jboss.seam.web.webSession (duplicate is specified in components.xml)
at org.jboss.seam.core.Init.checkDuplicateFactoryExpressions(Init.java:227)
at org.jboss.seam.core.Init.checkDuplicateFactory(Init.java:220)
at org.jboss.seam.core.Init.addFactoryValueExpression(Init.java:283)
at org.jboss.seam.init.Initialization.installComponents(Initialization.java:1152)
at org.jboss.seam.init.Initialization.init(Initialization.java:737)
at org.jboss.seam.servlet.SeamListener.contextInitialized(SeamListener.java:36)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4939)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5434)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:633)
at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:976)
at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1653)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:441)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
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)
解决方案: 我将容器从apache-tomcat更改为jboss-as-5.1。
答案 0 :(得分:1)
您必须使用Seam 2扩展程序在测试工作中@In
进行注入 - https://github.com/arquillian/arquillian-extension-seam2。请记住,由于Seam 2不再处于活跃开发状态,因此扩展本身也不是。