Spring 集成测试期间重复的bean冲突(多个、合并的上下文?、re:surefire vs failsafe?)
我在应用程序的集成测试阶段遇到一些依赖项注入错误。我认为这是因为spring在单元测试期间启动了一个上下文,然后缓存了用于单元测试的所有模拟bean实例,然后这些实例与集成测试阶段启动的bean冲突,导致类中的DI失败,因为spring正在查找同一bean的多个实例模拟实例与真实实例 注意:此问题仅在应用程序的集成测试阶段出现。该应用程序启动、运行和单元测试通常是一个webapp,我使用tomcat:runwar目标与maven 3一起运行它 这是我的集成测试的基本组成部分。我昨天写了一篇伪造的文章,只是为了试图找出这个测试中没有DI参与的问题:Spring 集成测试期间重复的bean冲突(多个、合并的上下文?、re:surefire vs failsafe?),spring,unit-testing,maven-3,integration-testing,applicationcontext,Spring,Unit Testing,Maven 3,Integration Testing,Applicationcontext,我在应用程序的集成测试阶段遇到一些依赖项注入错误。我认为这是因为spring在单元测试期间启动了一个上下文,然后缓存了用于单元测试的所有模拟bean实例,然后这些实例与集成测试阶段启动的bean冲突,导致类中的DI失败,因为spring正在查找同一bean的多个实例模拟实例与真实实例 注意:此问题仅在应用程序的集成测试阶段出现。该应用程序启动、运行和单元测试通常是一个webapp,我使用tomcat:runwar目标与maven 3一起运行它 这是我的集成测试的基本组成部分。我昨天写了一篇伪造的
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="file:src/main/webapp/WEB-INF/spring/root-context.xml")
@DirtiesContext
public class ITNonsenseTest {
@Test
public void doNothing() {
System.out.println("hello, world");
Assert.assertTrue(true);
}
}
我的集成测试都以前缀IT开头,单元测试都以前缀UT开头
我使用apache surefire进行单元测试,使用apache failsafe进行集成测试:
从my pom.xml:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven.nfire.plugin.version}</version>
<configuration>
<excludes>
<exclude>**/IT*.java</exclude>
</excludes>
<includes>
<include>**/UT*.java</include>
</includes>
<skipTests>
${skipUnitTests}
</skipTests>
</configuration>
<executions>
<execution>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-report-plugin</artifactId>
<version>${maven.nfire.plugin.version}</version>
</plugin>
<!-- For Integration Tests -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>${maven.nfire.plugin.version}</version>
<configuration>
<includes>
<include>**/IT*.java</include>
</includes>
<excludes>
<exclude>**/UT*.java</exclude>
</excludes>
<skipTests>
${skipIntegrationTests}
</skipTests>
</configuration>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
配置classesDirectory可以帮我完成这项工作。看
org.apache.maven.plugins
maven故障保护插件
${project.build.outputDirectory}
首先,看起来您没有阅读文档maven surefire插件和maven failsafe插件都没有导致include/excludes具有您应该遵循的良好默认值。因为您的include/exclude定义的某些部分是默认的,就像许多年后我在Micronaut框架中遇到的一样。你找到解决办法了吗?
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:util="http://www.springframework.org/schema/util"
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/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- Root Context: defines shared resources visible to all other web components -->
<context:property-placeholder location="file://${conf.dir}/*.properties" />
<context:component-scan base-package="com.myapp.models, com.myapp.dao, com.myapp.service" />
<beans:import resource="simplesm-context.xml" />
<beans:import resource="datasources.xml" />
<beans:import resource="rabbit-context.xml" />
<util:properties id="swaggerProps" location="classpath:swagger.properties" />
<util:properties id="encryptionProps" location="classpath:sharedEncryption.properties" />
<aop:aspectj-autoproxy />
<beans:bean name="cacheManager" class="com.google.code.ssm.spring.SSMCacheManager">
<beans:property name="caches">
<beans:set>
<beans:bean class="com.google.code.ssm.spring.SSMCache">
<beans:constructor-arg name="cache" index="0" ref="defaultCache" />
<beans:constructor-arg name="expiration" index="1" value="86400" />
<beans:constructor-arg name="allowClear" index="2" value="false" />
</beans:bean>
</beans:set>
</beans:property>
</beans:bean>
<beans:bean name="defaultCache" class="com.google.code.ssm.CacheFactory">
<!-- <beans:property name="cacheName" value="defaultCache" /> -->
<beans:property name="cacheClientFactory">
<beans:bean name="cacheClientFactory" class="com.google.code.ssm.providers.spymemcached.MemcacheClientFactoryImpl" />
</beans:property>
<beans:property name="addressProvider">
<beans:bean class="com.google.code.ssm.config.DefaultAddressProvider">
<beans:property name="address" value="${ureg.memcached.url}:${ureg.memcached.port}" />
</beans:bean>
</beans:property>
<beans:property name="configuration">
<beans:bean class="com.google.code.ssm.providers.CacheConfiguration">
<beans:property name="consistentHashing" value="true" />
</beans:bean>
</beans:property>
</beans:bean>
</beans:beans>
<properties>
<java-version>1.7</java-version>
<org.springframework-version>4.0.6.RELEASE</org.springframework-version>
<org.springframework.amqp.version>1.3.5.RELEASE</org.springframework.amqp.version>
<org.springframework.data.version>1.6.0.RELEASE</org.springframework.data.version>
<google.simple.spring.memcached.version>3.2.1</google.simple.spring.memcached.version>
<maven.nfire.plugin.version>2.17</maven.nfire.plugin.version>
</properties>
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
@DirtiesContext
public class UTAdditionalDataPointsServiceTest {
@Configuration
static class AdditionalDataPointsServiceUnitTestContextConfiguration {
@Bean
@Qualifier("props")
public Properties getProperties() {
return Mockito.mock(Properties.class);
}
@Bean
@Qualifier("npJdbcTemplate")
public NamedParameterJdbcTemplate getNamedParameterJdbcTemplate() {
return Mockito.mock(NamedParameterJdbcTemplate.class);
}
@Bean
@Qualifier("uJdbcTemplate")
public JdbcTemplate getJdbcTemplate() {
return Mockito.mock(JdbcTemplate.class);
}
@Bean
public AdditionalDataPointsDao getAdditionalDataPointsDao() {
return Mockito.mock(AdditionalDataPointsDaoImpl.class);
}
@Bean
public AdditionalDataPointService getAdditionalDataPointService() {
return Mockito.mock(AdditionalDataPointServiceImpl.class);
}
}
@Inject
AdditionalDataPointService adpService;
@Test
public void testGetAdditionalDataPoints() throws Exception {
List<AdditionalDataPoint> adpList = adpService.getAdditionalDataPoints(1);
org.junit.Assert.assertNotNull(adpList);
}
nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [java.util.Properties] is defined: expected single matching bean but found 2: realInstanceBeanName,mockInstanceBeanNameGetter