【问题标题】:Why maven-surefire-plugin skip tests with log message "because it has already been run for this configuration"?为什么 maven-surefire-plugin 跳过带有日志消息的测试“因为它已经为此配置运行”?
【发布时间】:2015-12-11 13:31:06
【问题描述】:

我不明白为什么 maven-surefire-plugin 不运行 jUnit4 测试。我的 pom 是(无法在此处添加,因为“看起来帖子主要是代码”):http://pastebin.com/Jj3iJZpY

当我执行mvn clean test cmd 窗口显示:

C:\Users\maya\git\services>mvn clean test
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building services 1.0.18
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ services ---
[INFO] Deleting C:\Users\maya\git\services\target
[INFO]
[INFO] --- maven-mule-plugin:1.9:attach-test-resources (default-attach-test-resources) @ services ---
[INFO] attaching test resource C:\Users\maya\git\services\src\main\app
[INFO]
[INFO] --- build-helper-maven-plugin:1.7:add-resource (add-resource) @ services ---
[INFO]
[INFO] --- maven-resources-plugin:2.7:resources (default-resources) @ services ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 4 resources
[INFO] Copying 3 resources
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-mule-plugin:1.9:filter-resources (default-filter-resources) @ services ---
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ services ---
[INFO] Compiling 60 source files to C:\Users\maya\git\services\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.7:testResources (default-testResources) @ services ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO] Copying 3 resources
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ services ---
[INFO] Compiling 1 source file to C:\Users\maya\git\services\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.19:test (default-test) @ services ---
[INFO]
[INFO] --- maven-surefire-plugin:2.19:test (default) @ services ---
[INFO] Skipping execution of surefire because it has already been run for this configuration
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 8.554 s
[INFO] Finished at: 2015-12-11T15:48:05+03:00
[INFO] Final Memory: 48M/312M
[INFO] ------------------------------------------------------------------------

测试类是:

  package com.comp.utils.UtilsTest;

    import static org.junit.Assert.assertTrue;
    import org.apache.log4j.Logger;
    import org.junit.Test;



    public class UtilsTest {
         private static final Logger LOG = Logger.getLogger(UtilsTest.class.getName());


        @Test
        public void testHasPersonSameProd() {


             boolean hasSameProduct = false;

            assertTrue("Should be True", hasSameProduct);
        }
    }

为什么 maven-surefire-plugin:2.19 运行两次并且不想运行我的测试类?如何在我的情况下运行测试?谢谢。

【问题讨论】:

    标签: java maven mule junit4 maven-surefire-plugin


    【解决方案1】:

    鉴于您链接的 pom(实际上应该包含在问题中,因为将来链接可能会断开):

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.19</version>
        <executions>
                <execution>
                        <goals>
                                <goal>test</goal>
                        </goals>
                </execution>
        </executions>
    
    
        <dependencies>
                <dependency>
                        <groupId>org.apache.maven.surefire</groupId>
                        <artifactId>surefire-junit47</artifactId>
                        <version>2.19</version>
                </dependency>
        </dependencies>
    
        <configuration>
                <includes>
                        <include>UtilTest.java</include>
                </includes>
        </configuration>
    </plugin>
    
    • Maven Surefire 插件运行两次,因为您配置了插件的额外执行,但未提供 id 元素,因此默认情况下它称为 default (maven-surefire-plugin:2.19:test (default))。此执行在 Surefire (maven-surefire-plugin:2.19:test (default-test)) 的开箱即用 Maven 配置之后运行。因此,因此,您有两次执行(defaultdefault-test)。删除 Surefire 插件配置中的 executions 部分,您将只有一次执行(default-test)。
    • 您在 Surefire 配置中也有一个错字,&lt;include&gt;UtilTest.java&lt;/include&gt; 配置指向 UtilTest.java 类,而在您的问题中它被命名为 UtilsTest(注意附加的 's')。
    • 如果测试类位于src/test/java 文件夹下,则无需配置它的包含,因为它也已遵循 Surefire 的默认约定 "**/*Test.java"
    • 您收到的消息 (Skipping execution of surefire because it has already been run for this configuration) 是因为 Surefire 插件的 configuration 元素位于任何 executions 元素之外,这意味着适用于所有插件执行,即使是默认的 (default-test) .

    所以你可能会从你的 pom 中删除整个 Surefire 插件部分,问题应该得到解决。

    【讨论】:

    • 谢谢。错误在&lt;include&gt;UtilTest.java&lt;/include&gt;。应该是 UtilTests.java
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-25
    相关资源
    最近更新 更多