Test class extending test class in dependency module

懵懂的女人 提交于 2019-12-20 09:15:18

问题


I've got a test class in a module that extends another test class in one of its dependency modules. How can I import the dependency's test code into the test scope of the dependent module?

To illiterate, I've got two modules, "module-one" being a dependency of "module-two". SubTestCase is a subclass of TestCase.

module-one
          \src\test\java\com\example\TestCase.java
module-two
          \src\test\java\com\example\SubTestCase.java

But the build is failing because the test code of "module-one" is not being imported into "module-two", just the main code.


回答1:


Usually this is solved by building and deploying modulename-test.jar files in addition to the regular modulename.jar file. You deploy these to repos like regular artifacts. This is not totally flawless, but works decently for code artifacts.

Then you would add test scoped dependencies to the test-jars to other modules.

You can also solve this by putting test scoped artifacts in "main" scope in a separate module of its own and then include this in regular test-scope in other modules. This solution does not work very well in a multi-module build where each module exports some test artifacts, since you basically get 2N modules.

A lot of us actually give up on both of these solution when we realize that the number of classes is fairly limited and there are problems associated with both of these solution. We just put them in an appropriately named package in "main" scope. I just keep forgetting why the two first solutions are a pain.




回答2:


You can deploy the test code as an additional artifact by using the maven-jar-plugin's test-jar goal. It will be attached to the project and deployed with the classifier tests.

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <executions>
      <execution>
        <phase>package</phase>
        <goals>
          <goal>test-jar</goal>
        </goals>
      </execution>
    </executions>
  </plugin>

Other projects can then reference the test jar by declaring the tests classifier in the dependency.

<dependency>
  <groupId>name.seller.rich</groupId>
  <artifactId>foo</artifactId>
  <version>1.0.0</version>
  <classifier>tests</classifier>
  <scope>test</scope>
</dependency>



回答3:


Regarding Rich Seller's answer: The use of <classifier>tests</classifier> is out dated see the user’s guide.

I am using maven 2.2.1 and maven-jar-plugin 2.2 and it required to switch <type>test-jar</type> instead of <classifier>tests</classifier>.

Note that tests jar are not transitive and so you may need to add them explicitly.

<project>
    ...
    <dependencies>
        <dependency>
            <groupId>name.seller.rich</groupId>
            <artifactId>foo</artifactId>
            <version>1.0.0</version>
            <type>test-jar</type>
            <scope>test</scope>
         </dependency>
    </dependencies>
     ...
</project>

Update following Mike Sokolov comment:
The user’s guild for maven 3 updated on 2014-03-28 see link above say’s

Note that previous editions of this guide suggested to use <classifier>tests</classifier> instead of <type>test-jar</type>. While this currently works for some cases, it does not properly work during a reactor build of the test JAR module and any consumer if a lifecycle phase prior to install is invoked. In such a scenario, Maven will not resolve the test JAR from the output of the reactor build but from the local/remote repository. Apparently, the JAR from the repositories could be outdated or completely missing, causing a build failure (cf. MNG-2045).



来源:https://stackoverflow.com/questions/359921/test-class-extending-test-class-in-dependency-module

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