JUnit测试在Eclipse中不运行

时间:2023-02-05 10:31:05

This is a snapshot of the eclipse editor. You can see the test and the JUnit tab. When I try to run the test, it says Terminated.

这是eclipse编辑器的快照。您可以看到测试和JUnit选项卡。当我尝试运行测试时,它说终止。

JUnit测试在Eclipse中不运行

Anybody know what is going on here?

有人知道这是怎么回事吗?

Thanks

谢谢

10 个解决方案

#1


3  

One possibility is that Eclipse's JUnit runner is confused by the presence of the main method and is running that rather than the test methods.

一种可能是,Eclipse的JUnit运行器被主方法的存在搞混了,它正在运行主方法,而不是测试方法。

Try commenting out the main method. It shouldn't be there at all, IMO.

尝试注释出主要方法。它根本不应该在那里,在我看来。

(By the way, the main method doesn't look right to me. I'd have thought it would instantiate a TestSuite with the unit test classes as parameters, but you seem to be giving it the class under test. That won't have any "test" methods, which would explain why no tests are being run ...)

)顺便说一下,我觉得主要的方法不太对。我本以为它会用单元测试类作为参数实例化一个TestSuite,但您似乎给了它一个正在测试的类。它没有任何“测试”方法,这可以解释为什么没有运行测试……

#2


2  

Like already mentioned, remove the main method, might helpt. Any particular reason for extend from TestCase that is Juni3 if I remember correctly? Did you try Junit 4 by using @Test annotation

如前所述,删除主方法,可能会获得。如果我没记错的话,从Juni3测试中有什么特别的原因吗?您尝试过使用@Test注释Junit 4吗

for instance:

例如:

@Test
public void testSomething() {
     //you stuff
}

#3


2  

Not sure if your problem is already solved.

不确定你的问题是否已经解决。

I had a similar issue which got solved when I corrected the Project -> Properties -> Java Compiler -> JDK Compliance settings.

我有一个类似的问题,当我纠正项目->属性-> Java编译器-> JDK遵从性设置时得到了解决。

I had a conflict between 1.5 and 1.6. When I reverted back to 1.5 (which was the current active JRE), my JUnits started working again.

我在1.5和1.6之间发生了冲突。当我恢复到1.5(这是当前的活动JRE)时,JUnits又开始工作了。

#4


0  

Manually define the run behavior: Right click -> Run As -> Run Configurations:

手动定义运行行为:右击->运行为->运行配置:

Then you could define the every details of the test case: test class, test methods, class path, junit version, JVM arguments, etc.

然后您可以定义测试用例的所有细节:测试类、测试方法、类路径、junit版本、JVM参数等等。

#5


0  

When I saw no tests being run I opened the console and saw the following error:

当我看到没有运行测试时,我打开控制台并看到以下错误:

java.lang.UnsupportedClassVersionError: junit/framework/Test (Unsupported major.minor version 49.0)
   at java.lang.ClassLoader.defineClass0(Native Method)
   at java.lang.ClassLoader.defineClass(Unknown Source)
   at java.security.SecureClassLoader.defineClass(Unknown Source)
   at java.net.URLClassLoader.defineClass(Unknown Source)
   at java.net.URLClassLoader.access$100(Unknown Source)
   at java.net.URLClassLoader$1.run(Unknown Source)
   at java.security.AccessController.doPrivileged(Native Method)
   at java.net.URLClassLoader.findClass(Unknown Source)
   at java.lang.ClassLoader.loadClass(Unknown Source)
   at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
   at java.lang.ClassLoader.loadClass(Unknown Source)
   at java.lang.ClassLoader.loadClassInternal(Unknown Source)
   at java.lang.Class.forName0(Native Method)
   at java.lang.Class.forName(Unknown Source)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.loadTestLoaderClass(RemoteTestRunner.java:328)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.createRawTestLoader(RemoteTestRunner.java:318)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.createLoader(RemoteTestRunner.java:313)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.defaultInit(RemoteTestRunner.java:291)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.init(RemoteTestRunner.java:212)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)

After further investigation I realized that the junit framework I was using was built against a 1.6 jre and I was building my unit tests against a 1.3 jre. When I built my unit tests against 1.6 my tests ran. Alternatively, I suppose I could have built (or found) junit against 1.3 and I could have been in good shape. I went the path of least resistance.

在进一步的调查之后,我意识到我使用的junit框架是基于1.6 jre构建的,我是基于1.3 jre构建单元测试的。当我在1.6的基础上构建单元测试时,我的测试运行了。或者,我认为我可以根据1.3构建(或发现)junit,而且我的状态很好。我走了一条阻力最小的路。

#6


0  

Happened to me when I had two jars of junit on the classpath. One jar was hidden inside selenium server standalone. I fixed the issue by removing the other jar from the classpath and letting the runtime use the junit jar in the selenium server standalone. I couldn't find any messages in the console about a class conflict or java version warning.

当我在类路径上有两个junit jar时,这种情况就发生了。一个jar单独隐藏在selenium服务器中。通过从类路径中删除另一个jar并让运行时在selenium服务器中独立使用junit jar,我解决了这个问题。我无法在控制台找到关于类冲突或java版本警告的消息。

#7


0  

In my case was that I had done a refactoring and the .classpath was not updated and the console was showing an error about invalid class. After updating the classpath file, everything started to work again.

在我的例子中,我进行了重构,.classpath没有被更新,控制台显示了一个关于无效类的错误。在更新类路径文件之后,一切重新开始工作。

#8


0  

Not sure if this helps, but in my case the I had to update my project configuration Maven->update Project configuration before Junit finally started responding

我不确定这是否有用,但在我的情况下,我必须在Junit最终开始响应之前更新我的项目配置Maven->更新项目配置

#9


0  

In my case I was using an inner class which was not being identified as eligible for tests. The fix was to make that class a static inner class like this:

在我的例子中,我使用的是一个内部类,它没有被标识为合格的测试。修正的方法是使这个类成为像这样的静态内部类:

public class MyClass { //... class I wanted to test
}

This inner class (i.e. in the same file) is used for testing MyClass - but eclipse does not found it ... unless it is static.

这个内部类(即在同一个文件中)用于测试MyClass——但是eclipse没有找到它……除非它是静态的。

public static class MyClassTest  { // ....
}

#10


0  

Make sure you don't have another process blocking execution. In my case, building workspace was sleeping, blocking all junit execution.

确保没有另一个进程阻塞执行。在我的例子中,构建工作区处于休眠状态,阻塞了所有的junit执行。

#1


3  

One possibility is that Eclipse's JUnit runner is confused by the presence of the main method and is running that rather than the test methods.

一种可能是,Eclipse的JUnit运行器被主方法的存在搞混了,它正在运行主方法,而不是测试方法。

Try commenting out the main method. It shouldn't be there at all, IMO.

尝试注释出主要方法。它根本不应该在那里,在我看来。

(By the way, the main method doesn't look right to me. I'd have thought it would instantiate a TestSuite with the unit test classes as parameters, but you seem to be giving it the class under test. That won't have any "test" methods, which would explain why no tests are being run ...)

)顺便说一下,我觉得主要的方法不太对。我本以为它会用单元测试类作为参数实例化一个TestSuite,但您似乎给了它一个正在测试的类。它没有任何“测试”方法,这可以解释为什么没有运行测试……

#2


2  

Like already mentioned, remove the main method, might helpt. Any particular reason for extend from TestCase that is Juni3 if I remember correctly? Did you try Junit 4 by using @Test annotation

如前所述,删除主方法,可能会获得。如果我没记错的话,从Juni3测试中有什么特别的原因吗?您尝试过使用@Test注释Junit 4吗

for instance:

例如:

@Test
public void testSomething() {
     //you stuff
}

#3


2  

Not sure if your problem is already solved.

不确定你的问题是否已经解决。

I had a similar issue which got solved when I corrected the Project -> Properties -> Java Compiler -> JDK Compliance settings.

我有一个类似的问题,当我纠正项目->属性-> Java编译器-> JDK遵从性设置时得到了解决。

I had a conflict between 1.5 and 1.6. When I reverted back to 1.5 (which was the current active JRE), my JUnits started working again.

我在1.5和1.6之间发生了冲突。当我恢复到1.5(这是当前的活动JRE)时,JUnits又开始工作了。

#4


0  

Manually define the run behavior: Right click -> Run As -> Run Configurations:

手动定义运行行为:右击->运行为->运行配置:

Then you could define the every details of the test case: test class, test methods, class path, junit version, JVM arguments, etc.

然后您可以定义测试用例的所有细节:测试类、测试方法、类路径、junit版本、JVM参数等等。

#5


0  

When I saw no tests being run I opened the console and saw the following error:

当我看到没有运行测试时,我打开控制台并看到以下错误:

java.lang.UnsupportedClassVersionError: junit/framework/Test (Unsupported major.minor version 49.0)
   at java.lang.ClassLoader.defineClass0(Native Method)
   at java.lang.ClassLoader.defineClass(Unknown Source)
   at java.security.SecureClassLoader.defineClass(Unknown Source)
   at java.net.URLClassLoader.defineClass(Unknown Source)
   at java.net.URLClassLoader.access$100(Unknown Source)
   at java.net.URLClassLoader$1.run(Unknown Source)
   at java.security.AccessController.doPrivileged(Native Method)
   at java.net.URLClassLoader.findClass(Unknown Source)
   at java.lang.ClassLoader.loadClass(Unknown Source)
   at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
   at java.lang.ClassLoader.loadClass(Unknown Source)
   at java.lang.ClassLoader.loadClassInternal(Unknown Source)
   at java.lang.Class.forName0(Native Method)
   at java.lang.Class.forName(Unknown Source)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.loadTestLoaderClass(RemoteTestRunner.java:328)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.createRawTestLoader(RemoteTestRunner.java:318)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.createLoader(RemoteTestRunner.java:313)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.defaultInit(RemoteTestRunner.java:291)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.init(RemoteTestRunner.java:212)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)

After further investigation I realized that the junit framework I was using was built against a 1.6 jre and I was building my unit tests against a 1.3 jre. When I built my unit tests against 1.6 my tests ran. Alternatively, I suppose I could have built (or found) junit against 1.3 and I could have been in good shape. I went the path of least resistance.

在进一步的调查之后,我意识到我使用的junit框架是基于1.6 jre构建的,我是基于1.3 jre构建单元测试的。当我在1.6的基础上构建单元测试时,我的测试运行了。或者,我认为我可以根据1.3构建(或发现)junit,而且我的状态很好。我走了一条阻力最小的路。

#6


0  

Happened to me when I had two jars of junit on the classpath. One jar was hidden inside selenium server standalone. I fixed the issue by removing the other jar from the classpath and letting the runtime use the junit jar in the selenium server standalone. I couldn't find any messages in the console about a class conflict or java version warning.

当我在类路径上有两个junit jar时,这种情况就发生了。一个jar单独隐藏在selenium服务器中。通过从类路径中删除另一个jar并让运行时在selenium服务器中独立使用junit jar,我解决了这个问题。我无法在控制台找到关于类冲突或java版本警告的消息。

#7


0  

In my case was that I had done a refactoring and the .classpath was not updated and the console was showing an error about invalid class. After updating the classpath file, everything started to work again.

在我的例子中,我进行了重构,.classpath没有被更新,控制台显示了一个关于无效类的错误。在更新类路径文件之后,一切重新开始工作。

#8


0  

Not sure if this helps, but in my case the I had to update my project configuration Maven->update Project configuration before Junit finally started responding

我不确定这是否有用,但在我的情况下,我必须在Junit最终开始响应之前更新我的项目配置Maven->更新项目配置

#9


0  

In my case I was using an inner class which was not being identified as eligible for tests. The fix was to make that class a static inner class like this:

在我的例子中,我使用的是一个内部类,它没有被标识为合格的测试。修正的方法是使这个类成为像这样的静态内部类:

public class MyClass { //... class I wanted to test
}

This inner class (i.e. in the same file) is used for testing MyClass - but eclipse does not found it ... unless it is static.

这个内部类(即在同一个文件中)用于测试MyClass——但是eclipse没有找到它……除非它是静态的。

public static class MyClassTest  { // ....
}

#10


0  

Make sure you don't have another process blocking execution. In my case, building workspace was sleeping, blocking all junit execution.

确保没有另一个进程阻塞执行。在我的例子中,构建工作区处于休眠状态,阻塞了所有的junit执行。