[英]Running Junit from the command line (Mac)
这一定是显而易见的,但是在这里和其他地方看完之后,我陷入了困境。 我想从命令行运行Junit测试。 我执行了脚本化的部署过程,并且想要在进行部署之前进行验证。
我找到了这个,但出现错误(更多信息请参见下文)。 如何从命令行运行JUnit测试用例
我可以运行它:java -cp。:/ path / junit-4.8.1.jar org.junit.runner.JUnitCore HappyPath.class
我在.class文件所在的目录中。
$ ls -lart
total 40
-rw-r--r-- 1 rdejournett staff 144 Sep 22 12:09 package-info.class
-rw-r--r-- 1 rdejournett staff 11001 Sep 22 12:09 HappyPath.class
drwxr-xr-x 5 rdejournett staff 170 Sep 22 12:09 ..
-rw-r--r-- 1 rdejournett staff 660 Sep 22 12:13 AllTests.class
drwxr-xr-x 5 rdejournett staff 170 Sep 22 12:13 .
但是输出是:
JUnit version 4.8.1
Could not find class: HappyPath.class
Time: 0
OK (0 tests)
我需要创建一个JAR文件吗?
HappyPath类看起来像这样。
@Test
public static void happyPath() {
String xml = "";
xml = ReadJson.ReadFile("/app/mirth/UnitTests/GEoutput.xml");
Statements s = new Statements();
// need XmlDocRoot tag or whatever to parse it properly
try {
s = ConvertXmltoObj(xml);
happyPathStatement(s);
happyPathGuarantor(s);
happyPathAging(s);
happyPathEncounters(s);
happyPathEncounterCharges(s);
} catch (JAXBException e) {
e.printStackTrace();
fail();
} catch (ParserConfigurationException e) {
e.printStackTrace();
fail();
} catch (SAXException e) {
e.printStackTrace();
fail();
} catch (IOException e) {
e.printStackTrace();
fail();
}
assertTrue(true);
}
更新:
删除了.class,因此命令如下:
java -cp .:/path/junit-4.8.1.jar org.junit.runner.JUnitCore HappyPath
立即收到此错误:
JUnit version 4.8.1
Exception in thread "main" java.lang.NoClassDefFoundError: HappyPath (wrong name: com/xxx/xxx/datamodel/ge/HappyPath)
at java.lang.ClassLoader.defineClass1(Native Method)
经过大量研究后,我最终找到了解决方案。
这样的SO大大有助于找出根本原因。
我最终运行的命令是:
java -cp .:/app/mirth/UnitTests/junit-4.8.1.jar:/app/mirth/UnitTests/hamcrest-core-1.3.jar:/Applications/Mirth\ Connect/custom-lib/estatement-obj.jar com.xxx.xxx.datamodel.ge.HappyPath
从此目录:
/Users/me/mfss-rcm/mfss-rcm-mirth/Myproject/target/test-classes
基本上,您需要从根开始并使用FQN,它被java解释为相对路径。 这样类文件实际上就在这里:
/Users/me/mfss-rcm/mfss-rcm-mirth/Myproject/target/test-classes/com/xxx/xxx/datamodel/ge
我需要添加junit jar,一个依赖jar和我的实际代码以jar形式进行测试(不知道如何解决将其打包为jar的要求,无论如何,这没什么大不了的。)。
我还向测试类添加了main方法,该方法称为测试类。
public static void main(String[] args) {
System.err.println("Starting Happy Path Testing");
happyPath();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.