无法在Eclipse中运行jUnit测试

7

我使用新版Eclipse。使用jUnit创建演示测试(我已经添加了内置于Eclipse中的默认jUnit库)。然后我编写了以下代码:

import junit.framework.*;

import org.junit.Test;

public class SimpleTest extends TestCase { 
   public SimpleTest(String name) { 
      super(name);
   }
   public final void main(String method){

   }

   @Test
   public final void testSimpleTest() {
      int answer = 2;
      assertEquals((1+1), answer); 
   }
}

但是它无法运行。在调试选项卡中:

org.eclipse.jdt.internal.junit.runner.RemoteTestRunner at localhost:52754 
Thread [main] (Suspended (exception ClassNotFoundException)) 
URLClassLoader$1.run() line: not available [local variables unavailable] 
AccessController.doPrivileged(PrivilegedExceptionAction<T>, AccessControlContext) line: not available [native method] 
Launcher$AppClassLoader(URLClassLoader).findClass(String) line: not available 
Launcher$AppClassLoader(ClassLoader).loadClass(String, boolean) line: not available 
Launcher$AppClassLoader.loadClass(String, boolean) line: not available 
Launcher$AppClassLoader(ClassLoader).loadClass(String) line: not available 

我该如何解决这个问题?
2个回答

6

在调试模式下,删除异常断点,或者切换到非调试模式。

在调试视图中,点击右上方的 断点 选项卡,并且取消勾选所有异常断点,例如 ClassNotFoundException,然后重新运行测试。


我没有断点。当我在非调试模式下运行时,它会显示“找不到主类:org.eclipse.jdt.internal.junit.runner.RemoteTestRunner。程序将退出。” - KimKha
@BalusC 谢谢(再次)。虽然这可能没有帮助到KimKha,但它解决了我遇到的仅在调试模式下出现的问题。 - Tim

5

像许多人一样,您可能会混淆JUnit 3和JUnit 4。如果您使用JUnit 3,请将测试命名为“test*”并从TestCase继承。如果您使用JUnit 4,请使用注释。


2
虽然这是真的,但这并不是问题的根本原因。将代码复制粘贴到编辑器中,在 ClassNotFoundException 上设置断点并以调试模式运行。你会发现,直到移除断点之前都存在完全相同的问题。 - BalusC
很高兴了解这两个版本之间的区别。 - James P.
怎么做?我写了新代码:import static org.junit.Assert.*; import org.junit.Test;public class SimpleTest { @Test public final void abcTest() { int answer = 2; assertEquals((1+1), answer); } }但还是出错了。 - KimKha
@KimKha,这应该可以正常运行(我已经尝试过了),你能提供一下异常吗?请确保依赖项中有JUnit 4。 - Gabriel Ščerbák
@KimKha,我也尝试了你的原始示例,它可以工作(尽管是v3和v4混合)。我尝试修复它(删除了TestCase supr类和super构造函数调用),只有在拥有构造函数时才遇到问题,但在删除后运行得很好。我想到的另一件事是,您是否像JUnit测试或Java应用程序一样运行它。在Eclipse中,您应该使用Alt + Shift + X,T来运行测试。 - Gabriel Ščerbák
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接