jeudi 19 novembre 2015

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://ift.tt/IH78KX" xmlns:xsi="http://ift.tt/ra1lAU"
xsi:schemaLocation="http://ift.tt/IH78KX http://ift.tt/VE5zRx">
<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!

Aucun commentaire:

Enregistrer un commentaire