Home > Net >  How do I get my maven build to run generated unit tests from target\test-classes?
How do I get my maven build to run generated unit tests from target\test-classes?

Time:11-15

I use a code generator JAVA program to generate unit test sources into target\generated-test-sources that I compile successfully into target\test-classes but they are ignored by the maven build. I launch this build with a mvn clean test.

In my pom.xml file, I have an exec goal of a main class com.mypackage.TestCaseGenerator that I bind to the generate-test-sources phase.

      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>3.1.0</version>
        <executions>
          <execution>
            <id>myexec</id>
            <phase>generate-test-sources</phase>
            <goals>
              <goal>exec</goal>
            </goals>
            <configuration>
              <executable>java</executable>
              <classpathScope>test</classpathScope>
              <arguments>
                <argument>-classpath</argument>
                <classpath/>
                <argument>com.mypackage.TestCaseGenerator</argument>
                <argument>target/generated-test-sources/somedir</argument>
              </arguments>
            </configuration>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>build-helper-maven-plugin</artifactId>
        <executions>
          <execution>
            <id>add-test-source</id>
            <phase>generate-test-sources</phase>
            <goals>
              <goal>add-test-source</goal>
            </goals>
            <configuration>
              <sources>
                <source>target/generated-test-sources/somedir</source>
              </sources>
            </configuration>
          </execution>
        </executions>
      </plugin>

When I run the test cases from Eclipse with a right-click -> run as JUnit Test on target/generated-test-sources/somedir, the tests execute properly.

Am I missing some maven config ?

CodePudding user response:

If the class/file names of your generated tests do not match the defaults of the Maven Surefire Plugin, you will have to configure that to include tests by another pattern.

See the official plugin documentation for Inclusions and Exclusions of Tests:

By default, the Surefire Plugin will automatically include all test classes with the following wildcard patterns:

  • "**/Test*.java" - includes all of its subdirectories and all Java filenames that start with "Test".
  • "**/*Test.java" - includes all of its subdirectories and all Java filenames that end with "Test".
  • "**/*Tests.java" - includes all of its subdirectories and all Java filenames that end with "Tests".
  • "**/*TestCase.java" - includes all of its subdirectories and all Java filenames that end with "TestCase".

You can configure different patterns with the <includes> or <excludes> sections of the plugins configuration. You will find nice examples on how to do that at the documentation page linked above.

  • Related