博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
easymock使用方法_EasyMock静态方法– PowerMock,JUnit 4,TestNG
阅读量:2536 次
发布时间:2019-05-11

本文共 4795 字,大约阅读时间需要 15 分钟。

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-junit4

    powermock模块junit4
  • powermock-module-testng

    powermock模块测试
  • powermock-api-easymock

    powermock-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还不支持它。 我的示例使用以下版本。

4.12
6.14.3
2.0.0-beta.5
10
3.6

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 {	@Test	public 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{	@Test	public 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模拟框架的重要扩展。 通过扩展测试用例来模拟静态方法,它也对我们有帮助。

. 检出完整的项目和更多EasyMock示例。

翻译自:

easymock使用方法

转载地址:http://ayqzd.baihongyu.com/

你可能感兴趣的文章
[PYTHON]一个简单的单元測试框架
查看>>
iOS开发网络篇—XML数据的解析
查看>>
[BZOJ4303]数列
查看>>
一般处理程序在VS2012中打开问题
查看>>
C语言中的++和--
查看>>
thinkphp3.2.3入口文件详解
查看>>
POJ 1141 Brackets Sequence
查看>>
Ubuntu 18.04 root 使用ssh密钥远程登陆
查看>>
Servlet和JSP的异同。
查看>>
虚拟机centOs Linux与Windows之间的文件传输
查看>>
ethereum(以太坊)(二)--合约中属性和行为的访问权限
查看>>
IOS内存管理
查看>>
middle
查看>>
[Bzoj1009][HNOI2008]GT考试(动态规划)
查看>>
Blob(二进制)、byte[]、long、date之间的类型转换
查看>>
OO第一次总结博客
查看>>
day7
查看>>
iphone移动端踩坑
查看>>
vs无法加载项目
查看>>
Beanutils基本用法
查看>>