如果在测一个类的某一个方法时,这个方法还调用了此类的其他方法,那么如何指定其他方法的返回值呢?

public class Calc {

  1. static {
  2. System.out.println("hahaha");
  3. }
  4. public int add(int a, int b) {
  5. return interADD(a, b);
  6. }
  7. private int interADD(int a, int b) {
  8. return a + b;
  9. }
  10. public int minus(int a, int b) {
  11. return interMinus(a, b);
  12. }
  13. public int interMinus(int a, int b) {
  14. return a-b;
  15. }
  16. }
  17. package com.kevin.util;
  18. import static org.junit.Assert.*;
  19. import junit.framework.Assert;
  20. import org.easymock.EasyMock;
  21. import org.junit.Test;
  22. import org.junit.runner.RunWith;
  23. import org.powermock.api.easymock.PowerMock;
  24. import org.powermock.core.classloader.annotations.PrepareForTest;
  25. import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor;
  26. import org.powermock.modules.junit4.PowerMockRunner;
  27. @RunWith(PowerMockRunner.class)
  28. @PrepareForTest({Calc.class})
  29. //suppress static initial
  30. @SuppressStaticInitializationFor({"Calc.class"})
  31. public class CalcTest {
  32. //mock private method in class that be testing
  33. @Test
  34. public void testAdd() {
  35. Calc createPartialMock = PowerMock.createPartialMock(Calc.class, "interADD");
  36. try {
  37. PowerMock.expectPrivate(createPartialMock, "interADD",2,3).andReturn(1000);
  38. PowerMock.replay(createPartialMock);
  39. int result = createPartialMock.add(2, 3);
  40. Assert.assertEquals(1000, result);
  41. } catch (Exception e) {
  42. // TODO Auto-generated catch block
  43. fail();
  44. }
  45. }
  46. //mock public method int class that be testing
  47. @Test
  48. public void testMinus() {
  49. Calc createPartialMock = PowerMock.createPartialMock(Calc.class, "interMinus");
  50. try {
  51. EasyMock.expect(createPartialMock.interMinus(5, 4)).andReturn(1000);
  52. EasyMock.replay(createPartialMock);
  53. int result = createPartialMock.minus(5, 4);
  54. Assert.assertEquals(1000, result);
  55. } catch (Exception e) {
  56. // TODO Auto-generated catch block
  57. fail();
  58. }
  59. }
  60. }

PowerMock使用遇到的问题——2的更多相关文章

  1. Mock之easymock, powermock, and mockito

    easymock, powermock, and mockito Easymock Class Mocking Limitations To be coherent with interface mo ...

  2. 用PowerMock mock 由工厂方法产生的对象

    有些对象需要mock的对象是由工厂方法产生出来的,而工厂方法一般是静态方法,这时候就需要同时mock工厂方法及对象 被测方法: public class EmployeeServiceFactory ...

  3. 用PowerMock spy mock private方法

    在实际的工作中,经常碰到只需要mock一个类的一部分方法,这时候可以用spy来实现. 被测类: public class EmployeeService { public boolean exist( ...

  4. 用PowerMock mock static方法

    在编写代码时,经常需要调用别人已经写好的工具类,而这些工具提供的方法经常是static方法,在这里,直接贴出<PowerMock实战手册>中的例子 待测试方法: public class ...

  5. 用PowerMock mock 临时变量

    在开发时,经常遇到这种情况,被测试方法所调用的类不是通过构造注入的,而是通过临时new出来的,如下面待测方法: public class EmployeeService { private Emplo ...

  6. PowerMock遇到的问题——5

    在做单元测试时,有时在一个方法中会调用这个类的其他私有方法,那么如何指定这些方法的返回值呢? 解决方法:用 createPartialMock 具体用法如下: TestClass test=Power ...

  7. 用maven搭建 testNG+PowerMock+Mockito测试框架

    单元测试是开发中必不可少的一部分,是产品代码的重要保证. Junit和testNG是当前最流行的测试框架,Junit是使用最广泛的测试框架,有兴趣的话自己baidu一下. testNG基于Junit和 ...

  8. PowerMock 简介--转载

    原文地址:https://www.ibm.com/developerworks/cn/java/j-lo-powermock/ EasyMock 以及 Mockito 都因为可以极大地简化单元测试的书 ...

  9. PowerMock遇到的问题——4

    当我们在测试一个方法的构造方法的时候,有的时候内部需要new一些对象,这是就需要用到PowerMock.exceptNew(),这个方法,但有时候传的参数有关键字this,比如SAPPublisher ...

  10. PowerMock 遇到的问题——2

    如果我们要测试的类继承另一个类,而且在构造方法中有super():那么怎么Mock那个super语句呢? 在PowerMock中有一个suppressConstructor方法,具体写法如下: Pow ...

随机推荐

  1. html5实现GIF动画!

     代码如下: <!DOCTYPE html><html>    <head>        <meta charset="utf-8"&g ...

  2. 关于javascript的误区

    JavaScript 误区 接触JavaScript两年多遇到过各种错误,其中有一些让人防不胜防,原来对JavaScript的误会如此之深,仅以此文总结一下常见的各种想当然的误区 String rep ...

  3. flexbox弹性伸缩布局

    <!doctype html><html lang="en"><head> <meta charset="UTF-8" ...

  4. arr.sort()排序方法

    <!DOCTYPE html><html lang="zh-CN"><head> <meta charset="UTF-8&qu ...

  5. java中在linux下利用jstack检测死锁

    首先,编写一个死锁程序 package deadlock; public class testJstack { final static Object resource_1 = new Object( ...

  6. php 新特性

    PHP 5.6 1.可以使用表达式定义常量 https://php.net/manual/zh/migration56.new-features.php 在之前的 PHP 版本中,必须使用静态值来定义 ...

  7. PS去掉图片上的文字的6种基本方法,动态教程

    1.使用仿制图章工具去除文字这是比较常用的方法.具体的操作是,选取仿制图章工具,按住Alt键,在无文字区域点击相似的色彩或图案采样,然后在文字区域拖动鼠标复制以复盖文字.要注意的是,采样点即为复制的起 ...

  8. semantic-ui使用gulp执行build-css报错

    1.执行gulp build-css报错 [09:40:49] Starting 'build-css'... Building CSS Potentially unhandled rejection ...

  9. python 练习 3

    #!/usr/bin/python # -*- coding: utf-8 -*- def z94(): #斐波那契数列 def filie(x): a,b,t=1,1,0 if x==1 or x= ...

  10. web开发必须知道的javascripat工具

    1,JavaScript compressor and comparison tool 有许多工具可以帮助你压缩JavaScript代码,但是这个过程比较耗时,并且,对于某个特定的场景来说,很难分析出 ...