【问题标题】:Running Eclipse Junit Plugin tests with Junit 4.4 or newer -- why aren't tests detected?使用 Junit 4.4 或更高版本运行 Eclipse Junit 插件测试——为什么没有检测到测试?
【发布时间】:2010-09-20 02:09:02
【问题描述】:

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

使用 springsource 的 junit 4.4 或 4.5 包运行时未检测到测试 (junit44 和 junit45)。可以用 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 版本)。

【问题讨论】:

  • 我有完全相同的问题,我似乎无法找到任何有关原因的见解。显然,其他人也没有:(
  • 现在我已经回到了 junit 4.3(和早期版本的 jmock)。我认为下一步可能是向 Spring Source 报告有关其捆绑包的错误,和/或尝试创建新的 junit 4.4 捆绑包以查看捆绑包或 junit 是否存在问题。
  • 这将在未回答问题队列的顶部停留多长时间?
  • 啊,我(终于)想出了如何让这个社区可编辑...我没有时间评估下面的建议,所以如果有人可以,至少他们现在可以将这些信息正确地放入问题中(如果需要,我可以授予接受的答案——我不确定社区问题如何处理接受的答案)
  • 遇到了同样的问题。添加此行(如上所示)对我有用:Require-Bundle: org.junit4;bundle-version="4.3.1"

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


【解决方案1】:

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

【讨论】:

  • 感谢您的回答,这正是解决我的难题的原因。我在运行 Junit 测试时遇到了困难,因为我预计 Eclipse 的“插件 Junit 测试”配置会自动添加它
  • 添加 org.junit 作为可选依赖项也解决了我的“不可运行方法”问题。
【解决方案2】:

我认为spring测试框架不兼容junit 4.4+

【讨论】:

    【解决方案3】:

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

    公共类 AllTestSuite {

    公共静态测试套件(){

         return new JUnit4TestAdapter(AllTestSuite .class);
     }
    

    }

    【讨论】:

      【解决方案4】:

      我在使用 jMock、JUnit 和 Eclipse recently 时遇到了一些类似的问题,尽管 plugin 测试确实没有。

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

      • jmock-2.5.1.jar
      • hamcrest-core-1.1.jar
      • hamcrest-library-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() {
      

      【讨论】:

        【解决方案5】:

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

        沃尔克

        【讨论】:

          【解决方案6】:

          我现在无法测试这个,因为我没有手边的 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。从您的问题来看,springsource 似乎在 com.springsource.org.junit 下重新打包了 JUnit,但您在代码中只使用了 org.junit,所以我会坚持下去。

          【讨论】:

          • 重新打包只是为了提供 junit 作为 OSGi 包。 com.springsource.org.junit 只是包的名称,与包提供的实际包无关(除了您使用 . 命名的约定。
          • 下次有机会我会尝试@RunWith(..) 的想法——我们现在只使用junit 3.8 :(。我对改变测试运行有一些疑问会起作用,只是因为测试必须有一个非常特定的插件环境才能工作,我不确定这是否包括测试运行器。
          • 遇到了同样的问题,@RunWith(Junit4.class) 也解决了我的问题。
          【解决方案7】:

          ActivatorTest 需要扩展 TestCase

          【讨论】:

          • reppimp 说的对,我发帖时我没有意识到他正在使用 JUnit 4。
          【解决方案8】:

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

          动态导入包:*

          这是从其他包加载类的强制性要求。

          贝博

          【讨论】:

            【解决方案9】:

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

            在较新的版本中,您可以简单地用@Test 标记测试, 对我来说,它适用于这种组合:

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

            【讨论】:

            • JUnit 4 之前的所有 JUnit 版本。不过,这种“旧风格”仍然有效。
            • ...如果您查看我的测试用例,它会同时使用(@Test 和 v.
            猜你喜欢
            • 1970-01-01
            • 2014-05-24
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-10-11
            • 1970-01-01
            • 2010-10-31
            相关资源
            最近更新 更多