实例:
package com.bijian.study; import static org.junit.Assert.assertEquals; import java.io.IOException; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; class MyStringUtil { public static String uppercase(String s) { return s.toUpperCase(); } } //1.标注使用PowerRunner运行test(powermock会修改字节码) @RunWith(PowerMockRunner.class) //2.设置mock类(支持多个类,逗号分隔),这个可以设置到class上,也可以设置到method上 //这里面包含两种类型: //a.被mock的类(如上例MyStringUtil .class,如果mock类为系统类,如System.class,则不需要这里设置就可以使用) //b.context类,如果是在XxxServer里面希望mock MyStringUtil类,则要设置 XxxServer.class @PrepareForTest({ MyStringUtil.class }) public class HelloTest { @Before public void before() { //3.告诉powermock需要mock哪个类 PowerMockito.mockStatic(MyStringUtil.class); } @Test public void test() throws IOException { //4.打桩,设置mock对象返回预期值 PowerMockito.when(MyStringUtil.uppercase("abc")).thenReturn("ABC"); //5.执行mock方法,返回预期值 assertEquals("ABC", MyStringUtil.uppercase("abc")); } }
注意:如上代码引入javassist-3.4.GA.jar、junit-4.9.0.jar、mockito-all-1.9.0.jar、powermock-mockito-1.4.12-full.jar能正常运行。
附:如使用Powermock和mockito测试静态方法时,如运行单元测试报”NoClassDefFoundError: javassist/util/proxy/MethodFilter“错误,详细如下所示:
java.lang.NoClassDefFoundError: javassist/expr/ExprEditor at org.powermock.tests.utils.impl.AbstractTestSuiteChunkerImpl.createNewClassloader(AbstractTestSuiteChunkerImpl.java:196) at org.powermock.tests.utils.impl.AbstractTestSuiteChunkerImpl.chunkClass(AbstractTestSuiteChunkerImpl.java:170) at org.powermock.tests.utils.impl.AbstractTestSuiteChunkerImpl.<init>(AbstractTestSuiteChunkerImpl.java:92) at org.powermock.tests.utils.impl.AbstractTestSuiteChunkerImpl.<init>(AbstractTestSuiteChunkerImpl.java:85) at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.<init>(JUnit4TestSuiteChunkerImpl.java:47) 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.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) Caused by: java.lang.ClassNotFoundException: javassist.expr.ExprEditor at java.net.URLClassLoader$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) ... 25 more
解决方法:引入javassist-3.4.GA.jar