2013-02-08 24 views
5

मेरे पास एक ग्रिज़ली HttpServer है जो मैं परीक्षण समूह निष्पादन की पूरी अवधि के लिए दौड़ना चाहता हूं। इसके अतिरिक्त, मैं परीक्षण के अंदर @Rule से वैश्विक HttpServer इंस्टेंस के साथ बातचीत करना चाहता हूं।एक निश्चित निष्पादन में सभी परीक्षणों से पहले और बाद में कोड चलाना

चूंकि मैं जेनिट परीक्षण सूट का उपयोग करने के बजाय मेवेन सरेफायर का उपयोग कर रहा हूं, इसलिए मैं टेस्ट सूट पर @BeforeClass/@AfterClass का उपयोग नहीं कर सकता।

अभी, मैं सोच सकता हूं कि मैं एक स्थिर क्षेत्र शुरू कर रहा हूं और Runtime.addShutdownHook() से सर्वर को रोक रहा हूं - अच्छा नहीं!

+0

सकें कि आप अगर JUnit नहीं प्रयोग कर रहे हैं से है? – TheWhiteRabbit

+0

यदि आप पीओजेओ या टेस्टएनजी परीक्षण का उपयोग कर रहे हैं तो आप @BeforeClass – TheWhiteRabbit

+0

@TechExchange का उपयोग कर सकते हैं यह स्पष्ट करने के लिए अद्यतन किया गया है कि मैं मेवेन surefire – hertzsprung

उत्तर

7

दो विकल्प हैं, एक मेवेन समाधान और एक निश्चित समाधान। कम से कम युग्मित समाधान pre-integration-test और post-integration-test चरण में प्लगइन निष्पादित करना है। Introduction to the Build Lifecycle - Lifecycle Reference देखें। मैं ग्रिजली से परिचित नहीं हूँ, लेकिन यहाँ एक उदाहरण का उपयोग करते हुए घाट है:

<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> 

ध्यान दें कि start के लिए चरण pre-integration-test और stoppost-integration-test है। मुझे यकीन नहीं है कि कोई ग्रिज़ली मेवेन प्लगइन है, लेकिन आप इसके बजाय maven-antrun-plugin का उपयोग कर सकते हैं।

दूसरा विकल्प एक जुनीट RunListener का उपयोग करना है। RunListener इस तरह के परीक्षण शुरू, परीक्षण अंत, परीक्षण की विफलता, परीक्षण सफलता आदि

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 {} 
} 

के रूप में की घटनाओं, परीक्षण करने के लिए तो तुम RunStarted और RunFinished को सुन सके और सुनता है। ये आपकी इच्छित सेवाओं को शुरू/बंद कर देंगे। फिर, अचूक में, आप एक कस्टम श्रोता, निर्दिष्ट कर सकते हैं का उपयोग कर:

<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> 

यह Maven Surefire Plugin, Using JUnit, Using custom listeners and reporters

+0

का उपयोग कर रहा हूं, मुझे नहीं लगता कि पहला विकल्प काम करेगा क्योंकि मुझे 'HttpServer' उदाहरण तक पहुंच की आवश्यकता है 'टेस्टरुले' से, लेकिन 'रनलिस्टर' वादा करता है, धन्यवाद! पूर्व-एकीकरण चरण के हिस्से के रूप में, मेरे लिए – hertzsprung

+0

, जेटी सर्वर शुरू होता है। अंतिम लॉग लाइन है: [जानकारी] जेटी सर्वर शुरू किया। उसके बाद, कुछ भी नहीं होता है। यह अटक गया है। maven surefire failsafe प्लगइन परीक्षण निष्पादित नहीं करता है और न ही जेटी सर्वर बंद हो जाता है। कोई विचार क्या गलत है? मैं आपके द्वारा निर्दिष्ट अनुसार कॉन्फ़िगरेशन का उपयोग कर रहा हूं। –