为什么maven-surefire-plugin跳过带有日志消息的测试“因为它已经为这个配置运行了”?

时间:2015-12-11 13:31:06

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

我无法理解为什么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运行两次并且不想运行我的测试类?如何在我的情况下运行测试?谢谢。

1 个答案:

答案 0 :(得分:2)

鉴于您链接的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元素,因此默认情况下它被称为defaultmaven-surefire-plugin:2.19:test (default))。此执行在Surefire(maven-surefire-plugin:2.19:test (default-test))的开箱即用Maven配置之后运行。因此,您有两次执行(defaultdefault-test)。删除Surefire插件配置中的executions部分,您只能执行一次(default-test)。
  • 您在Surefire配置中也有拼写错误,<include>UtilTest.java</include>配置指向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插件部分,这个问题应该修复。