使用Powermock和mockito测试静态方法
编程技术  /  houtizong 发布于 3年前   76
实例:
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/ExprEditorat 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.ExprEditorat 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
请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!
技术博客集 - 网站简介:
前后端技术:
后端基于Hyperf2.1框架开发,前端使用Bootstrap可视化布局系统生成
网站主要作用:
1.编程技术分享及讨论交流,内置聊天系统;
2.测试交流框架问题,比如:Hyperf、Laravel、TP、beego;
3.本站数据是基于大数据采集等爬虫技术为基础助力分享知识,如有侵权请发邮件到站长邮箱,站长会尽快处理;
4.站长邮箱:[email protected];
文章归档
文章标签
友情链接