我正在 Eclipse 中编写 JUnit-Tests 来测试没有 Maven的 RCP-Eclipse-Application 。对于某些测试用例,必须使用 Powermock(使用 Easymock)来模拟对象创建和静态方法。
我有一个包含应该是 testet 的类的插件和包含相应测试的第二个插件。让我们捐赠它们 PluginObject 和 PluginTest:
工作情况:
* PluginObject
- src
- ClassUnderTest
* PluginTest
- src
- TestCase
- lib
- easymock
- powermock
- ... [aditional classes (like JavaAssist)]
使用 PLuginTest 中带有 powermock-Jar 的配置,TestCase 和 Plugins-Manifest-Runtim 中的配置运行正常并返回所需的结果。
我的 TestCase 从以下几行开始,当前使用@RunWith -Annotation:
@RunWith(PowerMockRunner.class)
@PowerMockIgnore({ "javax.management.*",
"javax.xml.parsers.*",
"com.sun.org.apache.xerces.internal.jaxp.*",
"javax.xml.*",
"ch.qos.logback.classic.*",
"org.slf4j.*",
"org.eclipse.core.runtime.jobs.ISchedulingRule" })
@PrepareForTest ({Controller.class,ProcessLoaderFactory.class,Engine.class,CommandCollector.class})
@SuppressStaticInitializationFor({"Controller"," ProcessLoaderFactory","Engine","CommandCollector"})
public class ControllerTest {
...
[skipped package information due to copyrights]
但是我有多个插件想要在我的应用程序中使用 Powermock 进行模拟,这对我来说将共享库提取到另一个特殊插件似乎是个好主意 - 我们称之为 Test.Util:
* PluginObject
- src
- ClassUnderTest
* PluginTest
- src
- TestCase
* Test.Util
- lib
- easymock
- powermock
- ... [aditional classes (like JavaAssist)]
对于除 Powermock 之外的所有其他库,我没有任何问题,但是当我将 Powermock 从 PluginTest 移动到 Test.Util-Plugin 时,在启动 JUnit Plugin-Test 时初始化时出现“ClassNotFoundException”。(正常的 JUnit-Test 不会抛出此错误)
java.lang.ClassNotFoundException: xxx.yyy.zzz.MyTest
at org.eclipse.osgi.internal.loader.BundleLoader.findClassInternal(BundleLoader.java:513)
at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:429)
at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:417)
at org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader.loadClass(DefaultClassLoader.java:107)
at java.lang.ClassLoader.loadClass(Unknown Source)
at org.powermock.core.classloader.MockClassLoader.loadModifiedClass(MockClassLoader.java:143)
at org.powermock.core.classloader.DeferSupportingClassLoader.loadClass(DeferSupportingClassLoader.java:67)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.createDelegatorFromClassloader(JUnit4TestSuiteChunkerImpl.java:133)
at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.createDelegatorFromClassloader(JUnit4TestSuiteChunkerImpl.java:39)
at org.powermock.tests.utils.impl.AbstractTestSuiteChunkerImpl.createTestDelegators(AbstractTestSuiteChunkerImpl.java:217)
at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.<init>(JUnit4TestSuiteChunkerImpl.java:59)
at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.<init>(AbstractCommonPowerMockRunner.java:32)
at org.powermock.modules.junit4.PowerMockRunner.<init>(PowerMockRunner.java:31)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:31)
at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:24)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:29)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:24)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.<init>(JUnit4TestReference.java:33)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestClassReference.<init>(JUnit4TestClassReference.java:25)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:48)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:38)
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.PlatformUITestHarness$1.run(PlatformUITestHarness.java:47)
at org.eclipse.swt.widgets.RunnableLock.run(RunnableLock.java:35)
at org.eclipse.swt.widgets.Synchronizer.runAsyncMessages(Synchronizer.java:135)
at org.eclipse.swt.widgets.Display.runAsyncMessages(Display.java:4140)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3757)
at org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.java:2696)
at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:2660)
at org.eclipse.ui.internal.Workbench.access$4(Workbench.java:2494)
at org.eclipse.ui.internal.Workbench$7.run(Workbench.java:674)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:667)
at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149)
at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:123)
at org.eclipse.pde.internal.junit.runtime.NonUIThreadTestApplication.runApp(NonUIThreadTestApplication.java:54)
at org.eclipse.pde.internal.junit.runtime.UITestApplication.runApp(UITestApplication.java:41)
at org.eclipse.pde.internal.junit.runtime.NonUIThreadTestApplication.start(NonUIThreadTestApplication.java:48)
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:344)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:179)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:622)
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:577)
at org.eclipse.equinox.launcher.Main.run(Main.java:1410)
at org.eclipse.equinox.launcher.Main.main(Main.java:1386)
我在 MANIFEST.MF 的 Runtime 下的 Classpath 中配置了 Test.Util,并导出了这些插件的每个包。在 Eclipse 中,正确的库是从 TestCase 链接的。
我还使用带有 JUnit-Rule 而不是@RunWith的 Bootstrapping ,但它会导致相同的 ClassNotFoundException。问题没有改变。
我现在尝试了很多东西 - 重新配置插件测试的运行配置,在清单中设置 ClassPath: .\,延迟/非延迟加载以及为 PluginTest 和 Test.Util-Plugin 设置 Buddy-ClassLoading。
经过数小时的网络尝试和搜索,我仍然不知道为什么 Powermock 会导致此异常,如果它是从 Test-Plugin 提取到第三个插件中,以及我如何使这项工作正常工作。我目前不能说我的应用程序中是否有错误配置。
有谁遇到过相同或类似的问题,或者得到了很好的解决方案?