java - Maven 在构建 Java 项目时运行带有 junit @Ignore 注释的测试

标签 java maven unit-testing testing junit

我在方法级别和类级别都使用@Ignore 注释了一个错误的测试。当通过命令行运行测试时(我试过“mvn clean install”、“mvn test”、“mvn clean install -DskipTests; mvn test”),然而,@Ignore 注释被忽略,测试运行,并且 - 因为这是一个糟糕的测试 - 它失败了。

这是测试:

public class UserTest extends PersistentTestBase {
    @Test
    @Ignore
    public void testPersistence() {
    ...
    }
}

这是我的 pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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>

...
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

...

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.8</java.version>
</properties>

<dependencies>
   ...

    <!-- Testing -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.jmock</groupId>
        <artifactId>jmock-junit4</artifactId>
        <version>2.8.1</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.jmock</groupId>
        <artifactId>jmock-legacy</artifactId>
        <version>2.8.1</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.easymock</groupId>
        <artifactId>easymock</artifactId>
        <version>3.4</version>
        <scope>test</scope>
    </dependency>
...

<!-- For dep management, see Mykong.com's "How to create a jar file with Maven" -->
<build>
    <plugins>
        <!-- Necessary to force language level of Java 8 -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.2</version>
            <configuration>
                <source>${java.version}</source>
                <target>${java.version}</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.18.1</version>
            <configuration>
                <junitArtifactName>junit:junit-dep</junitArtifactName>
                <useFile>false</useFile>
                <trimStackTrace>false</trimStackTrace>
                <reuseForks>false</reuseForks>
                <forkCount>1</forkCount>
            </configuration>
        </plugin>

        <!-- Packages into a jar, looking for deps in target/dependency-jars -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.2</version>
            <configuration>
                <excludes>
                    <exclude>**/log4j.properties</exclude>
                </excludes>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <mainClass>...Application</mainClass>
                        <classpathPrefix>dependency-jars</classpathPrefix>
                    </manifest>
                </archive>
            </configuration>
        </plugin>

        <!-- Moves all compiled deps to target/dependency-jars -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.5.1</version>
            <executions>
                <execution>
                    <id>copy-dependencies</id>
                    <phase>package</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <includeScope>runtime</includeScope>
                        <outputDirectory>${project.build.directory}/dependency-jars</outputDirectory>
                    </configuration>
                </execution>
            </executions>
        </plugin>

        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <configuration>
                <mainClass>...Application</mainClass>
            </configuration>
        </plugin>
    </plugins>
</build>

值得注意的是,当类在 Intellij IDEA 14 中运行时测试正在跳过,并且在 Intellij 中传递的 mvn test 上测试失败还有一些其他问题(但超出了这个问题的范围)。感谢您的帮助!

最佳答案

对于那些正在使用 Junit 的人5.

@Ignore不适用于 maven surefire都不是Intellij测试即使我已经包含了junit-vintage-engine

我用了@Disabled它适用于 maven至少!

关于java - Maven 在构建 Java 项目时运行带有 junit @Ignore 注释的测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33820444/

相关文章:

java - 发生重复键时的 hashmap 自定义覆盖值

java - 在网络上传递 Swing 类(引用)?

python - 模拟 ftplib.FTP 用于单元测试 Python 代码

java - 将 Generic 与 Class<?> 参数一起使用时出错

java - 使用 WatchService 设置发布者-订阅者

找不到 Maven 3.0.4 安装到 Ubuntu mvn

java - Maven中的交互模式和批处理模式有什么区别?

java - 是否有任何 Maven 插件可以发送 HTTP 请求并验证结果?

c++ - 从类 T 获取函数名 (__func__) 和指向成员函数 void(T::*pmf)() 的指针

node.js - chai 和 Eshint 的单元测试没有通过