如何测试自己拥有自动连接依赖项的Spring服务bean?

时间:2014-05-06 08:18:46

标签: java spring junit spring-test

我想测试一些他们自己包含其他自动服务的服务。但这些"外部"测试本身不需要服务。

如何创建测试设置,例如以下示例?

package de.myapp.service;

@Service
public class MyServiceDelegator {
    @Autowired
    private List<ServiceInterface> services;

    public ServiceInterface delegate(String id) {
        //routine to find the right ServiceInterface based on the given id
    }
}


@Service
public class MyService implements ServiceInterface {

}

@Service
public class MyCustomService implements ServiceInterface {
    //that is the problem during testing
    @Autowired
    private de.myapp.repository.SomeDao dao;
}


@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("applicationContext.xml")
public class ServiceDelegatorTest {
    @Autowired
    private ApplicationContext ac

    @Test
    public void testDelegator() {
        MyServiceDelegator dg = ac.getBean(MyServiceDelegator.class);
        ac.delegate("test");
    }
}

的applicationContext.xml:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan  base-package="de.myapp.service" />
</beans>

问题:包含未在JUnit测试中扫描的软件包(如MyCustomService)的自动连接依赖项的所有服务都会抛出异常:

  

引起:   org.springframework.beans.factory.NoSuchBeanDefinitionException:没有   [SomeDao]类型的限定bean发现依赖:期望在   至少有1个bean有资格作为autowire候选者   依赖。依赖注释:   {@ org.springframework.beans.factory.annotation.Autowired(所需=真)}     在   org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1103)     在   org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:963)     在   org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:858)     在   org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor $ AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:480)     ......还有57个

2 个答案:

答案 0 :(得分:2)

您可以使用Springockito将模拟服务实现添加到测试应用程序上下文中。

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mockito="http://www.mockito.org/spring/mockito"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
                       http://www.springframework.org/schema/beans/spring-beans.xsd
                       http://www.springframework.org/schema/context
                       http://www.springframework.org/schema/context/spring-context.xsd"
                       http://www.mockito.org/spring/mockito
                       http://www.mockito.org/spring/mockito.xsd">

    <context:component-scan  base-package="de.myapp.service" />

    <mockito:mock id="dao" class="de.myapp.repository.SomeDao" />

</beans>

答案 1 :(得分:0)

问题在于,SomeDao未被组件扫描拾取,因为它不在de.myapp.service包下。

您已明确声明组件扫描包是de.myapp.service

我建议你做出以下改变:

<context:component-scan base-package="de.myapp" />

这样de.myapp下的所有代码都有资格进行组件扫描。

如果您想避免在分量扫描中包含所有代码,可以执行以下操作:

<context:component-scan base-package="de.myapp.service, de.myapp.repository" />