java - How to write JUnit test with Spring Autowire? -


यहां जिन फ़ाइलों का मैं उपयोग करता हूं:

component.xml < / P>

  & lt;? Xml संस्करण = "1.0" एन्कोडिंग = "UTF-8"? & Gt; & Lt; बीन्स xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns: संदर्भ = "http: // Www.springframework.org/schema/context "xmlns: जी =" http://www.springframework.org/schema/jee "xsi: स्कीमा स्थान =" http://www.springframework.org/schema/beans http: / /www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0 .xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd"> & Lt; संदर्भ: घटक-स्कैन बेस-पैकेज = "नियंत्रक, सेवाएं, दाओ, org.springframework.jndi" / & gt; & Lt; / सेम ​​& gt;  

ServiceImpl.java

  @ org.springframework.stereotype.Service सार्वजनिक वर्ग ServiceImpl लागू करता MyService {@Autowired निजी MyDAO myDAO ; सार्वजनिक शून्य GetData () {...}}  

ServiceImplTest.java

  @RunWith (SpringJUnit4ClassRunner.class) @ संदर्भ कॉन्फ़िगरेशन ("क्लासपाथ *: conf / components.xml") सार्वजनिक वर्ग ServiceImplTest {@Test सार्वजनिक शून्य परीक्षण MyFunction () {...}}  

त्रुटि:

  16: 22: 48.753 [मुख्य] ​​त्रुटि ostest.context.TestContextManager - पकड़ा अपवाद TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@2092dcdb] परीक्षण उदाहरण तैयार करने के लिए अनुमति देता है, जबकि [Services.ServiceImplTest@9e1be92] org.springframework.beans.factory.BeanCreationException: नाम 'services.ServiceImplTest' के साथ सेम बनाने में त्रुटि: autowired निर्भरता का इंजेक्शन विफल; नेस्टेड अपवाद org.springframework.beans.factory.BeanCreationException: फ़ील्ड autowire नहीं कर सका: निजी सेवाओं। सेवाइंपल सेवाओं। सेवा ImplTest.publishedServiceImpl; [Services.ServiceImpl] के प्रकार कोई मिलती-जुलती सेम निर्भरता के लिए मिला: उम्मीद कम से कम 1 सेम जो इस निर्भरता के लिए autowire उम्मीदवार के रूप में उत्तीर्ण नेस्टेड अपवाद org.springframework.beans.factory.NoSuchBeanDefinitionException है। निर्भरता एनोटेशन: {@ org.springframework.beans.factory.annotation.Autowired (आवश्यक = सच)} at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues ​​(AutowiredAnnotationBeanPostProcessor.java:287) ~ [वसंत- beans.jar : 3.1.2.RELEASE]  

सुनिश्चित करें कि आपने सही पैकेज आयात किया है अगर मैं सही ढंग से याद दिलाता हूं, तो ऑटोवॉर्ंग के लिए दो अलग-अलग पैकेज हैं I होना चाहिए: org.springframework.beans.factory.annotation.Autowired;

इसके अलावा इस अजीब मेरे लिए लग रहा है:

  @ContextConfiguration ( " classpath *: conf / components.xml ")  

यहां एक उदाहरण है कि मेरे लिए ठीक काम करता है:

  @RunWith (SpringJUnit4ClassRunner.class) @ContextConfiguration (स्थानों = {"/applicationContext_mock.xml"}) सार्वजनिक श्रेणी के स्वामी इन्टिग्रेशनटस्ट {@Autowired OwnerService ownerService; @ सार्वजनिक निष्क्रिय सेटअप से पहले () {ownerService.cleanList (); } @Test सार्वजनिक शून्य परीक्षण ओनर्स () {स्वामी के मालिक = नया स्वामी ("Bengt", "कार्ल्सन", "अंकवागेन 3"); मालिक = स्वामीसेवा। सृजनकर्ता (मालिक); AssertEquals ("firstName चेक करें:", "Bengt", owner.getFirstName ()); AssertTrue ("उस आईडी की जांच करें:", स्वामी .getId ()> gt; 0); owner.setLastName ( "लार्सन"); ownerService.updateOwner (स्वामी); स्वामी = स्वामीसेवा। ओपनर (मालिक.गेटआईडी ()); AssertEquals ("नाम बदल गया है", "लार्सन", owner.getLlastName ()); }  

Comments