How to use surefire plugin to run test in customized folder structure
Asked Answered
L

2

5

I have a customized eclipse studio project. For unit testing we are creating a test file under -

  TestJavaSrc/demoTest.java

Now this TestJavaSrc folder is on the same level as of POM.xml Here is POM.xml -

 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project>
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.demo.test</groupId>
    <artifactId>demo</artifactId>
    <version>1.0.0</version>
    <packaging>jar</packaging>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.19.1</version>
            </plugin>
        </plugins>
    </build>
</project>

Now when I run this project as mvn test , it is not able to find any test files. And also , when I run following command on command line -

 mvn "-Dtest=TestJavaSrc/DemoTest.java" test

It gives me error-

 Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.19.1:test
 (default-test) on project demo: No tests were executed! 
 (Set -DfailIfNoTests=false to ignore this error.) -> [Help 1]

Also , I have JUnit4 in my classpath

Is there anything I am missing?

Lucarne answered 15/11, 2016 at 13:11 Comment(0)
B
7

In the Maven standard directory layout, unit tests written in Java should be located under src/test/java:

The src directory contains all of the source material for building the project, its site and so on. It contains a subdirectory for each type: main for the main build artifact, test for the unit test code and resources, site and so on.

As such, the Surefire Plugin will execute the tests that are exactly in that folder; this is configurable through the testSourceDirectory parameter. Note that this directory is the base source directory that contains all of the tests. This means that if that base directory is somedir and there is a Java class called DemoTest.java, declared to be in the package foo.bar, then the file on disk must be located in somedir/foo/bar/DemoTest.java.

This is where the parameter test that you're (mis)using enters: it selects the Java class to execute by its name only; not by its package declaration nor by the location of the file on disk.

I'd recommend that you place your test class in that standard directory, but if you truly want to do this, there are multiple options:

  1. If you want to have a single test source directory, which is TestJavaSrc, located at the base directory of the Maven project, then you can configure it in your POM with:

    <build>
      <testSourceDirectory>${project.basedir}/TestJavaSrc</testSourceDirectory>
    </build>
    
  2. If you want to have a special directory just for that lone test, which is to say that you want to keep the default src/test/java, but want to additionally consider this new source directory, then you can use the add-test-source goal of the Build Helper Maven Plugin:

    <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>build-helper-maven-plugin</artifactId>
      <version>1.12</version>
      <executions>
        <execution>
          <id>add-test-source</id>
          <goals>
            <goal>add-test-source</goal>
          </goals>
          <configuration>
            <sources>
              <source>TestJavaSrc</source>
            </sources>
          </configuration>
        </execution>
      </executions>
    </plugin>
    

Note that both approaches imply that the demoTest.java file, which is directly under that folder, will be in the default package, so it must not have a package declaration.

Ballerina answered 15/11, 2016 at 14:3 Comment(0)
T
2

Assuming you are not using src/test/java at all for tests, you can instruct the build to use a different directory, e.g.:

<build>          
    ...
    <testSourceDirectory>${project.basedir}/TestJavaSrc</testSourceDirectory>
    ...
</build>
Tendance answered 15/11, 2016 at 13:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.