For integration tests for my spring application with junit, I will subclass org.springframework.test.context.ContextLoaderbecause I want to use an existing one XmlWebApplicationContextto connect my test class as follows:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(loader=MyContextLoader.class)
@Transactional
public class MyTest {
@Autowired
public AccountDao accountDao;
}
The implementation of my ContextLoader is as follows:
Public class MyContextLoader implements ContextLoader {
@Override
public String[] processLocations(Class<?> clazz, String... locations) {
return locations;
}
@Override
public ApplicationContext loadContext(String... locations) throws Exception {
try {
EmbeddedTomcat tomcat = new EmbeddedTomcat("mas", 8080);
tomcat.launch();
Context rootContext = tomcat.getRootContext();
ContextLoaderListener contextLoaderListener = (ContextLoaderListener) rootContext.getApplicationLifecycleListeners()[0];
XmlWebApplicationContext context = (XmlWebApplicationContext) contextLoaderListener.getContext();
GenericApplicationContext c = new GenericApplicationContext(context);
AnnotationConfigUtils.registerAnnotationConfigProcessors(c);
return context;
}
catch(Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
loadContext(...) getBean (AccountDao.class), . , , . spring, , AbstractAutowireCapableBeanFactory.populateBean(String beanName, AbstractBeanDefinition mbd, BeanWrapper bw) PropertyValues .
, ?
: , , , tomcat, - RESTful. "" tomcat, : spring Embedded Tomcat 6
.
Erik