Dave
Dave

Reputation: 19090

How do I run an individual test in Maven?

We're using Maven 3.0.3 and are using JUnit 4.8.1 ...

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

We have this test file ...

./src/test/java/com/myco/clearing/common/xml/TextNodeTest.java

How can I run this individual test? When I try

mvn -Dtest=TextNodeTest test

I get an error saying no tests were run. I get the same error if I specify the entire package name to my test. ...

mvn clean -Dtest=com.myco.clearing.common.xml.TextNodeTest test

which produces the error message ...

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project myco-productplus-web: No tests were executed!
(Set -DfailIfNoTests=false to ignore this error.) -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

Here is the surefire configuration I'm using

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.12</version>
            <configuration>
                <skip>false</skip>
                <additionalClasspathElements>
                    <additionalClasspathElement>${project.build.sourceDirectory}</additionalClasspathElement>
                    <additionalClasspathElement>${project.build.testSourceDirectory}</additionalClasspathElement>
                </additionalClasspathElements>
                <useManifestOnlyJar>false</useManifestOnlyJar>
                <forkMode>always</forkMode>
                <systemProperties>
                    <property>
                        <name>gwt.args</name>
                        <value>-out "${webappDirectory}"</value>
                    </property>
                </systemProperties>
                <systemPropertyVariables>
                    <tomcat.port>${tomcat.servlet.port}</tomcat.port>
                    <project.artifactId>${project.artifactId}</project.artifactId>
                </systemPropertyVariables>
            </configuration>
        </plugin>

Upvotes: 3

Views: 5773

Answers (3)

Matthew Farwell
Matthew Farwell

Reputation: 61695

The way to run a single test (method) in surefire is to do:

mvn test -Dtest=uk.co.farwell.AppTest#testSlow

Note the # instead of the space between the class name and the method name.

However, as @Andrew says, there is a bug in 2.12 (SUREFIRE-827: Surefire 2.12 cannot run a single test, regression from 2.11), but it works in 2.11.

The above bug is still open (as of 24.02.2012), but actually works for me using 2.13-SNAPSHOT.

EDIT: This is now marked as fixed in 2.12.1.

Upvotes: 6

Oliver Marienfeld
Oliver Marienfeld

Reputation: 1393

Like the others say, it's a bug in Surefire and it's fixed since version 2.11.1.

Upvotes: 0

Andrew Logvinov
Andrew Logvinov

Reputation: 21821

Looks like it's a bug in 2.12 version - SUREFIRE-827. Try downgrading to 2.11.

Upvotes: 2

Related Questions