简体   繁体   English

Junit(JAVA)中找不到类异常

[英]Class not found exception in Junit (JAVA)

I've seen similar questions however all are related to maven (I'm not using maven). 我见过类似的问题,但是所有问题都与Maven有关(我没有使用Maven)。 I'm running intellij and have the Junit.jar 4.12 added in the library. 我正在运行intellij,并在库中添加了Junit.jar 4.12。 I can create test methods and classes. 我可以创建测试方法和类。

The problem is that when I try to run my TestRunner() main method i instantly get the following error 问题是当我尝试运行TestRunner()主方法时,我立即收到以下错误

Exception in thread "main" java.lang.ClassNotFoundException: TestRunner
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:122)

And the code looks like this: 代码看起来像这样:

import junit.framework.TestSuite;
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;

public class TestRunner {
    public static void main(String[] args) {
        Result res = JUnitCore.runClasses(TestSuite.class);
        for (Failure fail : res.getFailures()) {
            System.out.println(fail.toString());
        }
        System.out.println(res.wasSuccessful());
    }
}

The thing that confuses me is that I had the exact same code in eclipse (for a previous project test) and it worked perfectly fine there. 让我感到困惑的是,我在eclipse中使用了完全相同的代码(对于以前的项目测试),并且在那儿工作得很好。 What am I missing? 我想念什么?

  • Closing and restarting intellij did not work. 关闭并重新启动intellij无效。
  • Re-importing libraries didn't change anything either. 重新导入库也没有任何改变。

Update, the problem was that I had not "Correctly" added the library. 更新,问题是我没有“正确”添加库。 Apparently in Intellij, one must write @Test and then hit "Alt+enter" to prompt the auto fixes and choose the option to import JUnit that way. 显然,在Intellij中,必须编写@Test,然后单击“ Alt + enter”以提示自动修复,并选择以这种方式导入JUnit的选项。 So it automatically adds it to the classpath. 因此,它会自动将其添加到类路径中。 Which it didn't do properly if done manually. 如果手动完成,则无法正常执行。

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

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