2010-04-12 10 views
29

तरह धावक है करता है जब मैं JUnit में परीक्षण लिखने (वसंत संदर्भ में) मैं आमतौर पर इसे इस तरह कार्य करें:TestNG SpringJUnit4ClassRunner

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration("classpath:testContext.xml") 
public class SimpleTest { 

    @Test 
    public void testMethod() { 
     // execute test logic... 
    } 
} 

मैं कैसे TestNG साथ भी ऐसा ही कर सकते हैं?


मैं और विवरण जोड़ूंगा। AbstractTestNGSpringContextTests के साथ यह काम करता है, लेकिन जिस तरह से मैं चाहता हूं। मैं कुछ परीक्षण है ...

@ContextConfiguration(locations = { "classpath:applicationContextForTests.xml" }) 
public class ExampleTest extends AbstractTestNGSpringContextTests { 

    private Boolean someField; 

    @Autowired 
    private Boolean someBoolean; 

    @Test 
    public void testMethod() { 
     System.out.println(someField); 
     Assert.assertTrue(someField); 
    } 

    @Test 
    public void testMethodWithInjected() { 
     System.out.println(someBoolean); 
     Assert.assertTrue(someBoolean); 
    } 

    // setters&getters 
} 

और वर्णनकर्ता ...

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> 

    <bean id="exampleTest" class="pl.michalmech.ExampleTest"> 
     <property name="someField"> 
      <ref bean="someBoolean"/> 
     </property> 
    </bean> 

    <bean id="someBoolean" class="java.lang.Boolean"> 
     <constructor-arg type="java.lang.String" value="true"/> 
    </bean> 
</beans> 

परिणाम हैं ...

null 
true 
Tests run: 2, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.599 sec <<< FAILURE! 

Results : 

Failed tests: 
    testMethod(pl.michalmech.ExampleTest) 

है यही कारण है कि मैं के बारे में धावक पूछा।

+0

http://stackoverflow.com/questions/2608528/spring- निर्भरता- इंजेक्शन-with- के संभावित डुप्लिकेट testng –

+0

ऐसा लगता है, लेकिन काफी अलग है। –

उत्तर

3

टेस्टएनजी आपके परीक्षण को तुरंत चालू करने के लिए वसंत का उपयोग नहीं करता है। यही कारण है कि कुछ फ़ील्ड = शून्य

3

सही, टेस्टएनजी हमेशा टेस्ट क्लास को तुरंत चालू करता है (सत्यापित करने के लिए कन्स्ट्रक्टर में ब्रेकपॉइंट डालता है)। बाद में (@BeforeClass) संदर्भ से सेम टेस्ट क्लास में इंजेक्शन दिया जाता है।

हालांकि मैं उत्सुक हूं कि आप प्रत्येक जगह बीन के रूप में परीक्षण को परिभाषित क्यों करेंगे। 10 सालों में मैंने स्प्रिंग का उपयोग किया है, मुझे ऐसा करने की ज़रूरत नहीं है, या किसी ने इसे देखा है ...

संबंधित मुद्दे