2012-09-08 3 views
6

CDI와 함께 Tomcat 7을 사용하고 있는데,이를 위해 Knappsack Maven Archetypes의 jee6-servlet-minimal-archetype을 시작점으로 사용했습니다.Tomcat + CDI + Arquillian

이제는 CDI 빈을 테스트하기 위해 Arquillian을 사용하고 싶지만 꽤 오랜 시간 동안 검색 한 후에도 주제와 관련된 많은 문제점을 발견했습니다.

Tomcat 7의 CDI 테스트를 위해 Arquillian을 사용하여 작업 설정 (특히 오른쪽 pom.xml)을 가리킬 수 있습니까?


편집 2012년 9월 11일 : 으로 아래 댓글에서 지적, 내 문제는, 차라리을 해결하기 위해 노력보다, 전체 설정을 이해하고 도와 줄 사람이 필요 해결받을 생각 특정 시점에서 특정 예외가 발생합니다.

그래서 tomcat 7에 CDI 빈을 가지고 있고 임베디드 및 관리 컨테이너의 모든 주입 메커니즘으로 테스트 할 수있는 pom.xml과 테스트 클래스는 어떻게 생겼을까요? (그런데, 왜 6되었습니다 이상으로 톰캣 7에 대한 원격 컨테이너 어댑터가 없다?)

나는 이미 tomcat 6 example에 적응하려고 노력하지만 바람둥이 작업을 만들 수 없습니다 7.

답변

3

불행히도, 나는 아직 내 문제에 대한 실제 사례를 발견하거나 이야기하지는 못했지만, 나를 위해 일한 뭔가를 생각해 낼 수 있었다. 나는 여기서 보여주고 싶다. 이 문제는 이국적인 문제가 아니라고 생각하고 누군가가 그것을 살펴보고 뭔가 다른 점이있을 때를 대비하여 나에게 힌트를 줄 수 있습니다.

/pom.xml (사용 가능한 바람둥이 7 [7.0.30]와 일식 [3.7]에서 프로젝트 속성/배포 어셈블리 테스트 클래스/리소스가 포함되어 있지 않습니다 있는지 확인하십시오) :

<?xml version="1.0" encoding="UTF-8"?> 
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 
    <groupId>com.mycompany</groupId> 
    <artifactId>cditomcat</artifactId> 
    <packaging>war</packaging> 
    <version>0.0.1-SNAPSHOT</version> 
    <name>cditomcat</name> 
    <description>This pom is the base for a project using JSF2+CDI on a tomcat 7 and Arquillian for tests.</description> 

    <properties> 
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
    </properties> 

    <dependencies> 
     <!-- Java EE Dependencies --> 
     <dependency> 
      <groupId>com.sun.xml.ws</groupId> 
      <artifactId>jaxws-rt</artifactId> 
      <version>2.2.7</version> 
     </dependency> 

     <dependency> 
      <groupId>javax.faces</groupId> 
      <artifactId>jsf-api</artifactId> 
      <version>2.0</version> 
      <scope>compile</scope> 
     </dependency> 

     <dependency> 
      <groupId>com.sun.faces</groupId> 
      <artifactId>jsf-impl</artifactId> 
      <version>2.0.2-b10</version> 
      <scope>compile</scope> 
     </dependency> 

     <dependency> 
      <groupId>javax.validation</groupId> 
      <artifactId>validation-api</artifactId> 
      <version>1.0.0.GA</version> 
      <scope>compile</scope> 
     </dependency> 

     <dependency> 
      <groupId>org.hibernate</groupId> 
      <artifactId>hibernate-validator</artifactId> 
      <version>4.0.0.GA</version> 
      <scope>compile</scope> 
     </dependency> 

     <dependency> 
      <groupId>org.jboss.weld.servlet</groupId> 
      <artifactId>weld-servlet</artifactId> 
      <version>1.1.4.Final</version> 
     </dependency> 

     <!-- Test dependencies --> 
     <dependency> 
      <groupId>junit</groupId> 
      <artifactId>junit</artifactId> 
      <version>4.10</version> 
      <scope>test</scope> 
     </dependency> 

     <dependency> 
      <groupId>org.jboss.arquillian.junit</groupId> 
      <artifactId>arquillian-junit-container</artifactId> 
      <version>1.0.0.CR5</version> 
      <scope>test</scope> 
     </dependency> 

     <dependency> 
      <groupId>org.jboss.shrinkwrap.container</groupId> 
      <artifactId>shrinkwrap-container-tomcat-60</artifactId> 
      <version>1.0.0-beta-1</version> 
      <scope>test</scope> 
     </dependency> 

     <dependency> 
      <groupId>org.jboss.shrinkwrap</groupId> 
      <artifactId>shrinkwrap-api</artifactId> 
      <version>1.0.0-cr-1</version> 
      <scope>test</scope> 
     </dependency> 

     <dependency> 
      <groupId>org.jboss.shrinkwrap</groupId> 
      <artifactId>shrinkwrap-spi</artifactId> 
      <version>1.0.0-cr-1</version> 
      <scope>test</scope> 
     </dependency> 

     <dependency> 
      <groupId>org.jboss.shrinkwrap.descriptors</groupId> 
      <artifactId>shrinkwrap-descriptors-api</artifactId> 
      <version>1.1.0-beta-1</version> 
      <scope>test</scope> 
     </dependency> 

     <dependency> 
      <groupId>org.jboss.shrinkwrap.descriptors</groupId> 
      <artifactId>shrinkwrap-descriptors-impl</artifactId> 
      <version>1.1.0-beta-1</version> 
      <scope>test</scope> 
     </dependency> 

     <dependency> 
      <groupId>org.slf4j</groupId> 
      <artifactId>slf4j-api</artifactId> 
      <version>1.7.0</version> 
      <scope>test</scope> 
     </dependency> 
    </dependencies> 

    <build> 
     <finalName>cditomcat</finalName> 
     <testSourceDirectory>src/test/java</testSourceDirectory> 

     <plugins> 
      <!-- Facilitates downloading source and javadoc in Eclipse --> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-eclipse-plugin</artifactId> 
       <version>2.8</version> 
       <configuration> 
        <wtpversion>2.0</wtpversion> 
        <downloadSources>true</downloadSources> 
        <downloadJavadocs>true</downloadJavadocs> 
       </configuration> 
      </plugin> 

      <!-- Ensures we are compiling at 1.6 level --> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-compiler-plugin</artifactId> 
       <version>2.3.2</version> 
       <configuration> 
        <source>1.6</source> 
        <target>1.6</target> 
       </configuration> 
      </plugin> 

      <!-- Tomcat plugin for embedded tomcat --> 
      <plugin> 
       <groupId>org.codehaus.mojo</groupId> 
       <artifactId>tomcat-maven-plugin</artifactId> 
       <version>1.1</version> 
       <configuration> 
        <path>/${project.build.finalName}</path> 
       </configuration> 
      </plugin> 
     </plugins> 
    </build> 

    <profiles> 
     <profile> 
      <id>tc7-embedded</id> 
      <activation> 
       <activeByDefault>true</activeByDefault> 
      </activation> 

      <build> 
       <testResources> 
        <testResource> 
         <directory>src/test/resources</directory> 
        </testResource> 
        <testResource> 
         <directory>src/test/resources/embedded</directory> 
        </testResource> 
       </testResources> 
      </build> 

      <dependencies> 
       <dependency> 
        <groupId>org.jboss.arquillian.container</groupId> 
        <artifactId>arquillian-tomcat-embedded-7</artifactId> 
        <version>1.0.0.CR3</version> 
        <scope>test</scope> 
       </dependency> 

       <dependency> 
        <groupId>org.apache.tomcat.embed</groupId> 
        <artifactId>tomcat-embed-core</artifactId> 
        <version>7.0.19</version> 
        <scope>provided</scope> 
       </dependency> 

       <dependency> 
        <groupId>org.apache.tomcat.embed</groupId> 
        <artifactId>tomcat-embed-jasper</artifactId> 
        <version>7.0.19</version> 
        <scope>provided</scope> 
       </dependency> 

       <dependency> 
        <groupId>org.eclipse.jdt.core.compiler</groupId> 
        <artifactId>ecj</artifactId> 
        <version>3.7</version> 
        <scope>test</scope> 
       </dependency> 

       <!-- Provided scoped dependencies for embedded container --> 
       <dependency> 
        <groupId>org.apache.tomcat</groupId> 
        <artifactId>tomcat-catalina</artifactId> 
        <version>7.0.29</version> 
        <scope>provided</scope> 
       </dependency> 

       <dependency> 
        <groupId>org.apache.tomcat</groupId> 
        <artifactId>tomcat-coyote</artifactId> 
        <version>7.0.29</version> 
        <scope>provided</scope> 
       </dependency> 

       <dependency> 
        <groupId>org.apache.tomcat</groupId> 
        <artifactId>tomcat-jasper</artifactId> 
        <version>7.0.29</version> 
        <scope>provided</scope> 
       </dependency> 

       <dependency> 
        <groupId>javax.servlet</groupId> 
        <artifactId>servlet-api</artifactId> 
        <version>2.5</version> 
        <scope>provided</scope> 
       </dependency> 

       <dependency> 
        <groupId>org.jboss.arquillian.container</groupId> 
        <artifactId>arquillian-container-test-spi</artifactId> 
        <version>1.0.2.Final</version> 
        <scope>test</scope> 
       </dependency> 

       <dependency> 
        <groupId>org.jboss.arquillian.container</groupId> 
        <artifactId>arquillian-container-spi</artifactId> 
        <version>1.0.0.CR5</version> 
        <scope>test</scope> 
       </dependency> 
      </dependencies> 
     </profile> 

     <profile> 
      <id>tc7-managed</id> 
      <build> 
       <testResources> 
        <testResource> 
         <directory>src/test/resources</directory> 
        </testResource> 
        <testResource> 
         <directory>src/test/resources/managed</directory> 
        </testResource> 
       </testResources> 
      </build> 

      <dependencies> 
       <dependency> 
        <groupId>org.jboss.arquillian.container</groupId> 
        <artifactId>arquillian-tomcat-managed-7</artifactId> 
        <version>1.0.0.CR3</version> 
        <scope>test</scope> 
       </dependency> 

       <!-- Provided scoped dependencies for embedded container --> 
       <dependency> 
        <groupId>org.apache.tomcat</groupId> 
        <artifactId>tomcat-catalina</artifactId> 
        <version>7.0.29</version> 
        <scope>provided</scope> 
       </dependency> 

       <dependency> 
        <groupId>org.apache.tomcat</groupId> 
        <artifactId>tomcat-coyote</artifactId> 
        <version>7.0.29</version> 
        <scope>provided</scope> 
       </dependency> 

       <dependency> 
        <groupId>org.apache.tomcat</groupId> 
        <artifactId>tomcat-jasper</artifactId> 
        <version>7.0.29</version> 
        <scope>provided</scope> 
       </dependency> 

       <dependency> 
        <groupId>javax.servlet</groupId> 
        <artifactId>servlet-api</artifactId> 
        <version>2.5</version> 
        <scope>provided</scope> 
       </dependency> 

       <dependency> 
        <groupId>org.jboss.arquillian.container</groupId> 
        <artifactId>arquillian-container-test-spi</artifactId> 
        <version>1.0.2.Final</version> 
        <scope>test</scope> 
       </dependency> 

       <dependency> 
        <groupId>org.jboss.arquillian.container</groupId> 
        <artifactId>arquillian-container-spi</artifactId> 
        <version>1.0.0.CR5</version> 
        <scope>test</scope> 
       </dependency> 
      </dependencies> 
     </profile> 
    </profiles> 
</project> 

/src/main/webapp/WEB-INF/web.xml (여전히 버전 2를 사용했습니다.5) : 서블릿에서

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
    version="2.5"> 
    <welcome-file-list> 
     <welcome-file>index.html</welcome-file> 
    </welcome-file-list> 
    <context-param> 
     <param-name>facelets.DEVELOPMENT</param-name> 
     <param-value>true</param-value> 
    </context-param> 
    <servlet> 
     <servlet-name>Faces Servlet</servlet-name> 
     <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>Faces Servlet</servlet-name> 
     <url-pattern>*.jsf</url-pattern> 
    </servlet-mapping> 
    <listener> 
     <listener-class>org.jboss.weld.environment.servlet.Listener</listener-class> 
    </listener> 
    <resource-env-ref> 
     <description>Object factory for the CDI Bean Manager</description> 
     <resource-env-ref-name>BeanManager</resource-env-ref-name> 
     <resource-env-ref-type>javax.enterprise.inject.spi.BeanManager</resource-env-ref-type> 
    </resource-env-ref> 

</web-app> 

/src/main/webapp/META-INF/context.xml (주입, 청취자는,)를 사용하지 필터 :

<?xml version="1.0" encoding="UTF-8"?> 
<Context> 
    <!-- disable storage of sessions across restarts --> 
    <Manager pathname=""/> 
    <Resource name="BeanManager" auth="Container" type="javax.enterprise.inject.spi.BeanManager" factory="org.jboss.weld.resources.ManagerObjectFactory"/> 
</Context> 

/src/main/resources/META-INF/beans.xml/src/test/resources/in-container-beans.xml 가 (빈 마커 파일이 응용 프로그램과 테스트를 위해 동일입니다) :

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation=" 
     http://java.sun.com/xml/ns/javaee 
     http://java.sun.com/xml/ns/javaee/beans_1_0.xsd"> 
</beans> 

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
    version="2.5"> 

    <env-entry> 
     <env-entry-name>name</env-entry-name> 
     <env-entry-type>java.lang.String</env-entry-type> 
     <env-entry-value>Tomcat</env-entry-value> 
    </env-entry> 

    <listener> 
     <listener-class>org.jboss.weld.environment.servlet.Listener</listener-class> 
    </listener> 

    <resource-env-ref> 
     <resource-env-ref-name>BeanManager</resource-env-ref-name> 
     <resource-env-ref-type>javax.enterprise.inject.spi.BeanManager</resource-env-ref-type> 
    </resource-env-ref> 
</web-app> 

/src/test/resources/in-container-context.xml (임베디드 대한 동일 및 관리) :

/src/test/resources/in-container-web.xml (위한 동일한 임베디드 및 관리)

<Context> 
    <Resource name="BeanManager" auth="Container" 
     type="javax.enterprise.inject.spi.BeanManager" factory="org.jboss.weld.resources.ManagerObjectFactory" /> 
</Context> 

/src/test/resources/embedded/arquillian.xml (이 파일은 오직 삽입) :

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

    <engine> 
     <property name="deploymentExportPath">target</property> 
    </engine> 
    <container qualifier="tomcat" default="true"> 
     <configuration> 
      <property name="tomcatHome">target/tomcat-embedded-7</property> 
      <property name="workDir">work</property> 
      <property name="appBase">webapps</property> 
      <property name="bindHttpPort">8889</property> 
      <property name="unpackArchive">true</property> 
     </configuration> 
    </container> 
</arquillian> 

/src/test/resources/managed/arquillian.xml (이 파일은 관리만을위한 것입니다) :

... 
<role rolename="manager-gui" /> 
    <role rolename="manager-jmx" /> 
    <role rolename="manager-script" /> 
    <user username="tomcat" password="manager" 
     roles="manager-gui,manager-jmx, manager-script" /> 
... 
: 관리 컨테이너 배포가 작동하려면

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

    <container qualifier="tomcat" default="true"> 
     <configuration> 
      <property name="jmxPort">8099</property> 
      <property name="host">localhost</property> 
      <property name="port">8080</property> 
      <property name="user">tomcat</property> 
      <property name="pass">manager</property> 
     </configuration> 
    </container> 
</arquillian> 

는 바람둥이의 tomcat-users.xml 파일 관리자, 예를 들어 수 있도록 확장 할 수있다

마지막으로 각 arquillian test class은 다음과 같은 배포 방법을 가져야합니다. D 서블릿 2.5) :

... 
    @Deployment 
    @OverProtocol("Servlet 2.5") 
    public static WebArchive createDeployment() { 
     return ShrinkWrap 
       .create(WebArchive.class, "test.war") 
       .addClass(MyTest.class) 
       .addAsLibrary(
         MavenArtifactResolver 
           .resolve("org.jboss.weld.servlet:weld-servlet:1.1.4.Final")) 
       .addAsWebInfResource("in-container-beans.xml", "beans.xml") 
       .addAsManifestResource("in-container-context.xml", 
         "context.xml").setWebXML("in-container-web.xml"); 
    } 
... 

용접이 포장 될 수있다, 질문의 참조 예 (편집 섹션)에서 수행으로 다음 코드에 의해 내 경우에는 지역 받는다는 저장소에서 가져온 것입니다

public class MavenArtifactResolver { 
    private static final String LOCAL_MAVEN_REPO = 
      System.getProperty("user.home") + File.separatorChar + 
        ".m2" + File.separatorChar + "repository"; 

    public static File resolve(String groupId, String artifactId, String version) { 
     return new File(LOCAL_MAVEN_REPO + File.separatorChar + 
       groupId.replace(".", File.separator) + File.separatorChar + 
       artifactId + File.separatorChar + 
       version + File.separatorChar + 
       artifactId + "-" + version + ".jar"); 
    } 

    public static File resolve(String qualifiedArtifactId) { 
     String[] segments = qualifiedArtifactId.split(":"); 
     return resolve(segments[0], segments[1], segments[2]); 
    } 
} 
2

Tomcat 7 (TomEE)의 Java EE 인증 버전에서 작동하는 설정은 다음과 같습니다. 그것은 CDI를 포함하고 작업 Arquillian 어댑터의 소수가 있습니다 : 해당 페이지는 톰캣에 요청을 보내는 데 사용할 수있는 권리 포트를 얻을 속성을 사용하여 언급

참고. 그것을 무시하십시오. 올바른 접근 방식은 테스트 케이스에서 다음과 같은 필드입니다.

@ArquillianResource 
private URL url; 

웹 응용 프로그램, 포트 및 모두의 기본 URL이됩니다.

+0

나는 이미 TomEE에 대해 들어 봤지만, 고객의 환경이 설정되고 바닐라 바람둥이가있을 것이기 때문에 나의 경우에는 그렇게하지 않을 것입니다. 그러나 tomcat 7에 대한 어댑터가 있기 때문에 관리되고 내장되어 있으며 tomcat + CDI에 대한 작동 예제가 있기 때문에 나는 세 가지 모두에 대한 설정이 있기를 바랄뿐입니다 - 아직 예제를 찾지 못했습니다. –

+0

일반 Tomcat 7을 부팅하고 OpenWebBeans, OpenEJB 등을 배포 한 다음 응용 프로그램을 배포하는 어댑터 버전이 있습니다. 어떤 CDI 구현을 사용하고 있습니까? –

+0

JBoss 7 (필자가 선호하는 환경)과 함께 제공되는 Weld를 사용합니다. –

2

Welk-Servlet을 테스트 종속성으로 추가하는 Arquillian의 Tomcat-7 관리 컨테이너에 대해 POM을 살펴볼 수 있습니다.

용접 JAR은 ShrinkWrap Maven Dependency Resolver를 통해 Arquillian @Deployment defined in the test classes에로드됩니다. 또한 배포시 비어있는 beans.xml 파일을 포함해야합니다. 주 - 테스트 클래스에 주입 된 CDI 빈은 없지만 Weld는 @Resource을 삽입하는 데 사용됩니다.

+0

힌트를 보내 주셔서 감사합니다. 지적한 항목을 적용하려했지만 작동하지 않았습니다. 또한 [this] (https://community.jboss.org/wiki/Tomcat6EmbeddedAndRemote) 기사를 시작 지점으로 사용했습니다.이 지점은 Tomcat 6의 작동 설정을 보여 주며 Tomcat 7에 적용하려고했지만 작동하지 않았습니다. Tomcat 7 arquillian adapter와 cdi를 모두 다루는 작업 예제 나 적어도 pom을 가르쳐 주시겠습니까? –

+0

@ Geziefer 어떤 오류가보고 있습니까, 아니면 어디에서 오류가 났습니까? –

+0

것은, 나는 다른 것들을 시도하고 다른 예외를 가지고있다 (ClassNotFound, NoSuchMethod, ...) 그리고 정확히 무엇이 필요한지 모른 채 의존성을 계속 추가합니다. 그래서 제가 두려워 할 점은, 한 가지 예외를 게시하면, 여기에서 해결 될 수 있고, 처음에 잘못된 방향으로 갔다는 것을 알지 못해서 다음으로 이끌 수 있다는 것입니다. 그래서, 제 질문을 연장했습니다, 위를 참조하십시오. –