使用Powermock和mockito
来源:互联网 发布:思科acl应用到端口 编辑:程序博客网 时间:2024/04/28 04:48
使用powermock所需要的jar如下:
maven:
<!-- mock单元测试 start --><dependency> <groupId>org.powermock</groupId> <artifactId>powermock-module-junit4</artifactId> <version>${powermock.version}</version> <scope>test</scope> </dependency><dependency><groupId>org.powermock</groupId><artifactId>powermock-api-mockito</artifactId><version>${powermock.version}</version><scope>test</scope></dependency><!-- mock单元测试 end -->
import java.io.File;import org.junit.Assert;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;import com.yihaodian.wap.test.ClassDependency;import com.yihaodian.wap.test.ClassUnderTest;//必须要有@RunWith(PowerMockRunner.class)//需要模拟的类@PrepareForTest({ClassUnderTest.class,File.class})public class TestClassUnderTest { @Test public void testCallArgumentInstance() { showClassLoader("testCallArgumentInstance"); File file = PowerMockito.mock(File.class); ClassUnderTest underTest = new ClassUnderTest(); PowerMockito.when(file.exists()).thenReturn(true); Assert.assertTrue(underTest.callArgumentInstance(file)); } @Test public void testCallInternalInstance() throws Exception { showClassLoader("testCallInternalInstance"); File file = PowerMockito.mock(File.class); ClassUnderTest underTest = new ClassUnderTest(); PowerMockito.whenNew(File.class).withArguments("bbb").thenReturn(file); PowerMockito.when(file.exists()).thenReturn(true); Assert.assertTrue(underTest.callInternalInstance("bbb")); } @Test public void testCallFinalMethod() { showClassLoader("testCallFinalMethod"); ClassDependency depencency = PowerMockito.mock(ClassDependency.class); ClassUnderTest underTest = new ClassUnderTest(); PowerMockito.when(depencency.isAlive()).thenReturn(true); Assert.assertTrue(underTest.callFinalMethod(depencency)); } private void showClassLoader(String methodName) { System.out.println("=============="+methodName+"==============="); System.out.println("TestClassUnderTest: " + TestClassUnderTest.class.getClassLoader()); System.out.println("ClassUnderTest: " + ClassUnderTest.class.getClassLoader()); System.out.println("ClassDependency: " + ClassDependency.class.getClassLoader()); }}
public class ClassDependency {public boolean isAlive(){return false;}}
import java.io.File;public class ClassUnderTest {public boolean callArgumentInstance(File file) { return file.exists();}public boolean callInternalInstance(String path) { File file = new File(path); return file.exists(); } public boolean callFinalMethod(ClassDependency classDependency){return classDependency.isAlive();}}
0 0
- 使用Powermock和mockito
- 使用Powermock和Mockito测试静态方法
- 使用Powermock和mockito测试静态方法
- 使用Powermock和mockito来进行单元测试
- 使用Powermock和mockito来进行单元测试
- 使用powerMock和mockito模拟静态方法和私有方法
- PowerMock与Mockito的使用
- 使用 PowerMock 以及 Mockito 实现单元测试
- powerMock比easyMock和Mockito更强大
- 使用MRUnit,Mockito和PowerMock进行Hadoop MapReduce作业的单元测试
- Mockito 库、powermock扩展
- powermock mockito mock static
- Mockito 库、powermock扩展
- Mockito与PowerMock
- testng + mockito + powerMock
- mockito、powermock版本对应关系
- Mockito介绍和使用
- 单元测试及框架简介 --junit、jmock、mockito、powermock的简单使用
- Ecology表单二次开发-通过JS控制浏览按钮为只读
- Java中值传递及内存分配的理解
- DCCI:“文章事件”等话题引爆微博用户活跃度
- JackJson基本用法
- 那是最美
- 使用Powermock和mockito
- 收包软中断和netif_rx (linux网络子系统学习 第四节 )
- leetcode-Max Points on a Line
- 比特币监管罗生门:一纸《通知》的万般变化
- C# 轻松获取路径中文件名、目录、扩展名等(有用)
- Android中为ViewGroup设置selector无效果
- javascript获取URL后的参数
- perl 发送邮件 Net::SMTP_auth
- 谷歌眼镜下周二发售:仅限一天 售1500美元