Steps1, Inherit abstractjunit4springcontexttests2, introduce ApplicationContext
Sample Code
123456789101112131415161718192021222324 |
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith
(SpringJUnit4ClassRunner.
class
)
@ContextConfiguration
(locations = {
"/spring/applicationContext.xml" })
public class SpringTest
extends AbstractJUnit4SpringContextTests {
@Autowired
protected ApplicationContext ctx;
public <T> T getBean(Class<T> type) {
return applicationContext.getBean(type);
}
public Object getBean(String beanName) {
return applicationContext.getBean(beanName);
}
protected ApplicationContext getContext() {
return applicationContext;
}
}
|
From for notes (Wiz)
Springtest Framework junit Unit test Case Get ApplicationContext instance method