5

我正在尝试让 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,我应该尝试使用旧包。我做到了,但我仍然遇到同样的例外。有任何想法吗?

4

2 回答 2

6

对我来说,混乱始于junit版本控制,

显然 4.12 > 4.4 在junit版本中。

因此对于 > 4.4 的版本,您需要使用powermock-module-junit4

而不是powermock-module-junit4-legacy.

作为旁注,后来我发现我必须使用powermock-api-mockito2

代替powermock-api-mockito

这就是进步...

Maven 说明: https ://github.com/powermock/powermock/wiki/Mockito-Maven

于 2017-08-03T19:37:16.597 回答
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

从构建路径中删除这两个文件解决了这个问题。

于 2012-03-07T07:45:10.300 回答