3

@RunWithアノテーションをクラスに適用せずに、このランナーを使用してテストを起動するEclipseプラグインの一部になりたいカスタムJUnitランナーを作成しました。org.eclipse.debug.ui.launchShortcuts拡張ポイントを使用して、「実行」コンテキストメニューの下に追加の項目を取得することができました。ただし、カスタムランナーを使用してテストを呼び出す方法がわかりません。

4

1 に答える 1

3

それで、私は自分がやりたいことをする方法を考え出しました。しかし、それは少しハッキーに見えます。しかし、他の誰かが同じ問題に遭遇した場合に備えて、ここに回答を投稿すると思いました。

まず、次のようなjunitの種類を登録する必要があります。

<extension point="org.eclipse.jdt.junit.internal_testKinds">
      <kind
            id="my.junit.kind"
            displayName="Your Kind Name" 
            finderClass="org.eclipse.jdt.internal.junit.launcher.JUnit4TestFinder"
            loaderPluginId="org.eclipse.jdt.junit4.runtime"
            loaderClass="your.test.loader.MyLoaderClass">
         <runtimeClasspathEntry pluginId="org.eclipse.jdt.junit4.runtime" />
         <runtimeClasspathEntry pluginId="org.eclipse.jdt.junit.core" />        
         <runtimeClasspathEntry pluginId="org.eclipse.jdt.junit.runtime"/>    
      </kind>
   </extension>

xmlで、カスタム実装を指定する必要があります。カスタム実装はorg.eclipse.jdt.internal.junit.runner.ITestLoader、の実装を返しますorg.eclipse.jdt.internal.junit.runner.ITestReference。カスタムJUnitランナーのインスタンスを作成する場所であるため、コア部分はITestReferenceの実装です。

  public class MyTestReference extends JUnit4TestReference
   {

      public MyTestReference(final Class<?> p_clazz, String[] p_failureNames)
      {
         super(new Request()
         {
            @Override
            public Runner getRunner()
            {
               return new MyCustomRunner(p_clazz);
            }

         }, p_failureNames);
      }
...
}

最後に、これを、種類を適切に設定する起動ショートカットとリンクする必要があります

public class MyJunitLaunchShortcut extends JUnitLaunchShortcut
{
   @Override
   protected ILaunchConfigurationWorkingCopy createLaunchConfiguration(IJavaElement p_element) throws CoreException
   {
      ILaunchConfigurationWorkingCopy config = super.createLaunchConfiguration(p_element);
      config.setAttribute(JUnitLaunchConfigurationConstants.ATTR_TEST_RUNNER_KIND, "my.junit.kind");
      return config;
   }
}

これは多くの内部クラスを使用するので、おそらくより良い方法があります。しかし、これはうまくいくようです。

于 2012-11-13T01:47:34.737 に答える