Arquillian,Tomcat 和 Jboss Seam 2
Arquillian, Tomcat and Jboss Seam 2
我正在尝试在使用 Jboss Seam 2、Hibernate/JPa 和其他项目的 Web 项目中设置 Arquillian。我更新了我的 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 测试 class,请参阅:
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);
}
}
最后我尝试了 运行 我的测试 class 并且我注意到 "myService" 对象是空的,注入点没有工作,所以我得到了 NullPointerException。
我的观点是,我没有使用 ShrinkWrap 在 JavaArchive 中插入所有必要的东西。无论如何,在 arquillian 中创建我的 war 与 maven create 完全一样吗?
编辑 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.
你必须使用 Seam 2 扩展来使注入通过 @In
在测试工作中 - https://github.com/arquillian/arquillian-extension-seam2。请记住,由于 Seam 2 不再处于活跃开发状态,因此扩展本身也不是。
我正在尝试在使用 Jboss Seam 2、Hibernate/JPa 和其他项目的 Web 项目中设置 Arquillian。我更新了我的 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 测试 class,请参阅:
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);
}
}
最后我尝试了 运行 我的测试 class 并且我注意到 "myService" 对象是空的,注入点没有工作,所以我得到了 NullPointerException。
我的观点是,我没有使用 ShrinkWrap 在 JavaArchive 中插入所有必要的东西。无论如何,在 arquillian 中创建我的 war 与 maven create 完全一样吗?
编辑 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.
你必须使用 Seam 2 扩展来使注入通过 @In
在测试工作中 - https://github.com/arquillian/arquillian-extension-seam2。请记住,由于 Seam 2 不再处于活跃开发状态,因此扩展本身也不是。