Announcement Announcement Module
Collapse
No announcement yet.
Help-JUnit test - Caught exception while allowing TestExecutionListener Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Help-JUnit test - Caught exception while allowing TestExecutionListener

    Hi,
    this is first time I'm trying to use Spring.
    I have a problem with dependency injection in my simple test class and got this error:


    14:15:07.833 [main]
    14:15:07.838 [main] ERROR o.s.test.context.TestContextManager - Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.Dependenc yInjectionTestExecutionListener@32955cd5] to prepare test instance [at.repromedia.central.service.test.EmailAddressSer viceTest@6e811c88]
    org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'at.repromedia.central.service.test.EmailAddressSe rviceTest': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationExce ption: Could not autowire field: private at.repromedia.central.service.EmailAddressService at.repromedia.central.service.test.EmailAddressSer viceTest.emailAddressService; nested exception is org.springframework.beans.factory.NoSuchBeanDefini tionException: No matching bean of type [at.repromedia.central.service.EmailAddressService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Aut owired(required=true)}
    at org.springframework.beans.factory.annotation.Autow iredAnnotationBeanPostProcessor.postProcessPropert yValues(AutowiredAnnotationBeanPostProcessor.java: 287) ~[spring-beans-3.1.1.RELEASE.jar:3.1.1.RELEASE]
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.populateBean(AbstractAu towireCapableBeanFactory.java:1106) ~[spring-beans-3.1.1.RELEASE.jar:3.1.1.RELEASE]
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.autowireBeanProperties( AbstractAutowireCapableBeanFactory.java:374) ~[spring-beans-3.1.1.RELEASE.jar:3.1.1.RELEASE]
    at org.springframework.test.context.support.Dependenc yInjectionTestExecutionListener.injectDependencies (DependencyInjectionTestExecutionListener.java:110 ) ~[spring-test-3.1.1.RELEASE.jar:3.1.1.RELEASE]
    at org.springframework.test.context.support.Dependenc yInjectionTestExecutionListener.prepareTestInstanc e(DependencyInjectionTestExecutionListener.java:75 ) ~[spring-test-3.1.1.RELEASE.jar:3.1.1.RELEASE]
    at org.springframework.test.context.TestContextManage r.prepareTestInstance(TestContextManager.java:321) ~[spring-test-3.1.1.RELEASE.jar:3.1.1.RELEASE]
    at org.springframework.test.context.junit4.SpringJUni t4ClassRunner.createTest(SpringJUnit4ClassRunner.j ava:211) [spring-test-3.1.1.RELEASE.jar:3.1.1.RELEASE]
    at org.springframework.test.context.junit4.SpringJUni t4ClassRunner$1.runReflectiveCall(SpringJUnit4Clas sRunner.java:288) [spring-test-3.1.1.RELEASE.jar:3.1.1.RELEASE]
    at org.junit.internal.runners.model.ReflectiveCallabl e.run(ReflectiveCallable.java:15) [junit-4.8.1.jar:na]
    at org.springframework.test.context.junit4.SpringJUni t4ClassRunner.methodBlock(SpringJUnit4ClassRunner. java:290) [spring-test-3.1.1.RELEASE.jar:3.1.1.RELEASE]
    at org.springframework.test.context.junit4.SpringJUni t4ClassRunner.runChild(SpringJUnit4ClassRunner.jav a:231) [spring-test-3.1.1.RELEASE.jar:3.1.1.RELEASE]
    at org.junit.runners.BlockJUnit4ClassRunner.runChild( BlockJUnit4ClassRunner.java:50) [junit-4.8.1.jar:na]
    at org.junit.runners.ParentRunner$3.run(ParentRunner. java:193) [junit-4.8.1.jar:na]
    at org.junit.runners.ParentRunner$1.schedule(ParentRu nner.java:52) [junit-4.8.1.jar:na]
    at org.junit.runners.ParentRunner.runChildren(ParentR unner.java:191) [junit-4.8.1.jar:na]
    at org.junit.runners.ParentRunner.access$000(ParentRu nner.java:42) [junit-4.8.1.jar:na]
    at org.junit.runners.ParentRunner$2.evaluate(ParentRu nner.java:184) [junit-4.8.1.jar:na]
    at org.springframework.test.context.junit4.statements .RunBeforeTestClassCallbacks.evaluate(RunBeforeTes tClassCallbacks.java:61) [spring-test-3.1.1.RELEASE.jar:3.1.1.RELEASE]
    at org.springframework.test.context.junit4.statements .RunAfterTestClassCallbacks.evaluate(RunAfterTestC lassCallbacks.java:71) [spring-test-3.1.1.RELEASE.jar:3.1.1.RELEASE]
    at org.junit.runners.ParentRunner.run(ParentRunner.ja va:236) [junit-4.8.1.jar:na]
    at org.springframework.test.context.junit4.SpringJUni t4ClassRunner.run(SpringJUnit4ClassRunner.java:174 ) [spring-test-3.1.1.RELEASE.jar:3.1.1.RELEASE]
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestR eference.run(JUnit4TestReference.java:50) [.cp/:na]
    at org.eclipse.jdt.internal.junit.runner.TestExecutio n.run(TestExecution.java:38) [.cp/:na]
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.runTests(RemoteTestRunner.java:467) [.cp/:na]
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.runTests(RemoteTestRunner.java:683) [.cp/:na]
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.run(RemoteTestRunner.java:390) [.cp/:na]
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.main(RemoteTestRunner.java:197) [.cp/:na]
    Caused by: org.springframework.beans.factory.BeanCreationExce ption: Could not autowire field: private at.repromedia.central.service.EmailAddressService at.repromedia.central.service.test.EmailAddressSer viceTest.emailAddressService; nested exception is org.springframework.beans.factory.NoSuchBeanDefini tionException: No matching bean of type [at.repromedia.central.service.EmailAddressService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Aut owired(required=true)}
    at org.springframework.beans.factory.annotation.Autow iredAnnotationBeanPostProcessor$AutowiredFieldElem ent.inject(AutowiredAnnotationBeanPostProcessor.ja va:506) ~[spring-beans-3.1.1.RELEASE.jar:3.1.1.RELEASE]
    at org.springframework.beans.factory.annotation.Injec tionMetadata.inject(InjectionMetadata.java:87) ~[spring-beans-3.1.1.RELEASE.jar:3.1.1.RELEASE]
    at org.springframework.beans.factory.annotation.Autow iredAnnotationBeanPostProcessor.postProcessPropert yValues(AutowiredAnnotationBeanPostProcessor.java: 284) ~[spring-beans-3.1.1.RELEASE.jar:3.1.1.RELEASE]
    ... 26 common frames omitted
    Caused by: org.springframework.beans.factory.NoSuchBeanDefini tionException: No matching bean of type [at.repromedia.central.service.EmailAddressService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Aut owired(required=true)}
    at org.springframework.beans.factory.support.DefaultL istableBeanFactory.raiseNoSuchBeanDefinitionExcept ion(DefaultListableBeanFactory.java:924) ~[spring-beans-3.1.1.RELEASE.jar:3.1.1.RELEASE]
    at org.springframework.beans.factory.support.DefaultL istableBeanFactory.doResolveDependency(DefaultList ableBeanFactory.java:793) ~[spring-beans-3.1.1.RELEASE.jar:3.1.1.RELEASE]
    at org.springframework.beans.factory.support.DefaultL istableBeanFactory.resolveDependency(DefaultListab leBeanFactory.java:707) ~[spring-beans-3.1.1.RELEASE.jar:3.1.1.RELEASE]
    at org.springframework.beans.factory.annotation.Autow iredAnnotationBeanPostProcessor$AutowiredFieldElem ent.inject(AutowiredAnnotationBeanPostProcessor.ja va:478) ~[spring-beans-3.1.1.RELEASE.jar:3.1.1.RELEASE]
    ... 28 common frames omitted

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(loader = AnnotationConfigContextLoader.class)
    public class EmailAddressServiceTest {

    // this bean will be injected into the EmailAddressServiceTest class
    @Bean
    public EmailAddressService emailAddressService() {

    final EmailAddressService emailAddressService = new EmailAddressServiceImpl();
    // personService
    final PersonService personService = mock(PersonService.class);
    ReflectionTestUtils.setField(emailAddressService, "personService", personService);
    // domainDao
    final DomainDao domainDao = mock(DomainDao.class);
    ReflectionTestUtils.setField(emailAddressService, "domainDao", domainDao);
    // emailAddressDao
    final EmailAddressDao emailAddressDao = mock(EmailAddressDao.class);
    ReflectionTestUtils.setField(emailAddressService, "emailAddressDao", emailAddressDao);
    // emailAccountDao
    final EmailAccountDao emailAccountDao = mock(EmailAccountDao.class);
    ReflectionTestUtils.setField(emailAddressService, "emailAccountDao", emailAccountDao);
    // emailAccountDao
    final PersonAccountDao personAccountDao = mock(PersonAccountDao.class);
    ReflectionTestUtils.setField(emailAddressService, "personAccountDao", personAccountDao);

    // set properties, etc.
    return emailAddressService;
    }

    @Autowired
    private EmailAddressService emailAddressService;

    @Test
    public void testEmailAddressService() {
    // test the transferService
    }

    }



    @Service("emailAddressService")
    public class EmailAddressServiceImpl implements EmailAddressService {

    private final static Logger logger = LoggerFactory.getLogger(EmailAddressServiceImpl.cl ass);

    @Autowired
    private DomainDao domainDao;

    @Autowired
    private EmailAddressDao emailAddressDao;

    @Autowired
    private EmailAccountDao emailAccountDao;

    @Autowired
    private PersonAccountDao personAccountDao;

    @Autowired
    private PersonService personService;

    .
    .
    .
    .
    .
    .
    .
    .
    .

    I try to use these examples for my test cases

    http://mrhaki.blogspot.com/2010/09/p...ns.htmlhttp://


    http://www.swiftmind.com/de/2011/06/...-and-profiles/

  • #2
    Hi seifi,

    First, please wrap your code in [ CODE ][/ CODE ] tags (not in QUOTE tags) so that people can read it.

    Second, you cannot just annotate a method with @Bean directly in your test class. @Bean methods must be in an @Configuration class, either in an external class or in a nested static class. Please review the examples on my blog again for working examples.

    For convenience, here's the example with the nested static class:
    Code:
    package com.example;
    
    @RunWith(SpringJUnit4ClassRunner.class)
    // ApplicationContext will be loaded from the static inner ContextConfiguration class
    @ContextConfiguration
    public class OrderServiceTest {
    
        @Configuration
        static class ContextConfiguration {
    
            // this bean will be injected into the OrderServiceTest class
            @Bean
            public OrderService orderService() {
                OrderService orderService = new OrderServiceImpl();
                // set properties, etc.
                return orderService;
            }
        }
    
        @Autowired
        private OrderService orderService;
    
        @Test
        public void testOrderService() {
            // test the orderService
        }
    }
    Note that you do not need to declare loader=AnnotationConfigContextLoader.class.

    More importantly, the test you're writing is actually a unit test since you are mocking all of the dependencies. Thus there is zero benefit to using the Spring TestContext Framework here. If you're not using Spring to configure your SUT (subject under test -- i.e., the EmailAddressServiceImpl in this case), then you should just write a plain JUnit test.

    Regards,

    Sam

    Comment


    • #3
      Also, autowiring private fields in your components (e.g., in EmailAddressServiceImpl) is a bad practice regarding testability.

      Best practices include either autowiring a constructor or setter methods. Then you can unit test your component without the need for ReflectionTestUtils.

      Regards,

      Sam

      Comment

      Working...
      X