Java 使用 Junit 4.4 或更新版本运行 Eclipse Junit 插件测试——为什么没有检测到测试?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/251791/
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
Running Eclipse Junit Plugin tests with Junit 4.4 or newer -- why aren't tests detected?
提问by rcreswick
I need to use JUnit 4.4 (or newer) in a set of eclipse plugin tests, but I've run into the following problem:
我需要在一组 eclipse 插件测试中使用 JUnit 4.4(或更新版本),但我遇到了以下问题:
Tests are not detected when running with the junit 4.4 or 4.5 bundles from springsource (junit44and junit45). The org.junit4 bundle that can be obtained with eclipse supplies junit 4.3 (as of Ganymead / Eclipse 3.4). The org.junit4 bundle doeswork in that it identifies and runs the tests, but it is not compatible with the latest versions of JMock, and I need to use a mocking library.
使用来自 springsource(junit44和junit45)的 junit 4.4 或 4.5 包运行时,未检测到测试。可以通过 eclipse 获得的 org.junit4 包提供了 junit 4.3(从 Ganymead / Eclipse 3.4 开始)。org.junit4 包确实可以识别并运行测试,但它与最新版本的 JMock 不兼容,我需要使用模拟库。
Here is a sample test:
这是一个示例测试:
package testingplugin;
import static org.junit.Assert.*;
import org.junit.Test;
public class ActivatorTest {
@Test
public final void testDoaddTest() {
fail("Not yet implemented");
}
}
When running this test, I receive the following exception:
运行此测试时,我收到以下异常:
java.lang.Exception: No runnable methods
at org.junit.internal.runners.TestClassMethodsRunner.run(TestClassMethodsRunner.java:33)
at org.junit.internal.runners.TestClassRunner.runUnprotected(TestClassRunner.java:42)
at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34)
at org.junit.internal.runners.TestClassRunner.run(TestClassRunner.java:52)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:45)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.pde.internal.junit.runtime.RemotePluginTestRunner.main(RemotePluginTestRunner.java:62)
at org.eclipse.pde.internal.junit.runtime.CoreTestApplication.run(CoreTestApplication.java:23)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.eclipse.equinox.internal.app.EclipseAppContainer.callMethodWithException(EclipseAppContainer.java:574)
at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:195)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:382)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:179)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:549)
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:504)
at org.eclipse.equinox.launcher.Main.run(Main.java:1236)
at org.eclipse.equinox.launcher.Main.main(Main.java:1212)
However, if I switch the project dependencies from com.springsource.org.junit to org.junit4, then the test runs and fails (as expected).
但是,如果我将项目依赖项从 com.springsource.org.junit 切换到 org.junit4,则测试运行并失败(如预期)。
I am running the test as a JUnit Plug-in Test in Eclipse, with the following program arguments:
我在 Eclipse 中将测试作为 JUnit 插件测试运行,使用以下程序参数:
-os ${target.os} -ws ${target.ws} -arch ${target.arch} -nl ${target.nl}
-os ${target.os} -ws ${target.ws} -arch ${target.arch} -nl ${target.nl}
The following plug-ins selected during launch (selected by me, then I used "add required plugins" to get the rest of the dependencies.):
启动时选择的以下插件(由我选择,然后我使用“添加所需的插件”来获取其余的依赖项。):
Workspace:
testingPlugin
Target Platform:
com.springsource.org.hamcrest.core (1.1.0)
com.springsource.org.junit (4.5.0)
....and a bunch of others... (nothing related to testing was auto-selected)
Here is my MANIFEST.MF:
这是我的 MANIFEST.MF:
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: TestingPlugin Plug-in
Bundle-SymbolicName: testingPlugin
Bundle-Version: 1.0.0
Bundle-Activator: testingplugin.Activator
Import-Package: org.osgi.framework;version="1.3.0"
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Require-Bundle: com.springsource.org.junit;bundle-version="4.5.0"
Switching the last line to:
将最后一行切换为:
Require-Bundle: org.junit4;bundle-version="4.3.1"
And updating the selected plugins at launch to:
并在启动时将选定的插件更新为:
Workspace:
testingPlugin
Target Platform:
org.junit4 (4.3.1)
...bunches of auto-selected bundles... (again, nothing else test related)
Causes the test to run properly (but with the wrong version of junit).
使测试正常运行(但使用错误的 junit 版本)。
回答by Drejc
I don't know which version of JUnit it was, but to succesfully find test the test methods name must start with the word "test".
我不知道它是哪个版本的 JUnit,但要成功找到测试,测试方法名称必须以单词“ test”开头。
In newer version you can simply mark test with @Test, for me it works in this combination:
在较新的版本中,您可以简单地使用 @Test 标记测试,对我来说它可以在以下组合中使用:
import static junit.framework.Assert.*;
...
@Test
public void testDummy() throws Exception
回答by Drejc
Maybe your JUnit bundle is missing an entry in the MANIFEST.MF:
也许您的 JUnit 包在 MANIFEST.MF 中缺少一个条目:
Dynamic-Import-Package: *
动态导入包:*
This is mandatory to load classes from other bundles.
这是从其他包加载类所必需的。
Bebbo
贝博
回答by shsmurfy
ActivatorTest needs to extend TestCase
ActivatorTest 需要扩展 TestCase
回答by Thomas Dufour
I cannot test this right now as I don't have an Eclipse 3.4 installation handy, but I've run across a similar problem a while ago in (I think) IntelliJ IDEA 7.0.x, and a workaround was to explicitly specify a test runner.
我现在无法对此进行测试,因为我手头没有 Eclipse 3.4 安装,但不久前我在(我认为)IntelliJ IDEA 7.0.x 中遇到了类似的问题,一种解决方法是明确指定一个测试赛跑者。
With JUnit 4.5:
使用 JUnit 4.5:
import org.junit.runners.JUnit4;
@RunWith(JUnit4.class)
public class ActivatorTest {
//...
}
If this does not work you may have more success with org.junit.runners.BlockJUnit4ClassRunner
如果这不起作用,您可能会取得更大的成功 org.junit.runners.BlockJUnit4ClassRunner
For JUnit 4.4 I would try org.junit.internal.runners.JUnit4ClassRunner
对于 JUnit 4.4,我会尝试 org.junit.internal.runners.JUnit4ClassRunner
EDIT : not too sure about the com.springsource.
part as I don't use Springsource. From your question it seems that springsource repackages JUnit under com.springsource.org.junit
but you use just org.junit
in your code so I'll stick with that.
编辑:com.springsource.
对这部分不太确定,因为我不使用 Springsource。从您的问题来看,springsource 似乎重新打包了 JUnit,com.springsource.org.junit
但您只org.junit
在代码中使用,所以我会坚持下去。
回答by Volker Stolz
I'm wondering whether you might need to import the @Test tag from com.springsource.org.junit and not from org.junit.
我想知道您是否需要从 com.springsource.org.junit 而不是从 org.junit 导入 @Test 标签。
Volker
沃尔克
回答by James Mead
I had some similar sounding problems with jMock, JUnit & Eclipse recently, although admittedly not with plugintests.
我最近在使用 jMock、JUnit 和 Eclipse 时遇到了一些类似的问题,但不可否认的是插件测试没有。
I'm not sure if it's relevant, but I got it all working with the following versions :-
我不确定它是否相关,但我在以下版本中都可以使用:-
- jmock-2.5.1.jar
- hamcrest-core-1.1.jar
- hamcrest-library-1.1.jar
- jmock-junit4-2.5.1.jar
- jmock-2.5.1.jar
- hamcrest-core-1.1.jar
- hamcrest-library-1.1.jar
- jmock-junit4-2.5.1.jar
I also found I had to use the JMock test runnerlike this :-
我还发现我必须像这样使用JMock 测试运行器:-
import org.junit.Test;
import org.junit.runner.RunWith;
import org.jmock.Mockery;
import org.jmock.Expectations;
import org.jmock.integration.junit4.JUnit4Mockery;
import org.jmock.integration.junit4.JMock;
@RunWith(JMock.class)
public class PublisherTest {
Mockery context = new JUnit4Mockery();
@Test
public void oneSubscriberReceivesAMessage() {
回答by James Mead
@RunWith(Suite.class) @SuiteClasses( { UserServiceTest.class,ABCServiceTest.class })
@RunWith(Suite.class) @SuiteClasses( { UserServiceTest.class,ABCServiceTest.class })
public class AllTestSuite {
公共类 AllTestSuite {
public static Test suite() {
公共静态测试套件(){
return new JUnit4TestAdapter(AllTestSuite .class);
}
}
}
回答by ssmithstone
I think the spring testing framework is not compatible with junit 4.4+
我认为 spring 测试框架与 junit 4.4+ 不兼容
回答by ssmithstone
In my experience this happens if the plugin which contains the plugin tests does not depend on junit. After adding the junit 4.4 dependency to my MANIFEST.MF file the error went away and all tests were executed. The junit dependency should be optional because the plugin usually only needs it for the test code.
根据我的经验,如果包含插件测试的插件不依赖于 junit,就会发生这种情况。将 junit 4.4 依赖项添加到我的 MANIFEST.MF 文件后,错误消失并执行了所有测试。junit 依赖项应该是可选的,因为插件通常只需要它用于测试代码。