views:

295

answers:

2

I am running Junit tests from Maven. The ant script has

<junit failureproperty="failproperty" errorproperty="errorproperty">        
                     <classpath refid="classpath" /> 
                     <test name="${unit-test-suite}" />
                     <formatter type="brief" usefile="false" />
      </junit>

      <echo> ----------> ${failproperty} </echo>
      <echo> ----------> ${errorproperty} </echo>
      <fail message="something wrong" if="${failureproperty}"/>
      <fail message="something wrong" if="${errorproperty}"/>

I tried

  • Halting on error or failure in Junit - This halts when I run the ant script only but the build succeeds in Maven and does not halt even thought JUnit has an error.

    • I tried to send a fail message by setting the errorproperty and the failure property- This does not set the variables.

How do I tell Maven to stop everything when Junit fails?

Maven Details:

Maven version: 2.0.10
Java version: 1.6.0_17
OS name: "windows xp" version: "5.1" arch: "x86" Family: "windows"
A: 

This "fail fast" feature isn't currently supported by Maven 2, see SUREFIRE-580 (and maybe vote for it).

Pascal Thivent
A: 

Are you sure you're setting the properties correctly?

Note that the if and unless attributes take property names, not expressions. For example, try using:

<fail if="failproperty"/> instead of <fail if="${failproperty}"/>

The <fail/> tag works in this simple example. Try mvn package and then mvn package -Dfailproperty=true

<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/maven-v4_0_0.xsd"&gt;
    <modelVersion>4.0.0</modelVersion>

    <groupId>ant-test</groupId>
    <artifactId>ant-test</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

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

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <executions>
                    <execution>
                        <id>ant-test</id>
                        <phase>package</phase>
                        <configuration>
                            <tasks>
                                <junit failureproperty="fail">
                                    <classpath>
                                        <path refid="maven.plugin.classpath"/>
                                        <path refid="maven.test.classpath"/>
                                    </classpath>
                                    <formatter type="plain" />
                                    <batchtest>
                                        <fileset dir="src/test/java"/>
                                    </batchtest>
                                </junit>
                                <fail if="fail"/>
                            </tasks>
                        </configuration>
                        <goals>
                            <goal>run</goal>
                        </goals>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>org.apache.ant</groupId>
                        <artifactId>ant-junit</artifactId>
                        <version>1.7.1</version>
                    </dependency>
                </dependencies>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <skipTests>true</skipTests>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Assuming a Test case in src/test/java:

import junit.framework.Assert;

import org.junit.Test;

public class FakeTestCase {

    @Test
    public void testThis() {
        Assert.fail("Failure");
    }

}
Kevin
if I pass in the arguments like you do then yes it works but the fail property does not get set if I run from maven even when i get a JUnit Error. Try it in your project
kunjaan
It works. See the updated project
Kevin