简体   繁体   English

在 Mac 中从命令行运行 Junit 的语法

[英]The syntax for running Junit from commandline in Mac

I want to run Junit from commandline in Mac OS but Im not able to get it to work as expected with a simple test class that I have created.我想在 Mac OS 中从命令行运行 Junit,但我无法使用我创建的简单测试类让它按预期工作。

I have downloaded junit-4.12.jar for this purpose and been writing a very simple test class with one simple test method.为此,我下载了junit-4.12.jar ,并使用一个简单的测试方法编写了一个非常简单的测试类。

Im running with the following command to compile the class:我使用以下命令运行以编译类:

javac -cp .:/Full/path/to/junit/junit-4.12.jar DemoTest.java 

..and running the following command to execute the compiled class: ..并运行以下命令来执行编译后的类:

java -cp .:/Full/path/to/junit/junit-4.12.jar org.junit.runner.JUnitCore DemoTest 

import static org.junit.Assert.assertEquals;
import org.junit.Test;

public class DemoTest {

    @Test
    public void evaluate(){
         assertEquals(true, 1<2);
    }
}

I'm expecting to see "test pass" when I am running the junit class from commandline, because that's what it should generate.当我从命令行运行 junit 类时,我期待看到“测试通过”,因为这就是它应该生成的。 Instead Im getting the following error message (please note that Im only pasting the first line of error for the sake of simplicity):相反,我收到以下错误消息(请注意,为了简单起见,我只粘贴了第一行错误):

Exception in thread "main" java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing线程“main”中的异常 java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing

Does this mean that Im only missing the hamcrest.jar file to be able to run the application as expected?这是否意味着我只是缺少hamcrest.jar文件才能按预期运行应用程序?

Please let me know if you need the complete error stacktrace!如果您需要完整的错误堆栈跟踪,请告诉我!

As the error message作为错误信息

Exception in thread "main" java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing线程“main”中的异常 java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing

mentioned you miss the hamcrest-core library in your class path.提到您错过了课程路径中的hamcrest-core库。 Download it and add it to the classpath下载并添加到类路径

java -cp .:/Full/path/to/junit/junit-4.12.jar:/Full/path/to/hamcrest/hamcrest-core-1.3.jar  org.junit.runner.JUnitCore DemoTest 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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