Surefire doesn't launch test in src/main/java

非 Y 不嫁゛ 提交于 2019-11-30 06:56:57

问题


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.


回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/8986915/surefire-doesnt-launch-test-in-src-main-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!