PowerMock使用遇到的问题——2
如果在测一个类的某一个方法时,这个方法还调用了此类的其他方法,那么如何指定其他方法的返回值呢?
public class Calc {
- static {
- System.out.println("hahaha");
- }
- public int add(int a, int b) {
- return interADD(a, b);
- }
- private int interADD(int a, int b) {
- return a + b;
- }
- public int minus(int a, int b) {
- return interMinus(a, b);
- }
- public int interMinus(int a, int b) {
- return a-b;
- }
- }
- package com.kevin.util;
- import static org.junit.Assert.*;
- import junit.framework.Assert;
- import org.easymock.EasyMock;
- import org.junit.Test;
- import org.junit.runner.RunWith;
- import org.powermock.api.easymock.PowerMock;
- import org.powermock.core.classloader.annotations.PrepareForTest;
- import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor;
- import org.powermock.modules.junit4.PowerMockRunner;
- @RunWith(PowerMockRunner.class)
- @PrepareForTest({Calc.class})
- //suppress static initial
- @SuppressStaticInitializationFor({"Calc.class"})
- public class CalcTest {
- //mock private method in class that be testing
- @Test
- public void testAdd() {
- Calc createPartialMock = PowerMock.createPartialMock(Calc.class, "interADD");
- try {
- PowerMock.expectPrivate(createPartialMock, "interADD",2,3).andReturn(1000);
- PowerMock.replay(createPartialMock);
- int result = createPartialMock.add(2, 3);
- Assert.assertEquals(1000, result);
- } catch (Exception e) {
- // TODO Auto-generated catch block
- fail();
- }
- }
- //mock public method int class that be testing
- @Test
- public void testMinus() {
- Calc createPartialMock = PowerMock.createPartialMock(Calc.class, "interMinus");
- try {
- EasyMock.expect(createPartialMock.interMinus(5, 4)).andReturn(1000);
- EasyMock.replay(createPartialMock);
- int result = createPartialMock.minus(5, 4);
- Assert.assertEquals(1000, result);
- } catch (Exception e) {
- // TODO Auto-generated catch block
- fail();
- }
- }
- }
PowerMock使用遇到的问题——2的更多相关文章
- Mock之easymock, powermock, and mockito
easymock, powermock, and mockito Easymock Class Mocking Limitations To be coherent with interface mo ...
- 用PowerMock mock 由工厂方法产生的对象
有些对象需要mock的对象是由工厂方法产生出来的,而工厂方法一般是静态方法,这时候就需要同时mock工厂方法及对象 被测方法: public class EmployeeServiceFactory ...
- 用PowerMock spy mock private方法
在实际的工作中,经常碰到只需要mock一个类的一部分方法,这时候可以用spy来实现. 被测类: public class EmployeeService { public boolean exist( ...
- 用PowerMock mock static方法
在编写代码时,经常需要调用别人已经写好的工具类,而这些工具提供的方法经常是static方法,在这里,直接贴出<PowerMock实战手册>中的例子 待测试方法: public class ...
- 用PowerMock mock 临时变量
在开发时,经常遇到这种情况,被测试方法所调用的类不是通过构造注入的,而是通过临时new出来的,如下面待测方法: public class EmployeeService { private Emplo ...
- PowerMock遇到的问题——5
在做单元测试时,有时在一个方法中会调用这个类的其他私有方法,那么如何指定这些方法的返回值呢? 解决方法:用 createPartialMock 具体用法如下: TestClass test=Power ...
- 用maven搭建 testNG+PowerMock+Mockito测试框架
单元测试是开发中必不可少的一部分,是产品代码的重要保证. Junit和testNG是当前最流行的测试框架,Junit是使用最广泛的测试框架,有兴趣的话自己baidu一下. testNG基于Junit和 ...
- PowerMock 简介--转载
原文地址:https://www.ibm.com/developerworks/cn/java/j-lo-powermock/ EasyMock 以及 Mockito 都因为可以极大地简化单元测试的书 ...
- PowerMock遇到的问题——4
当我们在测试一个方法的构造方法的时候,有的时候内部需要new一些对象,这是就需要用到PowerMock.exceptNew(),这个方法,但有时候传的参数有关键字this,比如SAPPublisher ...
- PowerMock 遇到的问题——2
如果我们要测试的类继承另一个类,而且在构造方法中有super():那么怎么Mock那个super语句呢? 在PowerMock中有一个suppressConstructor方法,具体写法如下: Pow ...
随机推荐
- html5实现GIF动画!
代码如下: <!DOCTYPE html><html> <head> <meta charset="utf-8"&g ...
- 关于javascript的误区
JavaScript 误区 接触JavaScript两年多遇到过各种错误,其中有一些让人防不胜防,原来对JavaScript的误会如此之深,仅以此文总结一下常见的各种想当然的误区 String rep ...
- flexbox弹性伸缩布局
<!doctype html><html lang="en"><head> <meta charset="UTF-8" ...
- arr.sort()排序方法
<!DOCTYPE html><html lang="zh-CN"><head> <meta charset="UTF-8&qu ...
- java中在linux下利用jstack检测死锁
首先,编写一个死锁程序 package deadlock; public class testJstack { final static Object resource_1 = new Object( ...
- php 新特性
PHP 5.6 1.可以使用表达式定义常量 https://php.net/manual/zh/migration56.new-features.php 在之前的 PHP 版本中,必须使用静态值来定义 ...
- PS去掉图片上的文字的6种基本方法,动态教程
1.使用仿制图章工具去除文字这是比较常用的方法.具体的操作是,选取仿制图章工具,按住Alt键,在无文字区域点击相似的色彩或图案采样,然后在文字区域拖动鼠标复制以复盖文字.要注意的是,采样点即为复制的起 ...
- semantic-ui使用gulp执行build-css报错
1.执行gulp build-css报错 [09:40:49] Starting 'build-css'... Building CSS Potentially unhandled rejection ...
- python 练习 3
#!/usr/bin/python # -*- coding: utf-8 -*- def z94(): #斐波那契数列 def filie(x): a,b,t=1,1,0 if x==1 or x= ...
- web开发必须知道的javascripat工具
1,JavaScript compressor and comparison tool 有许多工具可以帮助你压缩JavaScript代码,但是这个过程比较耗时,并且,对于某个特定的场景来说,很难分析出 ...