Spring:单元和集成测试

Puc*_*uce 8 java spring integration-testing maven-2 unit-testing

我正在寻找使用Spring设置单元和集成测试的最佳实践.

我通常使用3种测试:

  • "真正的"单元测试(没有依赖)
  • 测试作为"单元"测试(内存数据库,本地调用,模拟对象,......)或集成测试(持久数据库,远程调用,...)运行
  • 测试仅作为集成测试运行

目前我只有第二类测试,这是棘手的部分.我设置了一个基础测试类,如:

@ContextConfiguration(locations = { "/my_spring_test.xml" })
public abstract class AbstractMyTestCase extends AbstractJUnit4SpringContextTests
Run Code Online (Sandbox Code Playgroud)

而"单位"测试如下:

public class FooTest extends AbstractMyTestCase
Run Code Online (Sandbox Code Playgroud)

使用自动装配的属性.

在不同(集成测试)环境中运行测试的最佳方法是什么?对测试进行子类化并覆盖ContextConfiguration?

@ContextConfiguration(locations = { "/my_spring_integration_test.xml" })
public class FooIntegrationTest extends FooTest
Run Code Online (Sandbox Code Playgroud)

这会有用吗(我目前无法在这里轻松测试)?这种方法的问题是"@ContextConfiguration(locations = {"/ my_spring_integration_test.xml"})"重复了很多.

有什么建议?

此致,弗洛里安

Sea*_*oyd 2

我会选择这个版本:

ContextConfiguration(locations = { "/my_spring_test.xml" })
public abstract class AbstractMyTestCase extends AbstractJUnit4SpringContextTests
Run Code Online (Sandbox Code Playgroud)

在 中my_spring_test.xml,我会使用该PropertyPlaceHolderConfigurer机制。

JPA 示例:

<context:property-placeholder
    system-properties-mode="OVERRIDE" 
    location="classpath:test.properties" />

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close">
    <property name="driverClassName" value="${test.database.driver}" />
    <property name="url" value="${test.database.server}" />
    <property name="username" value="${test.database.user}" />
    <property name="password" value="${test.database.password}" />
</bean>

<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceUnitName" value="test" />
    <property name="dataSource" ref="dataSource" />
    <property name="persistenceXmlLocation"
             value="classpath:META-INF/persistence.xml" />
    <property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
    <property name="showSql" value="false" />
    <property name="generateDdl" value="${test.database.update}" />
    <property name="database" value="${test.database.databasetype}" />
</bean>
    </property>
</bean>
Run Code Online (Sandbox Code Playgroud)

现在您需要做的就是在类路径上有不同版本的 test.properties 以进行内存中和实际集成测试(当然,需要存在相应的驱动程序类)。您甚至可以设置系统属性来覆盖属性值。


如果你想用maven来准备这个,你会发现用maven复制文件并不简单。您将需要一种执行代码的方法,标准选择是maven-antrun-plugingmaven-maven-plugin

无论哪种方式:有两个配置文件,例如在 src/main/config 中,并添加两个插件执行,一个是同相generate-test-resources,一个是同相pre-integration-test。这是 GMaven 版本:

<plugin>
    <groupId>org.codehaus.gmaven</groupId>
    <artifactId>gmaven-plugin</artifactId>
    <version>1.3</version>
    <executions>
        <execution>
            <phase>pre-integration-test</phase>
            <goals>
                <goal>execute</goal>
            </goals>
            <configuration>
            <source>
            new File(
               pom.build.testOutputDirectory,
               "test.properties"
            ).text = new File(
                       pom.basedir,
                       "src/main/config/int-test.properties"
            ).text;
            </source>
            </configuration>
        </execution>
        <execution>
            <phase>generate-test-resources</phase>
            <goals>
                <goal>execute</goal>
            </goals>
            <configuration>
            <source>
            new File(
               pom.build.testOutputDirectory,
               "test.properties"
            ).text = new File(
                       pom.basedir,
                       "src/main/config/memory-test.properties"
            ).text;
            </source>
            </configuration>
        </execution>
    </executions>
</plugin>
Run Code Online (Sandbox Code Playgroud)