我正在努力让PowerMockito使用我的应用程序。我已将库添加到构建路径中。我有以下测试类:
package client.controller;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
@RunWith(PowerMockRunner.class)
@PrepareForTest(System.class)
public class SampleTest
{
@Test
public void test()
{
}
}
当我尝试运行测试时,我得到以下异常:
java.lang.NoClassDefFoundError: org/junit/internal/runners/TestClassRunner
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
at org.powermock.modules.junit4.legacy.PowerMockRunner.<init>(PowerMockRunner.java:24)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
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.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.lang.ClassNotFoundException: org.junit.internal.runners.TestClassRunner
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
... 31 more
根据PowerMock FAQ,我应该尝试使用旧版程序包。我做了,我仍然得到同样的例外。有什么想法吗?
答案 0 :(得分:3)
看起来我正在错误地将PowerMock添加到构建中。当我查看Eclipse中的问题视图时,我发现了以下内容:
Archive for required library: 'lib/powermock-mockito-junit-1.4.11 4/powermock-release-junit-without-mock-framework-1.4.11.pom' in project 'Test' cannot be read or is not a valid ZIP file Test Build path Build Path Problem
Archive for required library: 'lib/powermock-mockito-junit-1.4.11 4/powermock-release-without-test-and-mock-framework-1.4.11.pom' in project 'Test' cannot be read or is not a valid ZIP file Test Build path Build Path Problem
从构建路径中删除这两个文件解决了这个问题。
答案 1 :(得分:1)
对我而言,混乱始于junit版本,
显然4.12&gt; 4.4在junit版本中。
因此对于版本&gt; 4.4,您需要使用powermock-module-junit4
而不是powermock-module-junit4-legacy
。
作为附注,后来我发现我必须使用powermock-api-mockito2
而不是powermock-api-mockito
这是进步......
Maven说明: https://github.com/powermock/powermock/wiki/Mockito-Maven