Menu

Unitils tests fail on Sonar

Users
2012-07-17
2015-05-04
  • Thierry LER

    Thierry LER - 2012-07-17

    Hello,

    Using Java 6, Unitils 3.3, I wrote a test :

    @SpringApplicationContext("test-applicationContext.xml")
    @DataSet
    public class MyServiceTest extends UnitilsJUnit4 {
      @SpringBeanByName
      private MyService myService;
      ...
    
      @Test
      public void testFoo() {
        myService.foo(...)
    ...
    

    This test works on Eclipse and on Maven (mvn test).

    But the test fails on Sonar :-( I do not understand why…

    Here is the error msg :

    Unable to create application context for locations [test-applicationContext.xml]
    org.unitils.core.UnitilsException: Unable to create application context for locations [test-applicationContext.xml]
    at org.unitils.spring.util.ApplicationContextManager.createInstanceForValues(ApplicationContextManager.java:121)
    at org.unitils.spring.util.ApplicationContextManager.createInstanceForValues(ApplicationContextManager.java:36)
    at org.unitils.core.util.AnnotatedInstanceManager.getInstanceImpl(AnnotatedInstanceManager.java:234)
    at org.unitils.core.util.AnnotatedInstanceManager.getInstance(AnnotatedInstanceManager.java:121)
    at org.unitils.spring.util.ApplicationContextManager.getApplicationContext(ApplicationContextManager.java:65)
    at org.unitils.spring.SpringModule.getApplicationContext(SpringModule.java:235)
    at org.unitils.spring.SpringModule$1.isApplicableFor(SpringModule.java:108)
    ...
    Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.foo.MyService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:924)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:793)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:707)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:478)
    

    Please help…

     
  • Jeroen Horemans

    Jeroen Horemans - 2012-07-17

    Probably your wiring goes wrong.
    ->Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching …
    This is the real cause of the exception
    So check the classpath and the compontent scanning

    My best gues is a classpath issue. If you would write the test the "old fashion" way (create the application context yourself) you will see that you get the same exception.

     
  • Thierry LER

    Thierry LER - 2012-07-17

    But it works on Eclipse and on Maven…

    What do you mean by old fashion ? I allready use the test-applicationContext.xml…

    But it might be not enough

     
  • Jeroen Horemans

    Jeroen Horemans - 2012-07-17

    when you add a normal Junit test, without unitils/default Junit Runner

    something like this :

    @Test
    public void oldFashionBeanFetchingTest(){
            ApplicationContext context = new ClassPathXmlApplicationContext("test-ApplicationContext.xml");
            MyService myService = (MyService) mycontext.getBean("myServiceId");
           Assert.assertNotNull(myService);
    }

    You will probably get the same error.

     
  • Thierry LER

    Thierry LER - 2012-07-18

    Hello,

    I've tried something else. It might help.  It works on Eclipse and Maven (command line) but not on Sonar :-(

    import org.junit.Assert;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration( locations= {"/test-applicationContext.xml"} )
    public class AutowiredTest {
        @Autowired
        private UserService userService;
        @Test
        public void testTrue() {
            Assert.assertTrue(true);
            System.out.println(userService);
            Assert.assertNotNull(userService);
        }
    }
    
     
  • Thierry LER

    Thierry LER - 2012-07-18

    Also works on Eclipse/Maven but not on Sonar with just that :

    @ContextConfiguration("/test-applicationContext.xml")
    
     
  • Jeroen Horemans

    Jeroen Horemans - 2012-07-18

    So probably a classpath issue when running on the sonar, not all your libraries are correctly loaded.
    You'll need to search in that direction. Put op some extra debug info and check what jar's are loaded when running the tests.

     
  • Thierry LER

    Thierry LER - 2012-07-18

    So, I just tested your proposal :

    public class NotAutowiredTest {
        @Test
        public void testOldFashionBeanFetching() {
            final ApplicationContext context = new ClassPathXmlApplicationContext("/test-applicationContext.xml");
            final UserService localUserService = (UserService) context.getBean("userService");
            System.out.println(localUserService);
            Assert.assertNotNull(localUserService);
        }
    }
    

    It works on Eclipse/Maven but still not on Sonar :

    Error creating bean with name 'userService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.foo.SiteService com.foo.UserService.siteService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.foo.SiteService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.foo.SiteService 
    ...
    Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.foo.SiteService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:924)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:793)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:707)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:478)
    

    By the way, my services are :

    @Service
    public class UserService {
    
        @Autowired
        private UserDao userDao;
        @Autowired
        private SiteService siteService;
         ....
    
    @Service
    public class SiteService {
        @Autowired
        private SiteDao siteDao;
        ...
    
     
  • mtvgn

    mtvgn - 2013-06-24

    For future readers having the same problem a solution :-)

    I had the same problem until a colleague came with a solution, instead of defining a controller that I wanted to be autowired for testing as

    <bean id="administrationController" class="<domain>.web.controller.AdministrationController" />

    define it as

    <bean id="administrationController" class="<domain>.web.controller.AdministrationController" >
    <aop:scoped-proxy proxy-target-class="true"/>
    </bean>

     
  • Anonymous

    Anonymous - 2015-04-30

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'Controller': Injection of autowired dependencies failed; nested exception is

     
  • Anonymous

    Anonymous - 2015-04-30

    could anybody help me.

     

Anonymous
Anonymous

Add attachments
Cancel





Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.