Announcement Announcement Module
Collapse
No announcement yet.
Correct usage of AnnotationConfigUtils in subclassed ContextLoader Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • 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:

    Code:
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(loader=MyContextLoader.class)
    @Transactional
    public class MyTest {
        @Autowired
        public AccountDao accountDao;
    }
    The implementation of my ContextLoader is as follows:

    Code:
    public class MyContextLoader implements ContextLoader {
    
        @Override
        public String[] processLocations(Class<?> clazz, String... locations) {
            return locations;
        }
    
        @Override
        public ApplicationContext loadContext(String... locations) throws Exception {
            try {
                // Start Embedded Tomcat
                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);
    
                //context.refresh();
                //context.registerShutdownHook();
    
                return context;
            }
            catch(Exception e) {
                e.printStackTrace();
                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.

    Heinrich
Working...
X