2015-01-16 2 views
11

मैंने लिखा है के लिए कक्षाओं प्रोसेस नहीं कर सकता:परीक्षण निम्न स्थानों और संदर्भ विन्यास

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(locations = {"classpath:META-INF/dataContext.xml"},classes = Configiuration.class) 
@ActiveProfiles("test") 
public class CityDaoImplTest { 
.... 
} 

मैं xml फ़ाइल से और जावा वर्ग बर से विन्यास का उपयोग करने जब मैं आह्वान

mvn परीक्षण मैं में निम्नलिखित इसे देख की जरूरत है कंसोल:

Tests in error: 
    initializationError(***.CityDaoImplTest): Cannot process locations AND classes for context configuration [[email protected] declaringClass = '***.CityDaoImplTest', classes = '{***.Configiuration}', locations = '{classpath:META-INF/dataContext.xml}', inheritLocations = true, initializers = '{}', inheritInitializers = true, name = [null], contextLoaderClass = 'org.springframework.test.context.ContextLoader']; configure one or the other, but not both. 

कॉन्फ़िगरेशन कॉन्फ़िगरेशन के बिना इसे कैसे ठीक किया जाए?

उत्तर

29

Spring Docs से:

3,1 वसंत के पहले, केवल पथ आधारित संसाधन स्थानों समर्थित थे। स्प्रिंग 3.1 के रूप में, संदर्भ लोडर या तो पथ-आधारित या क्लास-आधारित संसाधनों का समर्थन करना चुन सकते हैं। स्प्रिंग 4.0.4 के रूप में, संदर्भ लोडर पथ-आधारित और क्लास-आधारित संसाधनों का एक साथ समर्थन करना चुन सकते हैं।

हालांकि, वसंत परीक्षण के साथ एक छोटी सी चेतावनी है। यह SmartContextLoader जो AbstractDelegatingSmartContextLoader पर आधारित है का उपयोग करता है और दुर्भाग्य से यह इतना स्मार्ट नहीं है,)

@Override 
public void processContextConfiguration(
     final ContextConfigurationAttributes configAttributes) { 

    Assert.notNull(configAttributes, "configAttributes must not be null"); 
    Assert.isTrue(!(configAttributes.hasLocations() && configAttributes.hasClasses()), String.format(
     "Cannot process locations AND classes for context " 
       + "configuration %s; configure one or the other, but not both.", configAttributes)); 

के रूप में कोड, स्थानों में दिखाया गया है और कक्षाएं दोनों सेट नहीं किया जा सकता है।

तो, इसे कैसे ठीक करें?

@Configuration 
@ImportResource("classpath:META-INF/dataContext.xml") 
class TestConfig { 

} 

और, अपने परीक्षण कोड में निम्नलिखित का उपयोग करें:: ठीक है, एक ही समाधान जैसे कि निम्न एक अतिरिक्त config वर्ग को जोड़ने के लिए है

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(classes = {Configuration.class, TestConfig.class}) 
@ActiveProfiles("test") 
public class CityDaoImplTest { ... } 

तकनीकी तौर पर, इस विन्यास को फिर से लिखने है लेकिन आपको अपनी मौजूदा कॉन्फ़िगरेशन को बदलने की ज़रूरत नहीं है, बस एक नया @Configuration क्लास जोड़ें (और वह कक्षा आपके परीक्षण मामले के समान फ़ाइल में भी हो सकती है)।

+1

मेरी पोम से: 4.0.7.RELEASE gstackoverflow

0

यहां तक ​​कि यदि आपके लिए देर हो चुकी है तो भी मैं अपने उत्तर को अन्य लोगों की मदद के लिए पोस्ट करूंगा जो इसे पढ़ेंगे।

एक और समाधान डेटा कॉन्टेक्स्ट.एक्सएमएल में बीन के रूप में अपनी कॉन्फ़िगरेशन कक्षा घोषित करना है।

तुम सब करने की ज़रूरत है:

<bean class="com.packageWhereConfigClassIsPresent.Configuration"/> 

आशा है कि यह किसी को मदद मिलेगी;)

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