当前位置: 首页 > 工具软件 > easyMock > 使用案例 >

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

唐向荣
2023-12-01

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使用方法

 类似资料: