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

easymock使用方法

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

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

EasyMock使用PowerMock的最终方法和类 (EasyMock Final Method and Class using 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分为多个模块以支持JUnitTestNG测试框架。 同样,有一些模块可以扩展EasyMock和Mockito模拟框架。

I will provide an example to mock final method inside a final class 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 Final 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(Data.class). This has to be done at the class level and we can use its fullyQualifiedNames to specify multiple classes and packages.

    下一步是指定准备使用PowerMock进行测试的类,例如@PrepareForTest(Data.class) 。 这必须在类级别完成,我们可以使用其fullyQualifiedNames指定多个类和包。
  • In the test method, use PowerMock.createMock() method to mock the final class.

    在测试方法中,使用PowerMock.createMock()方法模拟最终类。
  • Stub the behaviors using EasyMock.expect() method.

    使用EasyMock.expect()方法对行为进行存根。
  • Use PowerMock.replay() to finalize the mock object and its behavior setup.

    使用PowerMock.replay()完成模拟对象及其行为设置。
  • Use JUnit 4 asserts to test the behaviors.

    使用JUnit 4断言测试行为。
  • Use PowerMock.verify() to verify that all the stubbed final methods were called.

    使用PowerMock.verify()验证是否调用了所有存根的final方法。

Let’s say we have a final class as:

假设我们有一个最终课程:

final class Data {
	final String reverse(String s) {
		return new StringBuffer(s).reverse().toString();
	}
}

Here is the JUnit test to mock the final class and its final method and test it.

这是模拟最终类及其最终方法并对其进行测试的JUnit测试。

package com.journaldev.easymock.powermock.finalmethod;

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.*;
import static org.easymock.EasyMock.expect;
import static org.junit.Assert.assertEquals;
import static org.easymock.EasyMock.anyString;

@RunWith(PowerMockRunner.class)
@PrepareForTest(Data.class)
public class JUnit4PowerMockEasyMockFinalExample {

	@Test
	public void test_final_method_class() {
		//PowerMock.createMock()
		Data mock = createMock(Data.class);
		
		expect(mock.reverse("CAT")).andReturn("TAC");
		expect(mock.reverse(anyString())).andReturn("INVALID");
		
		//PowerMock.replay()
		replay(mock);
		
		assertEquals("TAC", mock.reverse("CAT"));
		assertEquals("INVALID", mock.reverse("Java"));
		
		//PowerMock.verify()
		verify(mock);
	}

}

TestNG PowerMock EasyMock最终方法示例 (TestNG PowerMock EasyMock Final 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 use TestNG assert methods.

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

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

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

package com.journaldev.easymock.powermock.finalmethod;

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;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.anyString;

@PrepareForTest(Data1.class)
public class TestNGPowerMockEasyMockFinalExample extends PowerMockTestCase{

	@Test
	public void test_final_method_class() {
		//PowerMock.createMock()
		Data1 mock = createMock(Data1.class);
		
		expect(mock.reverse("CAT")).andReturn("TAC");
		expect(mock.reverse(anyString())).andReturn("INVALID");
		
		//PowerMock.replay()
		replay(mock);
		
		assertEquals("TAC", mock.reverse("CAT"));
		assertEquals("INVALID", mock.reverse("Java"));
		
		//PowerMock.verify()
		verify(mock);
	}

}

final class Data1 {
	final String reverse(String s) {
		return new StringBuffer(s).reverse().toString();
	}
}

摘要 (Summary)

PowerMock is a great extension to EasyMock and Mockito mocking frameworks. It helps us to extend our test cases to cover final classes and final methods too.

PowerMock是EasyMock和Mockito模拟框架的重要扩展。 它有助于我们将测试用例扩展到最终类和最终方法。

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

翻译自: https://www.journaldev.com/22338/easymock-final-method-powermock-junit-testng

easymock使用方法

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值