如何:针对测试环境(数据库)运行maven集成测试

Jon*_*sso 13 java integration-testing surefire maven maven-jetty-plugin

我正在使用maven和maven-failsafe-plugin在集成测试生命周期阶段启动jetty.然后,我对运行的webapp执行了许多(*IT.java)junit测试.这是按预期工作的.

但是,我想连接到测试数据库进行集成测试.我正在存储它的URL

${basedir}/src/test/resources/jdbc.properties  
Run Code Online (Sandbox Code Playgroud)

当jetty插件运行(jetty:run)时,它会使用

${basedir}/src/main/resources/jdbc.propertes 
Run Code Online (Sandbox Code Playgroud)

代替.我尝试通过classesDirectory属性重新配置jetty插件来使用

${project.build.testOutputDirectory}
Run Code Online (Sandbox Code Playgroud)

但是test-classes目录缺少我实际编译的项目类,以及存储的资源

${basedir}/src/main/resources 
Run Code Online (Sandbox Code Playgroud)

注意:surefire将测试资源添加到类路径中,然后是主资源,这样两者中的任何内容都将使用测试版本,因为它首先在类路径中找到.

有关如何正确设置此设置的任何想法?

谢谢!

编辑:

好吧,似乎jetty-plugin上有配置属性来处理这个问题:

  • testClassesDirectory:包含生成的测试类的目录.
  • useTestClasspath:如果为true,则test的依赖项将首先放在运行时类路径中.

不幸的是,它们不起作用.

这是我的pom.xml的相关部分:

  <testResources>
        <testResource>
            <filtering>true</filtering>
            <directory>src/test/resources</directory>
        </testResource>
    </testResources>
    <plugins>
        <plugin>
            <groupId>org.mortbay.jetty</groupId>
            <artifactId>maven-jetty-plugin</artifactId>
            <version>6.1.26</version>
            <configuration>
                <contextPath>/</contextPath>
                <stopPort>8005</stopPort>
                <stopKey>STOP</stopKey>
            </configuration>
            <executions>
                <execution>
                    <id>start-jetty</id>
                    <phase>pre-integration-test</phase>
                    <goals>
                        <goal>run</goal>
                    </goals>
                    <configuration>
                        <daemon>true</daemon>
                        <useTestClasspath>true</useTestClasspath>
                        <testClassesDirectory>${project.build.testOutputDirectory}</testClassesDirectory>
                    </configuration>
                </execution>
                <execution>
                    <id>stop-jetty</id>
                    <phase>post-integration-test</phase>
                    <goals>
                        <goal>stop</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <artifactId>maven-failsafe-plugin</artifactId>
            <version>2.6</version>
            <executions>
                <execution>
                    <goals>
                        <goal>integration-test</goal>
                        <goal>verify</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <useFile>false</useFile>
            </configuration>
        </plugin>
Run Code Online (Sandbox Code Playgroud)

Mar*_*ker 2

我有同样的问题,并通过使用自定义 web.xml (jettyweb.xml) 解决它,请参阅 Maven 配置

    <build>
    <plugins>

        <plugin>
            <groupId>org.mortbay.jetty</groupId>
            <artifactId>maven-jetty-plugin</artifactId>
            <configuration>
                <overrideWebXml>./src/main/webapp/WEB-INF/jettyweb.xml</overrideWebXml>
                <scanintervalseconds>3</scanintervalseconds>
            </configuration>
            <dependencies>

            </dependencies>
        </plugin>
    </plugins>

</build>
Run Code Online (Sandbox Code Playgroud)

就我而言,我使用此配置来使用其他一些 spring 配置来管理事务。但此策略也可用于使用其他属性文件。

我原来的 web.xml 有这个 spring 配置

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
    /WEB-INF/spring-hibernate.xml,
    /WEB-INF/spring-services.xml
    </param-value>
</context-param>
Run Code Online (Sandbox Code Playgroud)

我的 jettyweb.xml 有这个 spring 配置

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
    /WEB-INF/spring-hibernate-jetty.xml,
    /WEB-INF/spring-services.xml
    </param-value>
</context-param>
Run Code Online (Sandbox Code Playgroud)

这应该会让你走上正确的道路