使用Junit 4.4或更高版本运行Eclipse Junit插件测试 - 为什么没有检测到测试?

时间:2008-10-30 21:10:11

标签: java eclipse unit-testing junit eclipse-pde

我需要在一组eclipse插件测试中使用JUnit 4.4(或更新版本),但我遇到了以下问题:

使用springsource的junit 4.4或4.5软件包运行时,未检测到测试 (junit44junit45)。可以使用eclipse获得的org.junit4包提供junit 4.3(从Ganymead / Eclipse 3.4开始)。 org.junit4包 工作,它识别并运行测试,但它与最新版本的JMock不兼容,我需要使用模拟库。

以下是一个示例测试:

package testingplugin;

import static org.junit.Assert.*;
import org.junit.Test;

public class ActivatorTest {
   @Test
   public final void testDoaddTest() {
      fail("Not yet implemented");
   }
}

运行此测试时,我收到以下异常:

java.lang.Exception: No runnable methods
    at org.junit.internal.runners.TestClassMethodsRunner.run(TestClassMethodsRunner.java:33)
    at org.junit.internal.runners.TestClassRunner$1.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)

但是,如果我将项目依赖项从com.springsource.org.junit切换到org.junit4,则测试运行并失败(如预期的那样)。

我在Eclipse中运行测试作为JUnit插件测试,具有以下程序参数:

-os $ {target.os} -ws $ {target.ws} -arch $ {target.arch} -nl $ {target.nl}

在启动期间选择了以下插件(由我选择,然后我使用“添加必需的插件”来获取其余的依赖项。):

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)

这是我的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"

将最后一行切换为:

Require-Bundle: org.junit4;bundle-version="4.3.1"

在发布时将选定的插件更新为:

Workspace:
   testingPlugin
Target Platform:
   org.junit4 (4.3.1)
   ...bunches of auto-selected bundles... (again, nothing else test related)

使测试正常运行(但使用错误版本的junit)。

9 个答案:

答案 0 :(得分:4)

根据我的经验,如果包含插件测试的插件不依赖于junit,则会发生这种情况。将junit 4.4依赖项添加到我的MANIFEST.MF文件后,错误就消失了,所有测试都被执行了。 junit依赖项应该是可选的,因为插件通常只需要测试代码。

答案 1 :(得分:2)

我现在无法对此进行测试,因为我没有方便的Eclipse 3.4安装,但我刚刚在(我认为)IntelliJ IDEA 7.0.x中遇到了类似的问题,并且解决方法是明确的指定一个测试运行器。

使用JUnit 4.5:

import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
public class ActivatorTest {
    //...
}

如果这不起作用,您可以使用org.junit.runners.BlockJUnit4ClassRunner

获得更多成功

对于JUnit 4.4,我会尝试org.junit.internal.runners.JUnit4ClassRunner

编辑:对com.springsource.部分不太确定,因为我不使用Springsource。从你的问题来看,springource似乎在com.springsource.org.junit下重新打包JUnit,但你在代码中仅使用org.junit,所以我会坚持下去。

答案 2 :(得分:1)

我想知道您是否需要从com.springsource.org.junit导入@Test标记而不是从org.junit导入。

沃尔克

答案 3 :(得分:1)

我和jMock,JUnit& amp; Eclipse recently,虽然不能用插件测试。

我不确定它是否相关,但我完全使用以下版本: -

  • JMock的-2.5.1.jar
  • hamcrest核-1.1.jar
  • hamcrest库-1.1.jar
  • JMock的-junit4-2.5.1.jar

我还发现我必须像这样使用JMock test runner: -

  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() {

答案 4 :(得分:0)

我不知道它是哪个版本的JUnit,但为了成功找到测试,测试方法名称必须以“ test ”开头。

在较新版本中,您只需使用@Test标记测试, 对我来说,它适用于这种组合:

import static junit.framework.Assert.*;
...
@Test
    public void testDummy() throws Exception

答案 5 :(得分:0)

也许您的JUnit包缺少MANIFEST.MF中的条目:

动态导入包:*

从其他包中加载类是必须的。

Bebbo

答案 6 :(得分:0)

ActivatorTest需要扩展TestCase

答案 7 :(得分:0)

@RunWith(Suite.class) @SuiteClasses({UserServiceTest.class,ABCServiceTest.class})

公共类AllTestSuite {

public static Test suite(){

     return new JUnit4TestAdapter(AllTestSuite .class);
 }

}

答案 8 :(得分:0)

我认为spring测试框架与junit 4.4 +

不兼容