【问题标题】:Exclude maven dependency for one specific unit test排除一个特定单元测试的 Maven 依赖项
【发布时间】:2013-04-15 15:53:21
【问题描述】:

我想删除单元测试的依赖项。我找到了怎么做in this answer

但我只想删除一个特定测试的依赖项,而不是我的所有测试。有没有办法做到这一点?

【问题讨论】:

  • 我不这么认为,因为依赖项是针对 maven 模块的。如果您只需要一个测试,您可能需要将此测试移至单独的 Maven 模块。但这听起来像是集成测试,而不是单元测试。可能您可以详细说明一下或发布您的 pom 文件吗?
  • 确实它更像是一个集成测试,它是在缺少特定依赖项时测试我的应用程序的行为。
  • 您应该将该测试分离到一个单独的 Maven 模块中。
  • 那你需要为每个集成测试创建一个模块?!

标签: java unit-testing maven junit


【解决方案1】:

不是通过使用一次 Surefire 执行。

您必须定义 Surefire 插件的两种执行方式:一种包含用于大多数测试的完整 Classpath,另一种包含用于需要它的单个测试的专用 Classpath。

关注 Surefire 插件的文档:http://maven.apache.org/surefire/maven-surefire-plugin/examples/inclusion-exclusion.html

您必须创建两个执行,并将它们都绑定到test 阶段。使用以下示例作为骨架(您必须调整 includeexclude 模式,以及排除的 Classpath 工件):

<plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <executions>
            <execution>
                <id>full-cp</id>
                <phase>test</phase>
                <goals>
                    <goal>test</goal>
                </goals>
                <configuration>
                    <includes>
                        <include>**/Test*.java</include>
                    </includes>
                    <excludes>
                        <exclude>MyFancyTest.java</exclude>
                    </excludes>
                </configuration>
            </execution>
            <execution>
                <id>special-cp</id>
                <phase>test</phase>
                <goals>
                    <goal>test</goal>
                </goals>
                <configuration>
                    <includes>
                        <include>MyFancyTest.java</include>
                    </includes>
                    <classpathDependencyExcludes>
                        <classpathDependencyExcludes>excluded-artifact</classpathDependencyExcludes>
                    </classpathDependencyExcludes>
                </configuration>
            </execution>
        </executions>
    </plugin>
</plugins>

【讨论】:

  • Surefire 插件不适用于集成测试。集成测试应该由 maven-failsafe-plugin 运行。
  • 非常感谢。虽然 khmarbaise 是对的,但 Isaac 还是回答了我的问题。我将研究 maven-failsafe-plugin。但是文档看起来很糟糕:p
猜你喜欢
  • 2012-08-16
  • 1970-01-01
  • 2012-02-25
  • 2012-05-27
  • 1970-01-01
  • 2014-10-20
  • 2012-04-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多