【问题标题】:Why @Ignore annotation doesn't ignore the annotated test?为什么@Ignore 注释不会忽略带注释的测试?
【发布时间】:2016-03-08 03:15:44
【问题描述】:

同事们,我的测试类AppTest.java.中有@Ignore注解

import java.math.BigInteger;
import java.util.UUID;
    
import org.fluttercode.datafactory.impl.DataFactory;
import org.junit.Ignore;
import org.junit.Test;
    
import junit.framework.TestCase;
    
public class AdapterAppTest extends TestCase {    
    @Ignore("this test is not ready yet")
        public static void testCreateApplicationWithAllRequiredParameters() {
            
            AdapterApp.setWsURL(URL);
        
            AdapterApp adapterApp = new AdapterApp(); 
            System.out.println("Set UP Request Parameters");
            
            DataFactory df = new DataFactory();
            
            adapterApp.setRequestTimestamp("2015-12-01T12:12:12.123"));
            adapterApp.setRequestUid(UUID.randomUUID().toString());
            adapterApp.setProductType("0");
            
            
            String applicationNum = adapterApp.createApplication();
            assertEquals("2 symb", 2, applicationNum.length());
        } 
}

我的 pom 看起来:

<dependencies>
     

    <!-- Spring framework -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.2.3.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>4.2.3.RELEASE</version>
    </dependency>


    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
    </dependency>


    <dependency>
        <groupId>io.codearte.jfairy</groupId>
        <artifactId>jfairy</artifactId>
        <version>0.5.1</version>
    </dependency>


    <dependency>
        <groupId>org.fluttercode.datafactory</groupId>
        <artifactId>datafactory</artifactId>
        <version>0.8</version>
    </dependency>


    <dependency>
        <groupId>org.springframework.ws</groupId>
        <artifactId>spring-ws-core</artifactId>
        <version>2.2.3.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
    </dependency>

    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.5.1</version>
    </dependency>



    <dependency>
        <groupId>xerces</groupId>
        <artifactId>xercesImpl</artifactId>
        <version>2.11.0</version>
    </dependency>



</dependencies>

<build>


    <plugins>


        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.3</version>
            <configuration>
                <source>${jdk.version}</source>
                <target>${jdk.version}</target>
                <encoding>UTF-8</encoding>
            </configuration>
        </plugin>


        <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>AdapterAppTest.java</include>
                </includes>
            </configuration>
        </plugin>




        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.6</version>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>com.adapter</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>



        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>com.adapter</mainClass>
                    </manifest>
                </archive>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
        </plugin>


        <plugin>
            <groupId>org.jvnet.jaxb2.maven2</groupId>
            <artifactId>maven-jaxb2-plugin</artifactId>
            <version>0.13.0</version>
            <executions>
                <execution>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <schemaLanguage>WSDL</schemaLanguage>
                <generatePackage>com.adapter_entities</generatePackage>
                <schemas>
                    <schema>
                        <url>src/main/resources/Import.wsdl</url>
                    </schema>
                </schemas>

            </configuration>
        </plugin>



    </plugins>
</build>

结果我收到了:

[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building adapter 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-jaxb2-plugin:0.13.0:generate (default) @ adapter ---
[INFO] Up-to-date check for source resources 
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ adapter ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 3 resources
[INFO] Copying 1 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.3:compile (default-compile) @ adapter ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 25 source files to C:\Users\git\adapter\adapter\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ adapter ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\Users\git\adapter\adapter\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.3:testCompile (default-testCompile) @ adapter ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.19:test (default-test) @ adapter ---

-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.adapter.AdapterAppTest

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 3.546 sec - in com.adapter.AdapterAppTest

Results :

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

[INFO]
[INFO] --- maven-surefire-plugin:2.19:test (default) @ adapter ---
[INFO] Skipping execution of surefire because it has already been run for this configuration
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 12.452 s
[INFO] Finished at: 2015-12-03T16:09:13+03:00
[INFO] Final Memory: 25M/413M
[INFO] ------------------------------------------------------------------------

为什么@Ignore注解没有作用?

【问题讨论】:

  • 你在使用 @org.junit.Ignore 吗?
  • 是的,org.junit.Ignore。抱歉格式化,我正在尝试修复。
  • 在没有真正有问题的代码的情况下进行评论是很棘手的。我建议您更新您的问题,将您的 Test 类与应该跳过的测试方法以及 imports 一起包含在内,这样会更容易发现错误。请记住,您也可以使用 maven-surefire-plugin 配置跳过测试,但是,您希望 @Ignore 工作得足够公平,所以向我们展示更多代码

标签: maven testing junit maven-surefire-plugin


【解决方案1】:

您只展示了一小部分代码,但据我所见,我怀疑您使用的是 JUnit 3(从 Testcase 扩展并通过“测试”前缀识别测试方法。

Ignore 注释仅适用于 JUnit 4 测试,即:

Testmethods 用@Test 注释,并且类不继承自TestCase

使用现在发布的代码,将测试的开始更改为:

public class AdapterAppTest {    
    @Test     
    @Ignore("this test is not ready yet")
    public static void testCreateApplicationWithAllRequiredParameters() {

【讨论】:

  • 对不起,我试图只发布有意义的作品。我使用 jUnit 4
  • 詹斯,谢谢你:Tests run: 1, Failures: 0, Errors: 0, Skipped: 1
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-02-10
  • 2021-09-17
  • 2013-10-17
  • 1970-01-01
  • 2014-04-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多