26

I needed to move some src/test/java to src/main/java according to this recommandation from maven-jar-plugin documentation => http://maven.apache.org/plugins/maven-jar-plugin/usage.html

I did so because, i used tests (helper) classes in another projects in test scope.

So i create my-project-test, moved in test classes, and configured surefire to specify test classes directory =>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                <testSourceDirectory>${basedir}\src\main\java\</testSourceDirectory>
            </configuration>
        </plugin>
    </plugins>
</build>

But when i launched mvn test, 0 tests is executed =>

Results :

Tests run: 0, Failures: 0, Errors: 0, Skipped: 0

The problem is the same as this one => Maven does not find JUnit tests to run but the solution (configure surefire maven plugin) is not working for me.

Community
  • 1
  • 1
Antoine
  • 4,456
  • 4
  • 44
  • 51
  • Are you trying to run integration tests ? Furthermore why are you defining the testSourceDirectory ? And why are you using back-slashes ? – khmarbaise Jan 24 '12 at 12:53
  • As @khmarbaise mentioned, maybe it comes from the backslashes. – ndeverge Jan 24 '12 at 13:18
  • I tried ${basedir}/src/main/java/ or src/main/java/ but it's the same Tests run: 0, Failures: 0, Errors: 0, Skipped: 0 – Antoine Jan 24 '12 at 20:18

2 Answers2

32

The good configuration for maven surefire plugin is =>

<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <testSourceDirectory>${basedir}/src/main/java/</testSourceDirectory>
                    <testClassesDirectory>${project.build.directory}/classes/</testClassesDirectory>
                </configuration>
            </plugin>
        </plugins>
    </build>

Results :

Tests run: 20, Failures: 0, Errors: 0, Skipped: 0
Antoine
  • 4,456
  • 4
  • 44
  • 51
  • 3
    Although this will work, it is by no means a good configuration, as it does not follow the Maven way of doing things. Sources should be in `src/main/java`. Their compiled classes will be in `target/classes`. Test sources should be in `src/test/java`. Their compiled classes will be in `target/test-classes`. This is the correct convention and this way Surefire will work out of the box. Putting your tests in the same directory as your module's sources is simply not right. – carlspring Jan 25 '12 at 09:51
  • 6
    I didn't put tests classes in the same directory, i created anoter project suffixed by "-tests". That's the recommandation of maven-jar-plugin help => http://maven.apache.org/plugins/maven-jar-plugin/usage.html => "How to create a jar containing test classes" => "The preferred way" – Antoine Jan 31 '12 at 07:55
  • Thanks - this helped me a lot. I am developing a set of koans that are (initially) failing unit tests but are in src/main (the already completed koans are in the src/test dir), so I needed to be able to invoke the surefire JUnit plugin on the code in src/main. Thanks a lot for this help. – quux00 Jul 22 '12 at 03:28
  • This should work for maven-failsafe-plugin, too. That's what I needed this for! Thanks, @Antoine! – Christopher Jun 04 '15 at 17:33
1

Since the test classes have now been placed in src/main/java, it is likely that they are getting compiled during the compile phase to target/classes.

So, though surefire is configured to indicate that the test source location is src/main/java, it would still look for test classes in target/test-classes, since this is the default location for test classes.

You could try specify testClassesDirectory parameter suitably and see if it works.

Also, I think you may have misinterpreted the recommendation in the maven jar plugin documentation. I do not believe they suggest moving test sources to src/main/java.

Raghuram
  • 51,854
  • 11
  • 110
  • 122
  • OK i will try so. But the maven jar plugin recommandation seams clear : "Move the sources files from src/test/java you want to share from the original project to the src/main/java of this project. The same type of movement counts for the resources as well of course." – Antoine Jan 25 '12 at 08:53
  • @Antoine. You are correct. I was familiar with `the easy way`, but never realized `the preferred way`. I'll update the answer as well. – Raghuram Jan 25 '12 at 09:42