簡體   English   中英

使用Junit 4.4或更高版本運行Eclipse Junit插件測試 - 為什么沒有檢測到測試?

[英]Running Eclipse Junit Plugin tests with Junit 4.4 or newer — why aren't tests detected?

我需要在一組eclipse插件測試中使用JUnit 4.4(或更新版本),但我遇到了以下問題:

使用springsource( junit44junit45 )中的junit 4.4或4.5 bundle運行時,未檢測到測試。 可以使用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,則會發生這種情況。 將junit 4.4依賴項添加到我的MANIFEST.MF文件后,錯誤就消失了,所有測試都被執行了。 junit依賴項應該是可選的,因為插件通常只需要測試代碼。

我現在無法測試這個,因為我沒有方便的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 ,所以我會堅持下去。

我想知道您是否需要從com.springsource.org.junit導入@Test標記而不是從org.junit導入.Test標記。

沃爾克

最近在jMock,JUnit和Eclipse方面遇到了一些類似的聲音問題,盡管不能用插件測試。

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

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

我還發現我必須像這樣使用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() {

我不知道它是哪個版本的JUnit,但為了成功找到測試,測試方法名稱必須以“ test ”開頭。

在較新的版本中,您只需使用@Test標記測試,對我而言,它適用於此組合:

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

也許您的JUnit包缺少MANIFEST.MF中的條目:

動態導入包:*

從其他包中加載類是必需的。

Bebbo

ActivatorTest需要擴展TestCase

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

公共類AllTestSuite {

public static Test suite(){

     return new JUnit4TestAdapter(AllTestSuite .class);
 }

}

我認為spring測試框架與junit 4.4+不兼容

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM