easymock使用方法_EasyMock静态方法– PowerMock,JUnit 4,TestNG

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

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

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

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

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

easymock使用方法

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值