2011-01-06 48 views
0

我已成立了一个Java/Maven项目,以便进行测试,这种方式:maven2 - 为什么failesafe插件忽略了我的junit注释?

  • 单元测试与神火插件
  • 集成测试与故障安全插件

执行的处理这里是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测试类,看起来像那样(再次丑陋的comp ACT格式):

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的注解?

回答

1

从配置中删除

<junitArtifactName>none:none</junitArtifactName> 

。它强制Surefire以Junit3模式运行。

+0

在提供的pom示例中,您可以在构建部分找到failsafe插件声明。而在mvn输出中,有一个故障安全部分... – Guillaume 2011-01-06 15:32:41