【问题标题】:Maven running tests annotated with junit @Ignore when building Java projectMaven 在构建 Java 项目时运行带有 junit @Ignore 注释的测试
【发布时间】:2016-02-22 13:44:32
【问题描述】:

我在方法级别和类级别都使用@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 测试失败还有一些其他问题(但不在此问题的范围内)。感谢您的帮助!

【问题讨论】:

  • 确保您使用的是junit47 测试运行器。
  • 我认为你可以简单地从surefire配置中删除:junit:junit-dep
  • @wemu 太棒了,成功了,谢谢!为什么那个元素会破坏测试?
  • 说实话:我不知道。 junit-dep 工件已经很老了。我认为它可能无法解释 @Ignore 注释(过去的 junit 包已更改)。所以只是一个疯狂的猜测:)

标签: java maven unit-testing testing junit


【解决方案1】:

对于那些与Junit 5 合作的人。

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

我使用了@Disabled,它至少适用于maven

【讨论】:

    【解决方案2】:

    Wemu 贴出解决方法:

    我认为您可以简单地删除: junit:junit-dep 来自 surefire 配置 – wemu 11 月 20 日 7:10

    删除了元素,所有测试都通过了!

    【讨论】:

      【解决方案3】:

      我在使用 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>
      

      【讨论】:

        【解决方案4】:

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

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13</version>
            <scope>test</scope>
        </dependency>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2010-09-30
          • 2016-03-09
          • 2018-06-18
          • 1970-01-01
          • 2021-12-17
          • 2013-11-08
          • 2018-03-24
          相关资源
          最近更新 更多