2012-11-16 4 views
8

Tomcat에서 실행할 WAR을 생성하는 Maven 기반 웹 응용 프로그램 프로젝트가 있습니다. 논쟁을 위해서, 프로젝트의 단위 테스트가 네트워크 (단순히 모의 요청으로 서블릿 메소드를 호출하는 것이 아니라)을 통해 실제로 요청을 보내거나받는 것이 아주 중요하다고 가정 해 봅시다.테스트를 위해 "in-process"Tomcat 인스턴스를 가질 수 있습니까?

동일한 테스트 환경에서 동일한 JVM에서 Tomcat 인스턴스를 실행하고 현재 프로젝트를로드 한 다음 테스트 케이스가 localhost에 도달하도록 할 방법이 있습니까? 실패하면 어떻게하면 현재 프로젝트 (의존성과 함께)를 WAR에 프로그래밍하여 다른 C 라이브러리를 사용하여 다른 Tomcat 인스턴스에 프로그래밍 방식으로 업로드 할 수 있습니까? mvn으로 포격하는 것보다 더 좋은 대안이 있습니까?

가 내 요청이 특이 알고 단위 테스트를해야 더 독립적 인, 등,하지만 :)

+2

가 왜 임베디드 바람둥이 볼 수 없습니다 http://tomcat.apache.org/tomcat-7.0-doc/api/org/apache/catalina/startup/Embedded.html –

+0

가능한 복제본 [Howto embedded Tomcat 6?] (http://stackoverflow.com/questions/640022/howto-embed-tomcat-6) –

+0

@ArunPJohny : 정확히 내가 뭘 찾고 있었는지 - 훌륭했습니다! 당신 (또는 원하는 사람)이 답변으로 올리면 받아 들일 것입니다. :) –

답변

5

을 전쟁으로 바람둥이 인스턴스를 시작하고 셀레늄 테스트

을 실행

    • 임베디드 JUnit 테스트
    • 을 당신이 embedded Tomcat에 사용할 수 있습니다 정확히이 목적.테스트 하네스에 정적 인스턴스를 설정하고 마지막에 종료하십시오. 여기에 몇 가지 예제 코드입니다 :

      import org.apache.catalina.LifecycleException; 
      import org.apache.catalina.startup.Tomcat; 
      import org.junit.AfterClass; 
      import org.junit.BeforeClass; 
      
      public class TomcatIntegrationTest { 
          private static Tomcat t; 
          private static final int TOMCAT_PORT = 9999; 
      
          @BeforeClass 
          public static void setUp() throws LifecycleException { 
          t = new Tomcat(); 
          t.setBaseDir("."); 
          t.setPort(TOMCAT_PORT); 
          /* There needs to be a symlink to the current dir named 'webapps' */ 
          t.addWebapp("/service", "src/main/webapp"); 
          t.init(); 
          t.start(); 
          } 
      
          @AfterClass 
          public static void shutDownTomcat() throws LifecycleException { 
          t.stop(); 
          } 
      } 
      
  • 6

    부두는이 목적을 위해 정말 잘 작동 함께 그냥 플레이 주시기 바랍니다. Tomcat을 사용하지 않아도된다면 통합 테스트 단계에서 이것을 아주 쉽게 사용할 수 있습니다. 사전 통합이 부두에서 시작되고 통합 후 작업이 중지되고 실제 컨테이너에서 실행 중이므로 war 파일에서 요청을 던질 수 있습니다.

    +0

    Downvote는 Tomcat에 관한 질문이기 때문에. 당신이 설명하는 접근법은 Tomcat에서 가능하므로 컨테이너를 바꿀 필요가 없습니다. –

    +0

    대체 접근법을 제공하면 "Tomcat을 사용하지 않아도된다"고 구체적으로 언급했음을 알 수 있습니다. 그렇습니다. Tomcat을 임베드 할 수는 있지만, Jetty는이 사용법에서 훨씬 가볍습니다. – Michael

    2

    적절한 통합 테스트를 만들고 다음 설정을 사용하여 메이븐 빌드 중에 통합 테스트를 수행하는 것이 좋습니다.

    tomcat을 다운로드하거나 저장소에서 이슈를 사용해야하는 경우 다음 부분을 사용할 수 있습니다.

    <groupId>org.codehaus.cargo</groupId> 
    <artifactId>cargo-maven2-plugin</artifactId> 
    <configuration> 
        <wait>false</wait> 
        <container> 
         <containerId>tomcat${tomcat.major}x</containerId> 
         <zipUrlInstaller> 
          <url>http://archive.apache.org/dist/tomcat/tomcat-${tomcat.major}/v${tomcat.version}/bin/apache-tomcat-${tomcat.version}.tar.gz</url> 
          <extractDir>${project.build.directory}/extract/</extractDir> 
          <downloadDir>${project.build.directory}/download/</downloadDir> 
         </zipUrlInstaller> 
         <output>${project.build.directory}/tomcat${tomcat.major}x.log</output> 
         <log>${project.build.directory}/cargo.log</log> 
        </container> 
        <configuration> 
         <home>${project.build.directory}/tomcat-${tomcat.version}/container</home> 
         <properties> 
          <cargo.logging>high</cargo.logging> 
          <cargo.servlet.port>9080</cargo.servlet.port> 
          <cargo.tomcat.ajp.port>9008</cargo.tomcat.ajp.port> 
         </properties> 
        </configuration> 
    </configuration> 
    

    다음 부분은 응용 프로그램을 시작하여 주어진 tomcat (jetty와의 작업)에 배포하는 데 사용됩니다.

    <execution> 
        <id>stop-container</id> 
        <phase>post-integration-test</phase> 
        <goals> 
         <goal>stop</goal> 
        </goals> 
    </execution> 
    

    가장 좋은 것은이 앱을 호출 할 수있는 별도 받는다는 모듈에 구성 등의이 종류를 넣어하는 것입니다 (:

    <executions> 
        <execution> 
         <id>start-container</id> 
         <phase>pre-integration-test</phase> 
         <goals> 
          <goal>start</goal> 
          <goal>deploy</goal> 
         </goals> 
         <configuration> 
          <deployer> 
           <deployables> 
            <deployable> 
             <groupId>${project.groupId}</groupId> 
             <artifactId>mod-war</artifactId> 
             <type>war</type> 
             <pingURL>http://localhost:9080/mod-war</pingURL> 
             <pingTimeout>30000</pingTimeout> 
             <properties> 
              <context>mod-war</context> 
             </properties> 
            </deployable> 
           </deployables> 
          </deployer> 
         </configuration> 
        </execution> 
    

    물론

    마침내 통해 시작 서버를 중지합니다 통합 테스트 용). 에 통합 테스트 단계를 실행하고, 상기 구성을 개시한다 기간을 확인하는 반면 테스트 완전한 사이클 단순히

    mvn verify 
    

    의해 호출 될 수있다. 그러나 통합 테스트 자체가 실행되도록 maven-failsafe-plugin을 구성하는 것이 중요합니다. 이에 대한 자세한 설명은 당신이 할 수있는 것은 http://tomcat.apache.org/maven-plugin-2.0/archetype.html를 참조 아파치 톰캣 Maven 플러그인과 원형을 생성하는 것입니다 Maven Unit- and Integration Test Guide

    <plugin> 
        <groupId>org.apache.maven.plugins</groupId> 
        <artifactId>maven-failsafe-plugin</artifactId> 
        <version>2.12</version> 
        <executions> 
        <execution> 
         <id>integration-test</id> 
         <goals> 
         <goal>integration-test</goal> 
         </goals> 
        </execution> 
        <execution> 
         <id>verify</id> 
         <goals> 
         <goal>verify</goal> 
         </goals> 
        </execution> 
        </executions> 
    </plugin> 
    
    0

    에서 찾을 수 있습니다.

    당신은 다양한 샘플이됩니다 HTH

    관련 문제