Java 为什么我的测试没有运行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2639018/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 10:13:42  来源:igfitidea点击:

Why is my test not running

javajunit

提问by Gandalf StormCrow

Strangest thing happened my test was running ok and now its not anymore I didn't change the code at all here is the exception :

最奇怪的事情发生了,我的测试运行正常,现在不再是我根本没有更改代码,这里有一个例外:

Class not found com.example.test
java.lang.ClassNotFoundException: com.example.test
    at java.net.URLClassLoader.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.loadClass(RemoteTestRunner.java:693)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.loadClasses(RemoteTestRunner.java:429)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:452)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

采纳答案by cjstehno

I see that you are using Eclipse, but are you also using some other external build/test tool like Ant or Maven? Sometimes Eclipse can get out of sync when also using other external tools.

我看到您正在使用 Eclipse,但您是否也在使用其他一些外部构建/测试工具,如 Ant 或 Maven?有时 Eclipse 在使用其他外部工具时可能会不同步。

If you are using external tools, do whatever is necessary to clean up generated artifacts (e.g. mvn clean). Then refresh the Eclipse project and do a clean build as suggested previously. Then try running your unit test again using Eclipse.

如果您使用外部工具,请执行任何必要的操作来清理生成的工件(例如 mvn clean)。然后刷新 Eclipse 项目并按照之前的建议进行干净的构建。然后尝试使用 Eclipse 再次运行单元测试。

Good luck and hope this helps.

祝你好运,希望这能帮到你。

回答by GuruKulki

JVM is not able to find the class test. is the class name correct. I mean is it not com.example.Test?

JVM 无法找到类测试。类名是否正确。我的意思是不是 com.example.Test 吗?

回答by Fazal

Check run config of the JUnit test. In the classpath tab, resotre default entries once in case that is not the case already. In project properties check if both junit package and classes source folder is in the source package and there is an actual class compilation existring for the class under test in your expected target folder.

检查 JUnit 测试的运行配置。在类路径选项卡中,重新设置默认条目一次,以防情况并非如此。在项目属性中,检查 junit 包和类源文件夹是否都在源包中,并且在预期的目标文件夹中是否存在针对被测类的实际类编译。

回答by Andrey Belyaev

I had similar issue and problem was because of broken build paths to external libraries (e.g. Selenium server). So first of all please open Build path (right click project -> Build Path -> Configure Build Path) and verify that all external libraries can be loaded successfully (there are messages like 'cannot load'). Most likely a library was renamed/moved while optimization or adding external tools like Ant. Hope this helps and it will save your time goggling around :)

我有类似的问题,问题是由于外部库(例如 Selenium 服务器)的构建路径损坏。所以首先请打开构建路径(右键项目->构建路径->配置构建路径)并验证所有外部库都可以成功加载(有“无法加载”之类的消息)。很可能是在优化或添加外部工具(如 Ant)时重命名/移动了库。希望这会有所帮助,它会节省您四处寻找的时间:)

WBR, Andrey

WBR,安德烈

回答by ashah

If you use maven to build your project then it puts all compiled java classes in folder like target/classes and Test classes under target/test-classes. So I guess eclipse won't able to find classes from target/test-classes. If you want to run JUnit classes from Eclipse, use Eclipse->Project->clean and then you will have all classes under same target configured in eclipse.

如果您使用 maven 来构建您的项目,那么它会将所有已编译的 java 类放在文件夹中,如 target/classes 和 test classes 下的 target/test-classes。所以我猜 eclipse 将无法从目标/测试类中找到类。如果您想从 Eclipse 运行 JUnit 类,请使用 Eclipse->Project->clean,然后您将在 Eclipse 中配置相同目标下的所有类。