How to run Maven Failsafe integration tests from a jar file?

Ortomala Lokni :

I have a Spring Boot multi module Maven project, I can run integration tests with:

mvn clean verify

and it works well. I now want to run the same integration tests from a container and I don't want to embed all the source code in the container.

My question is : how can I run the Maven Failsafe Plugin without using the source code?

I tried to run the failsafe:integration-test goal and setting the dependenciesToScan parameter, from the command line:

mvn failsafe:integration-test -DdependenciesToScan=com.myorg:proj-tests.jar

but no tests are found.

P.S.1: I've seen this similar question Running spring tests from executable jar. But I don't need to run the tests without Maven. I prefer to run tests from the command line with Maven than adding code or modifying the structure of my project.

P.S.2: I'm using maven-failsafe-plugin 2.22.2 which is the version provided with Spring Boot 2.1.8.

b0gusb :

From the docs:

Since version 2.22.0 you can scan for test classes from a project dependency of your multi-module project.

It means the tests (proj-tests.jar) must be a dependency of the project. As you cannot have a dependency to the tests jar in the same project where you build them, the solution is to have another module or pom file. Example:

<groupId>failsafe.use.jar</groupId>
  <artifactId>failsafe-use-jar</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <dependencies>
    ...
    <dependency>
      <groupId>com.myorg</groupId>
      <artifactId>proj-tests</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <classifier>tests</classifier>
    </dependency>
    ...
  </dependencies>

   <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-failsafe-plugin</artifactId>
        <version>2.22.2</version>
      </plugin>
    </plugins>
  </build> 

The proj-tests is a project dependency and can be created with:

 <groupId>com.myorg</groupId>
  <artifactId>proj-tests</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>
 ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>3.1.2</version>
        <executions>
          <execution>
            <goals>
              <goal>test-jar</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

See Guide to using attached tests

To run the integration tests from the container you obviously need all the dependencies installed in the local (container) maven repository or deployed in remote. Then you can run with:

mvn failsafe:integration-test -DdependenciesToScan=com.myorg:proj-tests

Note that the format of the dependenciesToScan property is groupId:artifactId (you run with the name of jar instead of artifactid)

As another note, for integration tests failsafe searches by default for class files ending in IT (integration test).

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=165799&siteId=1