简体   繁体   中英

How to run JUnit Test Classes in Java Main Application using Maven and Eclipse

I want to automate my JUnit tests. For my tests I use Maven and other framework, for example RESTassured etc.

These tests work if I start them normally via 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.

The idea is to create jar file, so that the tests can be carried out without much effort.

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:

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.

However, the question remains why I can not outsource the Main class?

Folder Structure

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM