easymock使用方法

One of the limitations of EasyMock is that it can’t mock static methods. However, we can use PowerMock EasyMock extension to mock static methods.

EasyMock的局限性之一是它不能模拟静态方法。 但是,我们可以使用PowerMock EasyMock扩展来模拟静态方法。

EasyMock静态方法– PowerMock (EasyMock Static Method – PowerMock)

PowerMock is divided into multiple modules to support JUnit and TestNG testing framework. Similarly, there are modules to extend EasyMock and Mockito mocking frameworks.

PowerMock分为多个模块以支持JUnit和TestNG测试框架。 同样,有一些模块可以扩展EasyMock和Mockito模拟框架。

I will provide an example to mock static method using PowerMock on both JUnit 4 and TestNG frameworks. So we need to import the following artifacts.

我将提供一个在JUnit 4和TestNG框架上使用PowerMock模拟静态方法的示例。 因此,我们需要导入以下工件。

  • powermock-module-junit4powermock模块junit4
  • powermock-module-testngpowermock模块测试
  • powermock-api-easymockpowermock-api-easymock
  • junit, testng and easymock for obvious reasons.junit,testng和easymock的原因很明显。

I am not using JUnit 5 because PowerMock doesn’t support it yet. I am using the following versions for my examples.

我没有使用JUnit 5,因为PowerMock还不支持它。 我的示例使用以下版本。

<junit4.version>4.12</junit4.version>
<testng.version>6.14.3</testng.version>
<powermock.version>2.0.0-beta.5</powermock.version>
<java.version>10</java.version>
<easymock.version>3.6</easymock.version>

JUnit PowerMock EasyMock静态方法示例 (JUnit PowerMock EasyMock Static Method Example)

  • First step is to annotate test class with @RunWith(PowerMockRunner.class) annotation.第一步是使用@RunWith(PowerMockRunner.class)批注对测试类进行批注。
  • Next step is to specify the classes to prepare for testing using PowerMock, for example @PrepareForTest(Utils.class). This has to be done at the class level and we can use its fullyQualifiedNames to specify multiple classes and packages.下一步是指定准备使用PowerMock进行测试的类,例如@PrepareForTest(Utils.class) 。 这必须在类级别完成,我们可以使用其fullyQualifiedNames指定多个类和包。
  • In the test method, use PowerMock.mockStatic() method to mock the static methods of the class.在测试方法中,使用PowerMock.mockStatic()方法模拟该类的静态方法。
  • Stub the behaviors using EasyMock.expect() method.使用EasyMock.expect()方法对行为进行存根。
  • Since we don’t have a mock object, use PowerMock.replayAll() to finalize the setup.由于没有模拟对象,请使用PowerMock.replayAll()完成设置。
  • Use asserts to test the behaviors.使用断言测试行为。
  • Use PowerMock.verifyAll() to verify that all the stubbed methods were called.使用PowerMock.verifyAll()验证是否已调用所有存根方法。

Let’s say we have a utility class as:

假设我们有一个实用程序类:

class Utils {public static long generateID() {return System.currentTimeMillis();}
}

Here is the JUnit test to mock the static method and test it.

这是模拟静态方法并对其进行测试的JUnit测试。

package com.journaldev.easymock.powermock.staticmethod;import static org.easymock.EasyMock.*;
import static org.junit.Assert.assertEquals;import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import static org.powermock.api.easymock.PowerMock.*;@RunWith(PowerMockRunner.class)
@PrepareForTest(Utils.class)
public class JUnit4PowerMockEasyMockStaticExample {@Testpublic void test_static_method() {//PowerMock.mockStatic()mockStatic(Utils.class);expect(Utils.generateID()).andReturn(1000L);//PowerMock.replayAll()replayAll();assertEquals(1000L, Utils.generateID());//PowerMock.verifyAll()verifyAll();}
}

TestNG PowerMock EasyMock静态方法示例 (TestNG PowerMock EasyMock Static Method Example)

If you want to use TestNG instead of JUnit-4, then make sure your test class extends PowerMockTestCase class. Also remove the @RunWith annotation. Make necessary changes to other annotations and assert methods.

如果要使用TestNG而不是JUnit-4,请确保您的测试类扩展了PowerMockTestCase类。 同时删除@RunWith批注。 对其他注释和断言方法进行必要的更改。

Below class uses TestNG along with PowerMock to mock static methods using EasyMock.

下面的类使用TestNG和PowerMock来使用EasyMock模拟静态方法。

package com.journaldev.easymock.powermock.staticmethod;import static org.easymock.EasyMock.*;import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.testng.PowerMockTestCase;
import org.testng.annotations.Test;import static org.powermock.api.easymock.PowerMock.*;
import static org.testng.Assert.assertEquals;@PrepareForTest(Utils1.class)
public class TestNGPowerMockEasyMockStaticExample extends PowerMockTestCase{@Testpublic void test_static_method() {//PowerMock.mockStatic()mockStatic(Utils1.class);expect(Utils1.generateID()).andReturn(1000L);//PowerMock.replayAll()replayAll();assertEquals(1000L, Utils1.generateID());//PowerMock.verifyAll()verifyAll();}
}
class Utils1 {public static long generateID() {return System.currentTimeMillis();}
}

摘要 (Summary)

PowerMock is a great extension to EasyMock and Mockito mocking frameworks. It helps us by extending our test cases to mock static methods too.

PowerMock是EasyMock和Mockito模拟框架的重要扩展。 通过扩展测试用例来模拟静态方法,它也对我们有帮助。

GitHub Repository.GitHub存储库中检出完整的项目和更多EasyMock示例。

翻译自: https://www.journaldev.com/22305/easymock-static-method-powermock-junit-4-testng

easymock使用方法

easymock使用方法_EasyMock静态方法– PowerMock,JUnit 4,TestNG相关推荐

  1. easymock使用方法_EasyMock最终方法– PowerMock,JUnit 4,TestNG

    easymock使用方法 One of the limitations of EasyMock is that it can't mock final methods and final classe ...

  2. easymock使用方法_EasyMock无效方法– ExpectLastCall()

    easymock使用方法 Sometimes we want to mock void methods. EasyMock expect() method can't be used to mock ...

  3. powermock私有字段_使用PowerMock的EasyMock私有方法模拟

    powermock私有字段 Sometimes we want to test a method that is using a private method. We can create the m ...

  4. EasyMock 使用方法与原理剖析--转载

    原文地址:http://www.ibm.com/developerworks/cn/opensource/os-cn-easymock/ Mock 方法是单元测试中常见的一种技术,它的主要作用是模拟一 ...

  5. EasyMock 使用方法与原理剖析

    Mock 方法是单元测试中常见的一种技术,它的主要作用是模拟一些在应用中不容易构造或者比较复杂的对象,从而把测试与测试边界以外的对象隔离开. 编写自定义的 Mock 对象需要额外的编码工作,同时也可能 ...

  6. mockito 静态方法_Mockito模拟静态方法– PowerMock

    mockito 静态方法 Mockito allows us to create mock objects. Since static method belongs to the class, the ...

  7. 超详细解读Java接口:模块通信协议以及默认方法和静态方法

    有不少学习Java的同学一直有个疑问,不仅在初学者中很普遍,连许多经验丰富的老手也很难表述清楚,那就是:Java接口到底是什么? 来看看孙鑫老师的讲解,本文干货含量拉满,这可能是距离你深入理解Java ...

  8. 【Groovy】MOP 元对象协议与元编程 ( 方法注入 | 同时注入普通方法、静态方法、构造方法 )

    文章目录 一.同时注入普通方法.静态方法.构造方法 二.完整代码示例 一.同时注入普通方法.静态方法.构造方法 如果要同时为 Groovy 类注入大量方法 , 使用 Category 分类 或 Met ...

  9. Java8新特性Optional、接口中的默认方法与静态方法

    Optional Optional 类(java.util.Optional) 是一个容器类,代表一个值存在或不存在,原来用 null 表示一个值不存在,现在 Optional 可以更好的表达这个概念 ...

最新文章

  1. c if 判断select已经选择的值_Golang语言基础教程:if分支语句
  2. ASP.NET 内置对象
  3. // synopsys_translate_off,parallel_case 和 full_case
  4. JavaScript实现shortestCommonSupersequence最短公共超序列算法(附完整源码)
  5. 3 ie兼容 vue_前端开发:MVVM框架之Vue势必会取代JQuery吗?
  6. Tomcat服务器java.lang.IllegalArgumentException异常
  7. spring data jpa 分页查询
  8. 玩玩短视频平台和网课平台开发1——腾讯云对象储存COS的初步配置
  9. anaconda方法安装python教程_anaconda的安装教程和使用方法
  10. 1.9 使用PuTTY远程连接Linux 1.10 使用xshell连接Linux 1.11 PuTTY密钥认证 1.12 xshell密钥认证...
  11. 案例:监听域对象的属性变更
  12. 远程调试运行在Resin上面的Web应用程序
  13. python安装方法_听说你安装Python包很慢,试试这个方法
  14. 一文了解数据库索引:哈希、B-Tree 与 LSM
  15. vue 固定div 滚动_Vue - 让水平滚动条(scroll bar)固定在浏览器的底部
  16. Unity播放序列帧
  17. TextCNN文本分类实践
  18. Mac 操作系统版本简史,让我们回到过去
  19. C语言获取执行程序所在的目录路径
  20. MatLab SimuLink国产代替

热门文章

  1. .net中多控件共享事件处理程序的方法
  2. [转载] python中的eval函数
  3. Python 扩展知识:编程习惯
  4. python socket 连续send,出现粘包问题
  5. maven项目在eclipse中debug
  6. android 巧用资源文件(不断积累)
  7. tolua++ 使用有感
  8. 2010年年终“飞”的总结
  9. 【C++笔记】运算符重载
  10. vue 释放内存_13 道由浅入深的 Vue 自测题