问题描述
在一个 Maven 项目中,我有测试类和源类在同一个包中,但在不同的物理位置.
.../src/main/java/package/**
在测试类中访问源类没有问题,但我想在 main 方法中运行一个测试运行器并访问
AllTest.class
以便我可以创建 jar 并执行我的测试.
public static void main(String[] args) {//未找到所有测试结果 result = JUnitCore.runClasses(AllTest.class);for(失败失败:result.getFailures()){System.out.println(failure.toString());}System.out.println(result.wasSuccessful());}
但它不起作用,因为我无权访问测试代码.我不明白,因为它们在同一个包中.
问题:如何从应用程序类访问测试类?或者,Maven如何打包包含测试类的fat jar并执行测试?
解决方案
你不应该从你的应用程序代码中访问测试类,而是应该在测试范围内创建一个 main(相同的 main)并为你的项目创建一个额外的工件.
但是,在这个额外的工件(jar)中,您需要:
测试类
应用程序代码类
应用程序代码所需的外部依赖(在
compile
范围内)测试代码所需的外部依赖(在
test
范围内)
这基本上意味着一个添加了测试类(及其依赖项)的胖罐子.Maven Jar Plugin 及其
test-jar
目标不适合这种需要.Maven Shade Plugin 及其shadeTestJar
选项也无济于事.
那么,如何在 Maven 中创建一个带有测试类和外部依赖项的胖 jar?
Maven Assembly Plugin 是这种情况下的完美候选.>
这是一个最小的 POM 示例:
<modelVersion>4.0.0</modelVersion><groupId>com.sample</groupId><artifactId>sample-project</artifactId><version>1.0-SNAPSHOT</version><构建><artifactId>maven-assembly-plugin</artifactId><version>2.3</version><配置><descriptor>src/main/assembly/assembly.xml</descriptor></配置><执行><执行><id>make-assembly</id><phase>包</phase><目标><目标>单</目标></目标><配置><存档><清单><mainClass>com.sample.TestMain</mainClass></清单></归档></配置></执行></执行></插件></插件></build><依赖项><依赖><groupId>junit</groupId><artifactId>junit</artifactId><version>4.11</version><范围>测试</范围></依赖></依赖项></项目>
上面的配置是设置你在测试类中定义的主类.但这还不够.
您还需要创建一个描述符文件, 在
srcmainassembly
文件夹中,有一个 assembly.xml
文件,内容如下:
<id>fat-tests</id><format>jar</format></格式><includeBaseDirectory>false</includeBaseDirectory><依赖集><outputDirectory>/</outputDirectory><useProjectArtifact>true</useProjectArtifact><unpack>true</unpack><范围>测试</范围></dependencySet></dependencySets><文件集><文件集><目录>${project.build.directory}/test-classes</directory><outputDirectory>/</outputDirectory><包括><include>**/*.class</include></包括><useDefaultExcludes>true</useDefaultExcludes></fileSet></fileSets></组装>
上面的配置是:
设置要从
test
作用域中获取的外部依赖项(也将获取compile
作用域)设置
fileset
以将编译的测试类包含在打包的 fat jar 中使用
.莉>fat-tests
分类器设置最终 jar(因此您的最终文件将类似于sampleproject-1.0-SNAPSHOT-fat-tests.jar
).
然后您可以调用 main 如下(从 target
文件夹):
java -jar sampleproject-1.0-SNAPSHOT-fat-tests.jar
从这样的主程序中,您还可以调用所有测试用例,如下所示:
- 创建 JUni 测试套件
- 将相关测试添加到测试套件
- 从纯 Java 主程序调用测试套件
测试套件示例:
import org.junit.runner.RunWith;导入 org.junit.runners.Suite;导入 org.junit.runners.Suite.SuiteClasses;@RunWith(Suite.class)@SuiteClasses({ AppTest.class })公共类 AllTests {}
注意:在这种情况下,测试套件仅涉及 AppTest
示例测试.
然后你可以有一个如下的主类:
import org.junit.internal.TextListener;导入 org.junit.runner.JUnitCore;公共类 MainAppTest {公共静态无效主(字符串 [] args){System.out.println("运行测试!");JUnitCore 引擎 = new JUnitCore();engine.addListener(new TextListener(System.out));//需要打印报告引擎运行(AllTests.class);}}
然后上面的 main 将执行测试套件,该套件将在链中执行所有配置的测试.
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
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.
Question: how can access test classes from application classes? Alternatively, how can Maven package a fat jar including test classes and execute tests?
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.
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)
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.
So, how to create in Maven a fat jar with test classes and external dependencies?
The Maven Assembly Plugin is a perfect candidate in this case.
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.
You also need to create a descriptor file, in the srcmainassembly
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>
The configuration above is:
- 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
).
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:
- Create a JUni test suite
- Add to the test suite the concerned tests
- Invoke the test suite from your plain Java main
Example of test suite:
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 {
}
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 中并执行它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!