如何将测试类包含在Maven jar中并执行它们? [英] How can I include test classes into Maven jar and execute them?
问题描述
在Maven项目中,我在同一个包中有测试类和源类,但是在不同的物理位置。
In a Maven project, I have test classes and source classes in the same package, but in different physical locations.
.../src/main/java/package/** <-- application code
.../src/test/java/package/** <-- test code
这没问题访问测试类中的源类,
但是我想在main方法中运行一个测试运行器并访问 AllTest.class
以便我可以创建jar并执行我的测试。
It's no problem to access the source classes in the test classes,
but I would like to run a test runner in the main method and access the AllTest.class
so that I can create jar and execute my tests.
public static void main(String[] args) {
// AllTest not found
Result result = JUnitCore.runClasses(AllTest.class);
for (Failure failure : result.getFailures()) {
System.out.println(failure.toString());
}
System.out.println(result.wasSuccessful());
}
但它不起作用,因为我无法访问测试码。我不明白,因为它们在同一个包中。
But it doesn't work as I don't have access to the test code. I don't understand since they are in the same package.
问题:如何从应用程序类访问测试类?或者,Maven如何打包包含测试类和执行测试的胖jar?
Question: how can access test classes from application classes? Alternatively, how can Maven package a fat jar including test classes and execute tests?
推荐答案
您不应该从应用程序访问测试类代码,而是在测试范围内创建一个main(相同的main)并为您的项目创建一个额外的工件。
You should not access test classes from your application code, but rather create a main (the same main) in the test scope and create an additional artifact for your project.
但是,在这个额外的工件(jar)中你需要:
However, in this additional artifact (jar) you would need to have:
- 测试类
- 应用程序代码类
- 应用程序代码所需的外部依赖关系(在
编译
范围内) - 测试代码所需的外部依赖关系(在
测试
范围)
- The test classes
- The application code classes
- External dependencies required by application code (in
compile
scope) - External dependencies required by the test code (in
test
scope)
这基本上意味着增加了测试的胖罐类(及其依赖项)。 Maven Jar插件及其 test-jar
目标不适合这种需要。 Maven Shade插件及其 shadeTestJar
选项既不会有帮助。
Which basically means a fat jar with the addition of test classes (and their dependencies). The Maven Jar Plugin and its test-jar
goal would not suit this need. The Maven Shade Plugin and its shadeTestJar
option would not help neither.
那么,如何在Maven中创建一个包含测试类和外部依赖项的胖jar?
在这种情况下, Maven Assembly Plugin 是一个完美的候选者。
The Maven Assembly Plugin is a perfect candidate in this case.
这是一个最小的POM样本:
Here is a minimal POM sample:
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.sample</groupId>
<artifactId>sample-project</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.3</version>
<configuration>
<descriptor>src/main/assembly/assembly.xml</descriptor>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<archive>
<manifest>
<mainClass>com.sample.TestMain</mainClass>
</manifest>
</archive>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
上面的配置是设置测试类中定义的主类。但这还不够。
The configuration above is setting the main class defined by you in your test classes. But that's not enough.
你还需要创建一个描述符文件,在 src\main\assembly
文件夹中有一个程序集。 xml
包含以下内容的文件:
You also need to create a descriptor file, in the src\main\assembly
folder an assembly.xml
file with the following content:
<assembly
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
<id>fat-tests</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<outputDirectory>/</outputDirectory>
<useProjectArtifact>true</useProjectArtifact>
<unpack>true</unpack>
<scope>test</scope>
</dependencySet>
</dependencySets>
<fileSets>
<fileSet>
<directory>${project.build.directory}/test-classes</directory>
<outputDirectory>/</outputDirectory>
<includes>
<include>**/*.class</include>
</includes>
<useDefaultExcludes>true</useDefaultExcludes>
</fileSet>
</fileSets>
</assembly>
以上配置为:
- 设置外部依赖关系取自
test
范围(也将采用compile
范围() - 设置
文件集
以包含已编译的测试类作为打包的胖jar的一部分 - 使用
fat-tests
分类器设置最终jar(因此您的最终文件将类似于sampleproject-1.0-SNAPSHOT-fat-tests。 jar
)。
- setting external dependencies to be taken from the
test
scope (which will also take thecompile
scope as well) - setting a
fileset
to include compiled test classes as part of the packaged fat jar - setting a final jar with
fat-tests
classifier (hence your final file will be something likesampleproject-1.0-SNAPSHOT-fat-tests.jar
).
然后您可以按照以下方式调用main(来自目标
文件夹):
You can then invoke the main as following (from the target
folder):
java -jar sampleproject-1.0-SNAPSHOT-fat-tests.jar
从这样一个主要部分,你也可以调用全部您的测试用例如下:
From such a main, you could also invoke all of your test cases as following:
- 创建JUni测试套件
- 添加到测试套件中有关测试
- 从普通Java main调用测试套件
测试套件示例:
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
@RunWith(Suite.class)
@SuiteClasses({ AppTest.class })
public class AllTests {
}
注意:在这种情况下,测试套件仅涉及 AppTest
样本测试。
Note: in this case the test suite is only concerning the AppTest
sample test.
然后您可以拥有以下主要类:
Then you could have a main class as following:
import org.junit.internal.TextListener;
import org.junit.runner.JUnitCore;
public class MainAppTest {
public static void main(String[] args) {
System.out.println("Running tests!");
JUnitCore engine = new JUnitCore();
engine.addListener(new TextListener(System.out)); // required to print reports
engine.run(AllTests.class);
}
}
然后上面的主要执行测试套件在链中执行所有配置的测试。
The main above would then execute the test suite which will in chain execute all of the configured tests.
这篇关于如何将测试类包含在Maven jar中并执行它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!