Pogryziony
Pogryziony

Reputation: 58

Maven Surefire Not Running Tests in Suite - Tests Detected as Zero

I am experiencing an issue with Maven's Surefire plugin in my test automation project. When I run individual test classes using mvn test -Dtest=TestClass, it works fine. However, executing my test suite using mvn test -Dtest=TestSuite results in no tests being recognized. The output consistently shows "Tests run: 0", indicating that no tests are executed. Additionally, when running tests from the suite class, I get the message "No tests were found."

I'm seeking assistance to understand why my test suite isn't being recognized by Maven Surefire and how to resolve this issue. Any insights or suggestions on the cause of this behavior and potential fixes would be greatly appreciated.

CODE:

Here's my pom.xml: https://pastebin.com/QXVpd9CN

Here's an example of a base test class (BaseTest.java): https://pastebin.com/ihtNkxHp

And an example of a specific test class (Application1BaseTest.java): https://pastebin.com/WU3fj4Qv Finally, my suite class (SmokeTests.java):https://pastebin.com/Av9z1PkG

Here's what I've tried and observed so far:

  1. Test Structure: My tests are located in src/main/java, which is unconventional but intentional for this project.

  2. Test Annotations: I've ensured that my test methods are properly annotated with @Test from org.junit.jupiter.api.Test.

  3. POM Configuration: In pom.xml, I've set the <testSourceDirectory> to src/main/java. Additionally, I've checked the Surefire plugin configuration to make sure it includes my test classes and patterns.

  4. Suite Configuration: I created a test suite class with @Suite and @SelectClasses annotations from JUnit Jupiter to aggregate my tests. However, running this suite with Maven results in no tests being executed.

  5. Classpath Issues: I'm suspecting there might be a classpath issue, although running tests individually works fine.

  6. JUnit 5 Dependencies: I've made sure to include all necessary JUnit 5 dependencies in my pom.xml.

EDIT: I've actually tried conventional approach. I've placed all tests in src/main/java/test as well as BaseTest. Suite still can't find any tests. When running mvn test - test files are found, when running mvn test with only Suite enabled I'm getting 0 tests run. I've uploaded new pom.xml - note that I've already tried using build-helper-maven-plugin

Upvotes: 0

Views: 1889

Answers (1)

Nick Holt
Nick Holt

Reputation: 34321

Use the add-test-source goal from Build Helper plugin, to add src/main/java as a test source as follows:

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>build-helper-maven-plugin</artifactId>
  <version>3.4.0</version>
  <executions>
    <execution>
      <id>add-test-source</id>
      <phase>process-resources</phase>
      <goals>
        <goal>add-test-source</goal>
      </goals>
      <configuration>
        <sources>
          <source>src/main/java</source>
        </sources>
      </configuration>
    </execution>
  </executions>
</plugin>

And ensure you have the scope correct on the JUnit Jupiter dependencies; scope should not be set or set to compile.

It's also worth noting, this is a little weird. If you're putting tests in src/main/java because you want to get them into a JAR for reuse, then Maven has a convention for test JARs.

Upvotes: 1

Related Questions