maven2 - 为什么failesafe插件忽略我的junit注释?

发布于 2024-10-10 12:04:39 字数 5196 浏览 8 评论 0原文

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

  • 使用surefire插件执行单元测试
  • ,使用failsafe插件执行集成测试,

这里是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注释?

I have set up a java/maven project in order to perform tests this way:

  • unit tests are executed with the surefire plugin
  • integration tests are executed with the failsafe plugin

here is the POM (ugly compact formating):

<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>

I have a sample UNIT test class looking like that (again ugly compact format) :

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");}
}

I have the exact same Integration test:

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");}
}

And maven output is:

$ 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

...

Question: why failsafe integration tests totaly ignore my Junit annotation ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤者何惧 2024-10-17 12:04:39

删除

<junitArtifactName>none:none</junitArtifactName> 

从配置中 。它强制 Surefire 在 Junit3 模式下运行。

Remove

<junitArtifactName>none:none</junitArtifactName> 

from the configuration. It forces Surefire to run in Junit3 mode.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文