Announcement Announcement Module
No announcement yet.
Correct usage of AnnotationConfigUtils in subclassed ContextLoader Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Correct usage of AnnotationConfigUtils in subclassed ContextLoader

    For the integration tests for my spring application with junit I am subclassing org.springframework.test.context.ContextLoader, because I want to use a already present XmlWebApplicationContext for wiring up my test class like this:

    public class MyTest {
        public AccountDao accountDao;
    The implementation of my ContextLoader is as follows:

    public class MyContextLoader implements ContextLoader {
        public String[] processLocations(Class<?> clazz, String... locations) {
            return locations;
        public ApplicationContext loadContext(String... locations) throws Exception {
            try {
                // Start Embedded Tomcat
                EmbeddedTomcat tomcat = new EmbeddedTomcat("mas", 8080);
                Context rootContext = tomcat.getRootContext();
                ContextLoaderListener contextLoaderListener = (ContextLoaderListener) rootContext.getApplicationLifecycleListeners()[0];
                XmlWebApplicationContext context = (XmlWebApplicationContext) contextLoaderListener.getContext();
                GenericApplicationContext c = new GenericApplicationContext(context);
                return context;
            catch(Exception e) {
                throw new RuntimeException(e);

    When putting a breakpoint in the loadContext(...) method I can call getBean(AccountDao.class) and everything works fine. However, it seems that my test class actually is not autowired. I debugged a little and stepped through the spring code and it seems that in the method AbstractAutowireCapableBeanFactory.populateBean(St ring beanName, AbstractBeanDefinition mbd, BeanWrapper bw) the PropertyValues are not set for my class Test.

    Maybe, am I setting up the Annotation Processing wrong?

    Information to the code: As you might guess and see, I am doing an integration test and therefore starting an embedded tomcat server in order to test my RESTful webservice. How getting the application context with a "hack" from an embedded tomcat is shown in my post here: Getting Access to Spring with Embedded Tomcat 6

    I am looking forward to your replies.