在构建Java项目时,Maven正在运行使用junit @Ignore注释的测试

时间:2015-11-20 06:39:08

标签: java maven unit-testing testing junit

我在@Ignore上注释了一个关于方法级别和类级别的错误测试。通过命令行运行测试时(我已尝试" mvn clean install"," mvn test"," mvn clean install -DskipTests; mvn test&#34但是,@ Igore注释被忽略,测试运行,并且 - 因为它是一个糟糕的测试 - 它失败了。

以下是测试:

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中运行类时,测试正在跳过,并且在mvn测试中测试失败的一些其他问题是在Intellij中传递(但不在本问题的范围内)。谢谢你的帮助!

3 个答案:

答案 0 :(得分:0)

我遇到了与JUNIT库相同的问题。经过这么多尝试,它与下面的Pom.xml合作:

<repositories>
<repository>
<id>junit</id>
<url>http://junit.org</url>
</repository>
</repositories>

<dependencies>
<dependency><groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version></dependency>
</dependencies>

答案 1 :(得分:0)

针对与Junit 5一起工作的人。

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

我使用了@Disabled,它至少对maven有用!

答案 2 :(得分:0)

对我来说,解决方案是简单地将 JUnit 版本更新到 4.13:

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.13</version>
    <scope>test</scope>
</dependency>
相关问题