【问题标题】:maven2 - why failesafe plugin is ignoring my junit annotations?maven2 - 为什么故障安全插件忽略我的junit注释?
【发布时间】:2011-01-06 13:40:32
【问题描述】:

我已经建立了一个 java/maven 项目以便以这种方式执行测试:

  • 单元测试使用surefire插件执行
  • 使用故障安全插件执行集成测试

这里是 POM(丑陋的紧凑格式):

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <groupId>org.sample</groupId>
    <artifactId>sample-service</artifactId>
    <version>0.0.0</version>
    <name>sdp-sample-service</name>

    <build> <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration><debug>true</debug><source>1.6</source><target>1.6</target></configuration>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId><artifactId>maven-surefire-plugin</artifactId><version>2.6</version>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId><artifactId>maven-failsafe-plugin</artifactId><version>2.6</version>
                <executions><execution>
                        <id>integration-test</id>
                        <phase>integration-test</phase>
                        <goals>
                            <goal>integration-test</goal>
                            <goal>verify</goal>
                        </goals>
                        <configuration>
                            <junitArtifactName>none:none</junitArtifactName>
                            <failIfNoTests>false</failIfNoTests>
                            <testFailureIgnore>true</testFailureIgnore>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.8.2</version><scope>test</scope></dependency>                    
    </dependencies>

</project>

我有一个类似这样的示例 UNIT 测试类(又是丑陋的紧凑格式):

package org.sample;import java.util.logging.Logger;import org.junit.*;
public class SampleUnitTest {
    private static final Logger LOG = Logger.getLogger("SampleUnitTest");    
    @BeforeClass public static void beforeClass() {LOG.info("@BeforeClass");}    
    @Before  public void before() {LOG.info("@Before");}    
    @AfterClass public static void afterClass() { LOG.info("@AfterClass");}    
    @After public  void after() { LOG.info("@After"); }    
    @Test public void test1() { LOG.info("test1");}
    @Test public void test2() { LOG.info("test2");}
}

我有完全相同的集成测试:

package org.sample;import java.util.logging.Logger;import org.junit.*;
public class SampleIT {
    private static final Logger LOG = Logger.getLogger("SampleIT");
    @BeforeClass public static void beforeClass() {LOG.info("@BeforeClass");}
    @Before  public void before() {LOG.info("@Before");}
    @AfterClass public static void afterClass() { LOG.info("@AfterClass");}
    @After public  void after() { LOG.info("@After"); }
    @Test public void test1() { LOG.info("test1");}
    @Test public void test2() { LOG.info("test2");}
}

而maven的输出是:

$ mvn clean install
...
[INFO] [surefire:test {execution: default-test}]
...
-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running org.sample.SampleUnitTest
6 janv. 2011 14:38:38 org.sample.SampleUnitTest beforeClass
INFO: @BeforeClass
6 janv. 2011 14:38:38 org.sample.SampleUnitTest before
INFO: @Before
6 janv. 2011 14:38:38 org.sample.SampleUnitTest test1
INFO: test1
6 janv. 2011 14:38:38 org.sample.SampleUnitTest after
INFO: @After
6 janv. 2011 14:38:38 org.sample.SampleUnitTest before
INFO: @Before
6 janv. 2011 14:38:38 org.sample.SampleUnitTest test2
INFO: test2
6 janv. 2011 14:38:38 org.sample.SampleUnitTest after
INFO: @After
6 janv. 2011 14:38:38 org.sample.SampleUnitTest afterClass
INFO: @AfterClass
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.062 sec

Results :

Tests run: 2, Failures: 0, Errors: 0, Skipped: 0

...

[INFO] [failsafe:integration-test {execution: integration-test}]
...
-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running org.sample.SampleIT
6 janv. 2011 14:38:38 org.sample.SampleIT test1
INFO: test1
6 janv. 2011 14:38:38 org.sample.SampleIT test2
INFO: test2
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.015 sec

Results :

Tests run: 2, Failures: 0, Errors: 0, Skipped: 0

...

问题:为什么故障安全集成测试完全忽略了我的 Junit 注释?

【问题讨论】:

    标签: maven-2 integration-testing maven-failsafe-plugin


    【解决方案1】:

    删除

    <junitArtifactName>none:none</junitArtifactName> 
    

    来自配置。它强制 Surefire 在 Junit3 模式下运行。

    【讨论】:

    • 在提供的 pom 示例中,您可以在构建部分找到故障安全插件声明。在 mvn 输出中,有一个故障安全部分...
    • 抱歉,我的苹果移动设备#fail 上的部分被截断了。新的答案应该更好;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-23
    • 1970-01-01
    • 2013-10-17
    • 1970-01-01
    • 2014-04-06
    • 2016-03-08
    • 2014-12-05
    相关资源
    最近更新 更多