junit.core.java:Code Content

  1. package org.junit.runner;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import junit.runner.Version;
  5. import org.junit.internal.runners.OldTestClassRunner;
  6. import org.junit.internal.runners.TextListener;
  7. import org.junit.runner.notification.Failure;
  8. import org.junit.runner.notification.RunListener;
  9. import org.junit.runner.notification.RunNotifier;
  10. /**
  11.  * <code>JUnitCore</code> is a facade for running tests. It supports running JUnit 4 tests, 
  12.  * JUnit 3.8.x tests, and mixtures. To run tests from the command line, run 
  13.  * <code>java org.junit.runner.JUnitCore TestClass1 TestClass2 ...</code>.
  14.  * For one-shot test runs, use the static method {@link #runClasses(Class[])}. 
  15.  * If you want to add special listeners,
  16.  * create an instance of {@link org.junit.runner.JUnitCore} first and use it to run the tests.
  17.  * 
  18.  * @see org.junit.runner.Result
  19.  * @see org.junit.runner.notification.RunListener
  20.  * @see org.junit.runner.Request
  21.  */
  22. public class JUnitCore {
  23. private RunNotifier fNotifier;
  24. /**
  25.  * Create a new <code>JUnitCore</code> to run tests.
  26.  */
  27. public JUnitCore() {
  28. fNotifier= new RunNotifier();
  29. }
  30. /**
  31.  * Run the tests contained in the classes named in the <code>args</code>.
  32.  * If all tests run successfully, exit with a status of 0. Otherwise exit with a status of 1.
  33.  * Write feedback while tests are running and write
  34.  * stack traces for all failed tests after the tests all complete.
  35.  * @param args names of classes in which to find tests to run
  36.  */
  37. public static void main(String... args) {
  38. Result result= new JUnitCore().runMain(args);
  39. killAllThreads(result);
  40. }
  41. private static void killAllThreads(Result result) {
  42. System.exit(result.wasSuccessful() ? 0 : 1);
  43. }
  44. /**
  45.  * Run the tests contained in <code>classes</code>. Write feedback while the tests
  46.  * are running and write stack traces for all failed tests after all tests complete. This is
  47.  * similar to {@link #main(String[])}, but intended to be used programmatically.
  48.  * @param classes Classes in which to find tests
  49.  * @return a {@link Result} describing the details of the test run and the failed tests.
  50.  */
  51. public static Result runClasses(Class<?>... classes) {
  52. return new JUnitCore().run(classes);
  53. }
  54. /**
  55.  * Do not use. Testing purposes only.
  56.  */
  57. public Result runMain(String... args) {
  58. System.out.println("JUnit version " + Version.id());
  59. List<Class<?>> classes= new ArrayList<Class<?>>();
  60. List<Failure> missingClasses= new ArrayList<Failure>();
  61. for (String each : args)
  62. try {
  63. classes.add(Class.forName(each));
  64. } catch (ClassNotFoundException e) {
  65. System.out.println("Could not find class: " + each);
  66. Description description= Description.createSuiteDescription(each);
  67. Failure failure= new Failure(description, e);
  68. missingClasses.add(failure);
  69. }
  70. RunListener listener= new TextListener();
  71. addListener(listener);
  72. Result result= run(classes.toArray(new Class[0]));
  73. for (Failure each : missingClasses)
  74. result.getFailures().add(each);
  75. return result;
  76. }
  77. /**
  78.  * @return the version number of this release
  79.  */
  80. public String getVersion() {
  81. return Version.id();
  82. }
  83. /**
  84.  * Run all the tests in <code>classes</code>.
  85.  * @param classes the classes containing tests
  86.  * @return a {@link Result} describing the details of the test run and the failed tests.
  87.  */
  88. public Result run(Class<?>... classes) {
  89. return run(Request.classes("All", classes));
  90. }
  91. /**
  92.  * Run all the tests contained in <code>request</code>.
  93.  * @param request the request describing tests
  94.  * @return a {@link Result} describing the details of the test run and the failed tests.
  95.  */
  96. public Result run(Request request) {
  97. return run(request.getRunner());
  98. }
  99. /**
  100.  * Run all the tests contained in JUnit 3.8.x <code>test</code>. Here for backward compatibility.
  101.  * @param test the old-style test
  102.  * @return a {@link Result} describing the details of the test run and the failed tests.
  103.  */
  104. public Result run(junit.framework.Test test) { 
  105. return run(new OldTestClassRunner(test));
  106. }
  107. /**
  108.  * Do not use. Testing purposes only.
  109.  */
  110. public Result run(Runner runner) {
  111. Result result= new Result();
  112. RunListener listener= result.createListener();
  113. addFirstListener(listener);
  114. try {
  115. fNotifier.fireTestRunStarted(runner.getDescription());
  116. runner.run(fNotifier);
  117. fNotifier.fireTestRunFinished(result);
  118. } finally {
  119. removeListener(listener);
  120. }
  121. return result;
  122. }
  123. private void addFirstListener(RunListener listener) {
  124. fNotifier.addFirstListener(listener);
  125. }
  126. /**
  127.  * Add a listener to be notified as the tests run.
  128.  * @param listener the listener to add
  129.  * @see org.junit.runner.notification.RunListener
  130.  */
  131. public void addListener(RunListener listener) {
  132. fNotifier.addListener(listener);
  133. }
  134. /**
  135.  * Remove a listener.
  136.  * @param listener the listener to remove
  137.  */
  138. public void removeListener(RunListener listener) {
  139. fNotifier.removeListener(listener);
  140. }
  141. }
java.lang.IllegalArgumentException: Parameter 'directory' is not a directory at org.apache.commons.io.FileUtils.listFiles(FileUtils.java:293) at org.apache.commons.io.FileUtils.listFiles(FileUtils.java:378) at com.bosssoft.hr.train.j2se.util.UtilsDemo.method4(UtilsDemo.java:133) at Test1.testUtilsDemo4(Test1.java:66) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306) at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63) at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329) at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293) at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306) at org.junit.runners.ParentRunner.run(ParentRunner.java:413) at org.junit.runner.JUnitCore.run(JUnitCore.java:137) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69) at com.intellij.rt.junit.IdeaTestRunner$Repeater$1.execute(IdeaTestRunner.java:38) at com.intellij.rt.execution.junit.TestsRepeater.repeat(TestsRepeater.java:11) at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35) at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:232) at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:55)源码如下:public void method4(){ // 获取目录中的所有文件和子目录 Collection<File> files = FileUtils.listFiles(new File("com/bosssoft/hr/train/j2se/util"), new String[]{"*.java"},true); // 遍历文件和子目录 if (files != null) { for (File file : files) { if (file.isDirectory()) { log.info("Directory: " + file.getName()); } else { log.info("File: " + file.getName()); } } } }
07-14
根据提供的代码和堆栈跟踪,异常是由`FileUtils.listFiles`方法引起的,错误消息为"Parameter 'directory' is not a directory"。这意味着传递给`FileUtils.listFiles`方法的目录参数不是一个有效的目录。 在你的代码中,你将目录路径传递给了`FileUtils.listFiles`方法,但这个目录实际上是一个类路径(com/bosssoft/hr/train/j2se/util),而不是一个有效的文件系统目录。因此,方法无法找到该目录并抛出异常。 要解决这个问题,你需要传递一个有效的文件系统目录路径给`FileUtils.listFiles`方法。你可以尝试使用绝对路径或相对路径来指定目录。确保指定的目录路径存在并且是一个目录,而不仅仅是一个类路径。 例如,如果你想遍历`com/bosssoft/hr/train/j2se/util`这个类路径下的文件和子目录,你可以使用以下代码: ```java URL resource = UtilsDemo.class.getClassLoader().getResource("com/bosssoft/hr/train/j2se/util"); if (resource != null) { File directory = new File(resource.getFile()); Collection<File> files = FileUtils.listFiles(directory, new String[]{"java"}, true); // 遍历文件和子目录 if (files != null) { for (File file : files) { if (file.isDirectory()) { log.info("Directory: " + file.getName()); } else { log.info("File: " + file.getName()); } } } } ``` 在这个示例中,我们使用`getClassLoader().getResource`方法获取类路径的URL,然后将其转化为文件路径并创建一个`File`实例。然后,我们使用这个有效的目录路径调用`FileUtils.listFiles`方法来获取文件和子目录的集合。 希望这可以帮助你解决问题!如果还有其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值