我应该在哪里放置Java库的测试支持代码 [英] Where should I put test support code for a Java library

查看:75
本文介绍了我应该在哪里放置Java库的测试支持代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Maven项目,该项目在其他项目中用作库.设置非常标准:带有库代码的src/main,带有src/main测试用例的src/test.

I have a Maven project which is used as a library in other projects. The setup is pretty standard: src/main with the library code, src/test with test cases for src/main.

假设我有一个项目foo,该项目依赖于此库. foo也具有测试用例.为了帮助在foo中为使用我的库的代码编写测试,我想为foo提供一个支持框架来编写测试(例如,设置模拟环境).

Let's assume I have a project foo which depends in this library. foo also has test cases. To help writing tests in foo for code that uses my library, I'd like to give foo a support framework to write tests (like setting up a mock environment).

我可以在哪里放置此支持代码?

Where can I put this support code?

  • 不应将其放入src/main,因为它并不意味着要投入生产.
  • 它不能进入​​src/test,因为从foo的测试到库的测试之间建立依赖关系会给类路径增加太多垃圾(例如logback-test.xml配置文件,从未执行过的测试, ...).
  • It shouldn't go into src/main because it's not meant to go into production.
  • It can't go into src/test because creating a dependency from foo's tests to the tests of the library adds too much junk to the classpath (like logback-test.xml config files, tests which are never executed, ...).

我可以将代码放入一个新模块中,但它与src/main中的代码紧密结合,因此我想将其保留在同一项目中(还将允许测试支持代码访问包私有字段和方法).

I could put the code into a new module but it's tightly coupled with the code in src/main, so I'd like to keep it in the same project (would also allow the test support code to access package private fields and methods).

此外,src/test中的代码应该可以访问它,以便我可以使用它编写自己的单元测试.

Also, the code in src/test should have access to it, so that I can use it to write my own unit tests.

我有什么选择可以将Maven保留在同一项目中,但仍将其与src/mainsrc/test清晰地分开?我可以以某种方式为src/test-support创建第三个输出JAR吗?还是应该将代码放入src/test中,然后在JAR插件中使用过滤器以仅包含支持代码?

What are my options with Maven to keep this in the same project but still separate it cleanly from both src/main and src/test? Can I somehow create a third output JAR for, say, src/test-support? Or should I put the code into src/test and then use filters in the JAR plugin to include only the support code?

推荐答案

这里是一个POM片段,它将主JAR分为普通工件和"JUnit支持JAR":

Here is a POM fragment which splits the main JAR into the normal artifact and a "JUnit support JAR":

<build>
    <plugins>
        <plugin>
            <artifactId>maven-jar-plugin</artifactId>
            <executions>
                <execution>
                    <id>default-jar</id>
                    <phase>package</phase>
                    <goals>
                        <goal>jar</goal>
                    </goals>
                    <configuration>
                        <archive>
                            <index>true</index>
                            <manifest>
                                <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
                            </manifest>
                        </archive>
                        <excludes>
                            <exclude>**/junit/**</exclude>
                        </excludes>
                    </configuration>
                </execution>

                <execution>
                    <id>junit-jar</id>
                    <phase>package</phase>
                    <goals>
                        <goal>jar</goal>
                    </goals>
                    <configuration>
                        <classifier>junit</classifier>
                        <archive>
                            <index>true</index>
                            <manifest>
                                <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
                            </manifest>
                        </archive>
                        <includes>
                            <include>**/junit/**</include>
                        </includes>
                    </configuration>
                </execution>

                <execution>
                    <id>test-jar</id>
                    <goals>
                        <goal>test-jar</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

    </plugins>
</build>

简而言之,它将查找名称中带有junit的任何软件包,并将其放入JUnit JAR中,并将其从常规工件中排除.

In a nutshell, it looks for any package with junit in its name and puts it into the JUnit JAR, excluding it from the normal artifact.

如果普通罐子的坐标是

<groupId>com.group</groupId>
<artifactId>foo</artifactId>

然后您只需添加<classifier>junit</classifier>,即可获得JUnit支持代码:

then you can get the JUnit support code by simply adding <classifier>junit</classifier>:

<groupId>com.group</groupId>
<artifactId>foo</artifactId>
<classifier>junit</classifier>

因此要使用此功能,依赖于com.group:foo的POM将如下所示:

So to use this, the POM which depends on com.group:foo will look like this:

<dependency>
    <groupId>com.group</groupId>
    <artifactId>foo</artifactId>
    <version>...</version>
</dependency>
<dependency>
    <groupId>com.group</groupId>
    <artifactId>foo</artifactId>
    <version>...</version>
    <classifier>junit</classifier>
    <scope>test</scope>
</dependency>

有时,您将需要JUnit来编译"JUnit支持JAR".如果是这样,请使用

Sometimes, you will need JUnit to compile your "JUnit support JAR". If that's the case, use

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <scope>compile</scope>
    <optional>true</optional>
</dependency>

将JUnit包含在构建中.这将使依赖项在编译时可用,但不会泄漏给其他任何人.

to include JUnit into the build. This will make the dependency available at compile time but it will not leak to anyone else.

这篇关于我应该在哪里放置Java库的测试支持代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆