[英]How to run JUnit Test Classes in Java Main Application using Maven and Eclipse
I want to automate my JUnit tests. 我想自动化我的JUnit测试。 For my tests I use Maven and other framework, for example RESTassured etc.
对于我的测试,我使用Maven和其他框架,例如RESTassured等。
These tests work if I start them normally via Eclipse. 如果我通过Eclipse正常启动它们,这些测试将起作用。 Now I want to create a Java application, so that the test classes, in a Main class, can be processed automatically via a console.
现在,我想创建一个Java应用程序,以便可以通过控制台自动处理Main类中的测试类。
The idea is to create jar file, so that the tests can be carried out without much effort. 这个想法是创建jar文件,以便可以轻松进行测试。
For this I have set up the following main and test class: 为此,我设置了以下主要和测试类:
main class: 主类:
package com.openidm.ui;
import java.util.Scanner;
import org.junit.internal.TextListener;
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import com.openidm.firsttest.FirstTest;
public class UserInterFace {
static Scanner scanner;
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) {
JUnitCore junitCore = new JUnitCore();
scanner = new Scanner(System.in);
int input = 0;
while (input != 100) {
System.out.println("\n\n Test Menu");
System.out.println("--------------------------------------");
System.out.println("1 - Testnumber 1");
System.out.println("2 - Testnumber 2");
System.out.print("\nSelect a Menu Option: ");
input = Integer.parseInt(scanner.next());
switch (input) {
case 1:
junitCore.addListener(new TextListener(System.out));
Result result = junitCore.run(FirstTest.class);
System.out.println(result);
break;
case 2:
System.out.println("Testnumber 2");
break;
case 100:
break;
default:
System.out.println("Invalid Input");
}
}
}
}
test class: 测试类别:
package com.openidm.firsttest;
import org.junit.Test;
import com.openidm.testbases.TestBase;
import io.restassured.RestAssured;
public class FirstTest extends TestBase {
/**
* Get All User IDs
*/
@Test
public void Test001() {
System.out.println("--------------------All User IDs---------------");
RestAssured.given()
.spec(requestSpec)
.queryParam("_queryId", "query-all-ids")
.when()
.get("/user")
.body()
.prettyPrint();
RestAssured.given()
.spec(requestSpec)
.queryParam("_queryId", "query-all-ids")
.when().get("/user")
.then()
.statusCode(200);
}
If I now start the Java application, I get the following error message: 如果现在启动Java应用程序,则会收到以下错误消息:
Exception in thread "main" java.lang.NoClassDefFoundError:
com/openidm/firsttest/FirstTest
at com.openidm.ui.UserInterFace.main(UserInterFace.java:39)
Caused by: java.lang.ClassNotFoundException: com.openidm.firsttest.FirstTest
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 1 more
Can someone help me please 有人能帮助我吗
Thanks for your quick reply. 感谢您的快速回复。 I solved the problem by moving the Main class to the test packages.
我通过将Main类移至测试包来解决了这个问题。
However, the question remains why I can not outsource the Main class? 但是,问题仍然是为什么我不能将Main Class外包?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.