Sangeet
Sangeet

Reputation: 1118

Running a Single Parameterized Test with Maven in JUnit 4

I'm trying to run a specific parameterized test method (xyzIT#testTaskRestarts) from a JUnit 4 test class using the Maven Surefire Plugin, but I'm encountering issues.

With the base command being set to :

mvn -Dcloud -Pjenkins,pr-builder -Dlicense.skip=true -U \
-Dmaven.wagon.http.retryHandler.count=10 \
--batch-mode \
--no-transfer-progress clean verify install dependency:analyze validate

I tried all these variations, appending them at the end:

"-Dtest=xyzIT#testTaskRestarts"
"-Dtest=xyzIT#testTaskRestarts[*]"
"-Dtest=%regex[.*xyzIT.*#testTaskRestarts.*]"

However, this results in the following warning and the test is not executed:

[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
Jun 13, 2024 4:26:54 PM org.junit.vintage.engine.descriptor.RunnerTestDescriptor logIncompleteFiltering
WARNING: Runner org.junit.runners.Parameterized (used on class com.example.integration.end2end.xyzIT) was 
not able to satisfy all filter requests.
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 0, Failures: 0, Errors: 0, Skipped: 0

Details:

Edit: Adding more details addressing the first comment.

    <junit-jupiter.version>5.9.1</junit-jupiter.version>
    <junit-jupiter-aggregator.version>5.9.1</junit-jupiter-aggregator.version>

Also: junit junit 4.13.1 test

The maven failsafe comes from the parent pom and is 3.0.0-M4.

PS: I did take a look this issue : https://github.com/junit-team/junit5/issues/549 But couldn't gather the fix they were talking about that was shipped.

EDIT 2: I was able to run one particular test by using -Dit.test : mvn -Dcloud -Pjenkins,pr-builder -Dlicense.skip=true -U -Dmaven.wagon.http.retryHandler.count=10 --batch-mode --no-transfer-progress clean verify install dependency:analyze validate -DskipTests -Dit.test=abcIT -Ddependency.check.skip=true But this runs all the UTs as well. Some progress but I would want to have a way where I can skip the maven surefire tests and run just one maven failsafe test.

Upvotes: 0

Views: 283

Answers (2)

Jesse Glick
Jesse Glick

Reputation: 25461

Reminiscent of SUREFIRE-2010 but does not sound the same.

Upvotes: 0

khmarbaise
khmarbaise

Reputation: 97379

First Maven 4.0.0 does not yet exist... (4.0.0-beta-3 exists)... use the latest Maven 3.9.X (3.9.7).. define the maven-surefire-plugin ....

Also running tests etc:

mvn clean verify

nothing more is required...the following does not make sense:

clean verify install dependency:analyze validate

This is wrong because install life cycle includes verify etc. also validate starts from the beginning (https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle)

The following is an example how to configure JUnit 4 + Junit Jupiter (aka Junit 5). It is required to define all plugins for example surefire/failsafe plugins etc. and of course locate the unit test into the appropriate location src/test/<package>/XYZTest.java...

Furthermore the naming convention for integration tests is src/test/<package/AbcdeIT.java which are executed by the maven-failsafe-plugin...

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.release>17</maven.compiler.release>
  </properties>
  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.junit</groupId>
        <artifactId>junit-bom</artifactId>
        <version>5.10.2</version>
        <scope>import</scope>
        <type>pom</type>
      </dependency>
      <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.13.2</version>
      </dependency>
    </dependencies>
  </dependencyManagement>

  <dependencies>
    <dependency>
      <groupId>org.junit.jupiter</groupId>
      <artifactId>junit-jupiter-engine</artifactId>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.junit.vintage</groupId>
      <artifactId>junit-vintage-engine</artifactId>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.3.2</version>
        </plugin>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.3.1</version>
        </plugin>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-jar-plugin</artifactId>
          <version>3.4.0</version>
        </plugin>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.13.0</version>
        </plugin>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>3.2.5</version>
          <configuration>
            <skipTests>${skipUTs}</skipTests>
          </configuration>
        </plugin>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-failsafe-plugin</artifactId>
          <version>3.2.5</version>
          <configuration>
            <skipTests>${skipITs}</skipTests>
          </configuration>
        </plugin>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-install-plugin</artifactId>
          <version>3.1.1</version>
        </plugin>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>3.1.1</version>
        </plugin>
      </plugins>
    </pluginManagement>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-failsafe-plugin</artifactId>
        <executions>
          <execution>
            <goals>
              <goal>integration-test</goal>
              <goal>verify</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

The above examples is coming from https://github.com/khmarbaise/youtube-videos/tree/main/episode-3

If you need parameterized test you should take a look here: https://github.com/khmarbaise/youtube-videos/blob/main/episode-2/pom.xml which requires only add a supplemental dependency:

<dependency>
  <groupId>org.junit.jupiter</groupId>
  <artifactId>junit-jupiter-params</artifactId>
  <scope>test</scope>
</dependency>

Also check the docs of JUnit Jupiter (aka JUnit 5)

Upvotes: 0

Related Questions