JUnit4 を使用して実行される Ant PDE テスト
質問
pde-maven-plugin、maven 呼び出し、または ant タスクを使用して、pde テストを自動化しようとしています。記事の指示に従いました Ant を使用した Eclipse PDE 単体テストの自動化 test.xmlを作成するには
テストが失敗しました。まず、テストが見つかりませんでした
junit.framework.AssertionFailedError: junit.framework.AssertionFailedError: No tests found in com.example.TestSuite
at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
検索したところ、使用する必要があることがわかりました JUnit4Adapter
, 、しかし、そうすることで私は得ました ClassCastException
, 、これがスタックトレースです
java.lang.ClassCastException: junit.framework.JUnit4TestAdapter cannot be cast to junit.framework.Test
at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestLoader.getTest(JUnit3TestLoader.java:108)
at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestLoader.loadTests(JUnit3TestLoader.java:59)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:452)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
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:196)
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:368)
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:559)
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:514)
at org.eclipse.equinox.launcher.Main.run(Main.java:1311)
at org.eclipse.equinox.launcher.Main.main(Main.java:1287)
私のプラグインの依存関係は次のとおりです。
Require-Bundle:
org.eclipse.ui,
org.eclipse.core.runtime,
org.eclipse.jdt.core;bundle-version="3.4.2",
org.eclipse.jdt.launching;bundle-version="3.4.1",
org.eclipse.core.resources;bundle-version="3.4.1",
org.apache.xerces;bundle-version="2.9.0",
org.junit4;bundle-version="4.3.1"
そしてテストクラス
package com.example;
import junit.framework.JUnit4TestAdapter;
import junit.framework.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
@RunWith(Suite.class)
@SuiteClasses( { SimpleTests.class})
public class TestSuite {
public static Test suite() {
return new JUnit4TestAdapter(TestSuite.class);
}
}
修正方法はありますか?
解決
これは、次の行を追加することによって固定されている
<arg line="-testLoaderClass org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader"/>
<arg line="-loaderpluginname org.eclipse.jdt.junit4.runtime"/>
はここで完全なJava Antタスクです。
<java dir="${plugin.dir}"
fork="yes"
classname="org.eclipse.equinox.launcher.Main"
classpathref="equinox.launcher.class.path"
jvm="${jvm}">
<arg line="-application ${application}"/>
<arg line="-data ${test.reports.dir}/output/ws"/>
<arg line="-port ${pde.test.port}"/>
<arg line="-dev bin -clean -debug"/>
<arg line="-testpluginname ${plugin.name}"/>
<arg line="-testLoaderClass org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader"/>
<arg line="-loaderpluginname org.eclipse.jdt.junit4.runtime"/>
<arg line="-classnames ${test.classes.list}"/>
<jvmarg line="${vmargs}"/>
</java>
他のヒント
私も Noura さんと同じ問題を抱えており、彼らが提供した修正を試してみましたが、残念ながら問題の一部しか解決できませんでした。前述したものを投げる代わりに、 ClassCastException
, 、もらいました junit4 java.lang.Exception: No runnable methods
, 私の 29 個のテストには注意深く注釈が付けられているにもかかわらず、 @Test
.
数時間の検索と (JUnit 4 が新しい頃の本当に古いバグしか見つかりませんでした)、実験を行った後 (このSO投稿 どちらも役に立ちませんでした)、修正の 2 番目の部分がクラスパスに関連していることがわかりました。
私はクラスパスを定義していました(これは起動に使用されました) PDETestResultsCollector
, PDETestPortLocator
およびテストプラグイン) は次のようになります。
<path id="test.class.path">
<pathelement location="build/my-bundled-jar-with-tests.jar" /> <!-- contains my app and the PDE* helper classes -->
<fileset dir="${test.eclipse.dir}/plugins">
<include name="org.junit_4.*/**/junit.jar" />
<include name="org.hamcrest.core_*.jar" />
<include name="org.apache.ant_*/**/ant.jar" />
<include name="org.apache.ant_*/**/ant-junit.jar" />
<include name="org.eclipse.jdt.core_*.jar" />
<include name="org.eclipse.jdt.junit_*.jar" />
<include name="org.eclipse.jdt.junit.core_*.jar" />
<include name="org.eclipse.equinox.common_*.jar" />
<include name="org.eclipse.debug.core_*.jar" />
<include name="org.eclipse.osgi_*.jar" />
<include name="org.eclipse.core.resources_*.jar" />
<include name="org.eclipse.core.runtime_*.jar" />
<include name="org.eclipse.core.jobs_*.jar" />
<include name="org.eclipse.ui.workbench_*.jar" />
<include name="org.eclipse.swt_*.jar" />
<include name="org.eclipse.equinox.preferences_*.jar" />
<include name="org.eclipse.equinox.launcher_*.jar" />
</fileset>
</path>
そして次のように起動しました:
<java classpathref="test.class.path" classname="org.eclipse.equinox.launcher.Main" fork="yes" dir="${basedir}" >
<arg line="-application org.eclipse.pde.junit.runtime.uitestapplication" />
<arg line="-data reports/output/ws" />
<arg line="-dev bin -clean -port ${pde.test.port}" />
<arg line="-testApplication org.eclipse.ui.ide.workbench" />
<arg line="-testpluginname my-bundled-plugin-with-tests" />
<arg line="-testLoaderClass org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader" />
<arg line="-loaderpluginname org.eclipse.jdt.junit4.runtime" />
<arg line="-classnames TestAllMyCode" />
</java>
結局のところ、 build/my-bundled-jar-with-tests.jar
クラスパス上にあるとテストランナーが混乱するため、クラスファイルだけを含む新しいjarを作成する必要がありました。 PDETestListener
, PDETestPortLocator
そして PDETestResultsCollector
(pde-test.jar と呼ばれます)。私の固定パスは次のとおりです。
<path id="test.class.path">
<pathelement location="build/pde-test.jar" /> <!-- fixed the problem -->
<fileset dir="${test.eclipse.dir}/plugins">
<include name="org.junit_4.*/**/junit.jar" />
<include name="org.hamcrest.core_*.jar" />
... <!-- same as before -->
そして、29 個のテストがすべて見つかり、実行されました。