run maven tests from classpath

可紊 提交于 2019-12-05 16:34:09

This is now possible with Maven Surefire v2.15. Simply add the following kind of configuration to the surefire plugin:

<build>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.15</version>
    <configuration>
      <dependenciesToScan>
        <dependency>com.group.id:my-artifact</dependency>
        <dependency>com.group.id:my-other-artifact</dependency>
      </dependenciesToScan>
      ...
    </configuration>
    ...
  </plugin>
  ...
</build>

You should also declare the actual dependencies in the dependencies section:

<dependencies>
  <dependency>
    <groupId>com.group.id</groupId>
    <artifactId>my-artifact</artifactId>
    <type>test-jar</type>
    <version>1.1</version>
    <scope>test</scope>
  </dependency>
  <dependency>
    <groupId>com.group.id</groupId>
    <artifactId>my-other-artifact</artifactId>
    <type>test-jar</type>
    <version>1.1</version>
    <scope>test</scope>
  </dependency>
</dependencies>

No such luck. surefire wants to just run the tests that it can see in the sources.

This is currently not possible out of the box, surefire just looks at classes in target/test-classes:

This is actually logged as SUREFIRE-569 - There should be a way to run unit tests from a dependency jar.

I can see a sadly obvious solution here involving the build-helper-plugin and adding the tests into the test compilation environment as a source directory, but I was hoping to avoid it.

The current workaround is to use dependency:unpack to unpack the jar into target/test-classes before the test phase.

Can't you do it the other way round?

I mean put the code the src/test/java, depend on your main module, and run the tests in your test module?

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