Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven running tests annotated with junit @Ignore when building Java project

I've annotated a bad test with @Ignore, both on the method-level and on the class level. When running the test through the command line (I've tried "mvn clean install", "mvn test", "mvn clean install -DskipTests; mvn test"), however, the @Ignore annotation is ignored, the test is run, and- as it is a bad test- it fails.

Here is the test:

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

And here is my 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>

Worth noting that the test is skipping when the class is run in Intellij IDEA 14, and that there are some other issues with tests failing on mvn test that are passing in Intellij (but are out of the scope of this question). Thank you for the help!

like image 987
Will Avatar asked Nov 20 '15 06:11

Will


People also ask

How do you run a JUnit test case in Maven build?

We can run our unit tests with Maven by using the command: mvn clean test. When we run this command at command prompt, we should see that the Maven Surefire Plugin runs our unit tests. We can now create a Maven project that compiles and runs unit tests which use JUnit 5.

What is correct use of @ignore annotation in JUnit?

The @Ignore test annotation is used to ignore particular tests or group of tests in order to skip the build failure. @Ignore annotation can be used in two scenarios as given below: If you want to ignore a test method, use @Ignore along with @Test annotation.

Why tests are not running in Maven?

Test Method Not public Also, the test method could have been marked as private by mistake. Until JUnit 4, Maven will only run the test classes which are marked as public. We should note, though, that this won't be an issue with JUnit 5+.

Does Maven support JUnit?

The JUnit Platform Provider supports the test JVM system property supported by the Maven Surefire Plugin.


2 Answers

For those who are working with Junit 5.

@Ignore did not work formaven surefire neither Intellij tests even if I had already included junit-vintage-engine

I used @Disabled and it worked for maven at least!

like image 160
madz Avatar answered Oct 24 '22 17:10

madz


Wemu posted the solution:

I think you can simply remove: junit:junit-dep from the surefire config – wemu Nov 20 at 7:10

Removed the element and all tests passed!

like image 39
Will Avatar answered Oct 24 '22 18:10

Will