spring - वसंत आवेदन संदर्भ के साथ JUnit कस्टम धावक
integration-testing applicationcontext (1)
आप एक बेस टेस्ट क्लास बना सकते हैं - @ContextConfiguration
को विरासत में @ContextConfiguration
जा सकता है, साथ ही @Before
, @After
, और इसी तरह:
@ContextConfiguration(locations = { "/META-INF/spring/testContext.xml" })
public abstract class myBaseTest {
@Before
public void init() {
// custom initialization code for resources loaded by testContext.xml
}
@After
public void cleanup() {
// custom cleanup code for resources loaded by testContext.xml
}
}
@RunWith(SpringJUnit4ClassRunner.class)
public class myTest extends myBaseTest { ... }
मैं स्प्रिंग के लिए काफी नया हूँ और एक वेब एप्लीकेशन के लिए जेयूनेट 4.7 एकीकरण परीक्षणों के एक सूट के साथ काम कर रहा हूं। मेरे पास प्रपत्र के कई कार्य परीक्षण मामले हैं:
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/META-INF/spring/testContext.xml" })
public class myTest {
@Test
public void testCreate() {
//execute tests
....
}
}
मेरे आवेदन में कई बाहरी निर्भरताएं हैं जो मैं परीक्षण कर रहा हूं, जिनमें से सभी बीन्स है जो testContext.xml के लोडिंग के माध्यम से आरंभीकृत हैं। इनमें से कुछ बाहरी निर्भरताएं आवश्यक संसाधनों को प्रारंभ करने और आंसू करने के लिए कस्टम कोड की आवश्यकता होती हैं।
इसके बजाय हर टेस्ट क्लास में इस कोड को डुप्लिकेट करने के बजाए, मैं इसे एक सामान्य स्थान में समापित करना चाहता हूं। मेरा विचार एक अलग संदर्भ परिभाषा बनाने के साथ-साथ एक कस्टम रनर भी है जो SpringJUnit4ClassRunner को फैलाता है और इसमें @ContextConfiguration एनोटेशन और संबंधित कस्टम कोड शामिल है, जैसे:
import org.junit.runners.model.InitializationError;
import org.junit.runners.model.Statement;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
//load the context applicable to this runner
@ContextConfiguration(locations = { "/META-INF/spring/testContext.xml" })
public class MyCustomRunner extends SpringJUnit4ClassRunner {
public MyCustomRunner(Class<?> clazz) throws InitializationError {
super(clazz);
}
@Override
protected Statement withBeforeClasses(Statement statement) {
// custom initialization code for resources loaded by testContext.xml
...
return super.withBeforeClasses(statement);
}
@Override
protected Statement withAfterClasses(Statement statement) {
// custom cleanup code for resources loaded by testContext.xml
....
return super.withAfterClasses(statement);
}
}
तब मैं प्रत्येक टेस्ट क्लास को इसके लागू रनर को निर्दिष्ट कर सकता हूं:
@RunWith(MyCustomRunner)
जब मैं ऐसा करता हूं, मेरे परीक्षण चलते हैं और साथ-साथ पहले क्लासेस और एफ़्टर क्लास विधियों को निष्पादित किया जाता है। हालाँकि, कोई भी अनुप्रयोगान्तरण टेस्ट कक्षा में वापस प्रदान नहीं किया गया है और मेरे सभी परीक्षण विफल होते हैं:
java.lang.IllegalArgumentException: एक NULL 'contextLoader' के साथ एक ApplicationContext लोड नहीं किया जा सकता @ कॉन्टेक्स कॉन्फ़िगरेशन के साथ अपने टेस्ट क्लास की व्याख्या करें
प्रसंग केवल सही ढंग से लोड हो जाता है यदि मैं प्रत्येक परीक्षा वर्ग पर @ कन्टेक्स कॉन्फ़िगरेशन एनोटेशन निर्दिष्ट करता हूं - आदर्श रूप से, मैं यह एनोटेशन संसाधनों के लिए हेन्डलर कोड के साथ रहने के लिए चाहूंगा जो लोडिंग के लिए जिम्मेदार है। कौन मुझे मेरे प्रश्न पर ले जाता है - क्या कस्टम रैंडर क्लास से वसंत संदर्भ जानकारी को लोड करना संभव है?