Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如果类在包中,如何从命令行运行JUnit测试?_Java_Junit_Illegalargumentexception_Classnotfound_Junit Runner - Fatal编程技术网

Java 如果类在包中,如何从命令行运行JUnit测试?

Java 如果类在包中,如何从命令行运行JUnit测试?,java,junit,illegalargumentexception,classnotfound,junit-runner,Java,Junit,Illegalargumentexception,Classnotfound,Junit Runner,类ParserTest位于包myproject.tests中,并存储在此目录结构中: . └── myproject └── tests └── ParserTest.class 设置当前shell会话的类路径(no-cp选项,以保持java调用干净): 调用JUnit运行程序并将测试类作为参数传递: java org.junit.runner.JUnitCore myproject/tests/ParserTest 将抛出此错误: ... 1) initializa

ParserTest
位于包
myproject.tests
中,并存储在此目录结构中:

.
└── myproject
    └── tests
        └── ParserTest.class
设置当前shell会话的类路径(no
-cp
选项,以保持java调用干净):

调用JUnit运行程序并将测试类作为参数传递:

java org.junit.runner.JUnitCore myproject/tests/ParserTest
将抛出此错误:

...
1) initializationError(org.junit.runner.JUnitCommandLineParseResult)
java.lang.IllegalArgumentException: Could not find class [myproject/tests/ParserTest]
...

org.junit.runner.JUnitCore
要求参数使用
作为目录和类之间的分隔符,而不是
/

调用跟踪看起来像这样,以
java.lang.Class.forName(classname)
结尾:

java.lang.Class.forName(String className)
的JavaDoc说明:

参数:className-所需类的完全限定名


在Java中,完全限定名是用
而不是
/
org.junit.runner.JUnitCore
编写的。JUnitCore要求参数使用
作为目录和类之间的分隔符,而不是
//code>

调用跟踪看起来像这样,以
java.lang.Class.forName(classname)
结尾:

java.lang.Class.forName(String className)
的JavaDoc说明:

参数:className-所需类的完全限定名


在Java中,完全限定名是用
而不是
/

编写的。我花了一些时间才弄明白这一点,因为我在StackExchange上没有找到任何东西(使用我的搜索词),我写这个Q/A是为了帮助那些面临相同问题并使用相同行话搜索解决方案的人。我花了一些时间才弄明白这一点,因为我在StackExchange上没有找到任何东西(使用我的搜索词),所以我写这个Q/A是为了帮助那些面临相同问题并使用相同行话搜索解决方案的人。
...
1) initializationError(org.junit.runner.JUnitCommandLineParseResult)
java.lang.IllegalArgumentException: Could not find class [myproject/tests/ParserTest]
...
JUnitCore.main(args)
└── JUnitCore.runMain(args)
    └── JUnitCommandLineParseResult.parse(args)
        └── new JUnitCommandLineParseResult().parseArgs(args);
            └── JUnitCommandLineParseResult.parseParameters(...);
                └── ...
                    └── org.junit.internal.Classes.getClass(arg);
                        └── java.lang.Class.forName(className);