问题描述
我有一个项目结构:
├── main
│ ├── java
│ │ └── qbs
│ │ ├── QbsApplication.java
│ └── resources
│ ├── application.properties
|
└── test
└── java
└── qbs
└── QbsApplicationTests.java
我已经用mvn clean:install
构建它并创建了jar文件.现在,我想使用命令行运行QbsApplicationTests
.为此,我将两个罐子放在一个目录中:
I have build it with mvn clean:install
and created jar file. Now I want to run the QbsApplicationTests
using the command line.To do it I have put two jars into one dir:
-rw-rw-r--. 1 a a 29M 04-05 10:30 fi.qbs-0.0.1-SNAPSHOT.jar
-rw-rw-r--. 1 a a 1M 2014-12-04 junit-4.12.jar
并执行以下命令:
java -cp .:fi.qbs-0.0.1-SNAPSHOT.jar:junit-4.12.jar org.junit.runner.JUnitCore qbs.QbsApplication
但是,我不断收到以下错误消息
However, I keep getting the following error
JUnit version 4.12
.E
Time: 0,003
There was 1 failure:
1) initializationError(org.junit.runner.JUnitCommandLineParseResult)
java.lang.IllegalArgumentException: Could not find class [qbs.QbsApplication]
at org.junit.runner.JUnitCommandLineParseResult.parseParameters(JUnitCommandLineParseResult.java:102)
at org.junit.runner.JUnitCommandLineParseResult.parseArgs(JUnitCommandLineParseResult.java:50)
at org.junit.runner.JUnitCommandLineParseResult.parse(JUnitCommandLineParseResult.java:44)
at org.junit.runner.JUnitCore.runMain(JUnitCore.java:72)
at org.junit.runner.JUnitCore.main(JUnitCore.java:36)
Caused by: java.lang.ClassNotFoundException: qbs.QbsApplication
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:348)
at org.junit.internal.Classes.getClass(Classes.java:16)
at org.junit.runner.JUnitCommandLineParseResult.parseParameters(JUnitCommandLineParseResult.java:100)
... 4 more
FAILURES!!!
Tests run: 1, Failures: 1
问题:
- 我应该如何运行
QbsApplicationTests
测试表单控制台?
- How should I run the
QbsApplicationTests
tests form console?
编辑我还尝试添加以下内容:
EDITI have also tried to add the following:
@SpringBootApplication
public class QbsApplication {
public static void main(String[] args) {
SpringApplication.run(QbsApplication.class, args);
System.out.println("Running tests!");
JUnitCore engine = new JUnitCore();
engine.addListener(new TextListener(System.out) ); // required to print reports
engine.run( QbsApplicationTests.class);
}
}
到主类,但是Intellij一直说QbsApplicationTests
无法解析.
to the main class, but Intellij keeps saying that the QbsApplicationTests
cannot be resolved.
推荐答案
之所以在树形结构中拆分源类和测试类,是因为在运送最终的jar时,客户对测试类不感兴趣.他们只关心您编写的实际代码.
The reason you split up your source and test classes in the tree structure like that is because when shipping your final jar, your clients are not interested in the test classes. They only care about the actual code you have written.
这就是为什么使用maven构建jar会自动排除您的测试目录的原因.
That is why building the jar with maven will automatically exclude your test directory.
但是,您想测试代码的正确性是正确的.但是您要在之前先完成此操作,然后再构建罐子进行运输.
However, you are right that you want to test the correctness of your code. But you want to do this before you build the jar for shipping.
像建议的在此运行mvn test
或者因为您正在使用IntelliJ,所以只需右键单击测试类,然后单击运行".
Or since you're using IntelliJ, just right click on the test class and click "run".
这篇关于当JUnit找不到测试类时,如何在jar中运行JUnit测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!