2013-02-08 3 views
5

테스트 그룹 실행의 전체 기간 동안 실행하려는 Grizzly HttpServer이 있습니다. 또한 테스트 자체 내에서 @Rule의 글로벌 HttpServer 인스턴스와 상호 작용하고 싶습니다.확실한 실행에서 모든 테스트 전후의 코드 실행

JUnit 테스트 스위트를 사용하는 대신 Maven Surefire를 사용하고 있으므로 테스트 스위트 자체에서 @BeforeClass/@AfterClass을 사용할 수 없습니다.

지금 당장은 정적 필드를 느리게 초기화하고 Runtime.addShutdownHook()에서 서버를 중지하는 것으로 생각할 수 있습니다. 멋지지 않습니다!

+0

에서입니까? – TheWhiteRabbit

+0

POJO 또는 TestNG 테스트를 사용하는 경우 @BeforeClass를 사용할 수 있습니다. – TheWhiteRabbit

+0

@TechExchange Maven surefire를 사용하고 있음을 명확히하기위한 질문 업데이트 – hertzsprung

답변

7

메이븐 솔루션과 확실한 솔루션의 두 가지 옵션이 있습니다. 가장 적은 결합 솔루션은 pre-integration-testpost-integration-test 단계에서 플러그인을 실행하는 것입니다. Introduction to the Build Lifecycle - Lifecycle Reference을 참조하십시오. 나는 회색에 익숙하지 해요,하지만 여기에 예를 사용하여 부두입니다 : start에 대한 위상이 pre-integration-teststoppost-integration-test 인 것을

<build> 
    <plugins> 
    <plugin> 
    <groupId>org.mortbay.jetty</groupId> 
    <artifactId>maven-jetty-plugin</artifactId> 
    <configuration> 
    <contextPath>/xxx</contextPath> 
    </configuration> 
    <executions> 
    <execution> 
     <id>start-jetty</id> 
     <phase>pre-integration-test</phase> 
     <goals> 
     <goal>run</goal> 
     </goals> 
     <configuration> 
     </configuration> 
    </execution> 
    <execution> 
     <id>stop-jetty</id> 
     <phase>post-integration-test</phase> 
     <goals> 
     <goal>stop</goal> 
     </goals> 
    </execution> 
    </executions> 
    </plugin> 

참고. 그리즐리 메이븐 플러그인이 있는지 확실하지 않지만 대신 maven-antrun-plugin을 사용할 수 있습니다.

두 번째 옵션은 JUnit RunListener을 사용하는 것입니다. RunListener 그래서 당신이 RunStarted 및 RunFinished을 수신 할 수 등

public class RunListener { 
    public void testRunStarted(Description description) throws Exception {} 
    public void testRunFinished(Result result) throws Exception {} 
    public void testStarted(Description description) throws Exception {} 
    public void testFinished(Description description) throws Exception {} 
    public void testFailure(Failure failure) throws Exception {} 
    public void testAssumptionFailure(Failure failure) {} 
    public void testIgnored(Description description) throws Exception {} 
} 

테스트 시작, 테스트 종료, 테스트 실패, 시험 성공 등의 이벤트를 테스트하기 위해 수신합니다. 이것들은 당신이 원하는 서비스를 시작/중지시킬 것입니다. 그런 다음, 확실한, 당신은 사용하여 사용자 정의 리스너를 지정할 수 있습니다

<plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-surefire-plugin</artifactId> 
    <version>2.10</version> 
    <configuration> 
    <properties> 
     <property> 
     <name>listener</name> 
     <value>com.mycompany.MyResultListener,com.mycompany.MyResultListener2</value> 
     </property> 
    </properties> 
    </configuration> 
</plugin> 

을이 JUnit을하지 않을 경우 사용하는 하나의 Maven Surefire Plugin, Using JUnit, Using custom listeners and reporters

+0

'HttpServer' 인스턴스에 대한 액세스가 필요하기 때문에 첫 번째 옵션이 작동하지 않을 것이라고 생각합니다 'TestRule'에서 왔지만,'RunListener'는 유망한 소리입니다, 감사합니다! 나를위한 – hertzsprung

+0

은 사전 통합 단계의 일부로 부두 서버를 시작합니다. 마지막 로그 라인은 다음과 같습니다. [INFO] Jetty Server가 시작되었습니다. 그 후에는 아무 일도 일어나지 않습니다. 그것은 붙어 얻는다. surefire failsafe 플러그인은 테스트를 수행하지 않으며 부두가 서버를 중지하지 않습니다. 어떤 생각이 잘못된거야? 나는 당신이 지정한 것과 같은 구성을 사용하고 있습니다. –