powermockito教程_Mock Testing using PowerMock (With JUnit and Mockito)

本教程介绍了如何使用PowerMockito进行模拟测试。PowerMockito扩展了EasyMock和Mockito,允许模拟静态方法、构造函数、删除权限等。文中通过一个简单的例子展示了如何设置PowerMock,创建模拟对象,验证方法调用,并使用模拟设置获取更多测试运行信息。
摘要由CSDN通过智能技术生成

PowerMock是Java世界的开源模拟库。 它扩展了现有的

PowerMock currently extends the EasyMock and Mockito mocking frameworks. Depending on which extension is preferred, the syntax to write any unit test differs slightly. In this tutorial, I am using PowerMock with Mockito.

本教程将演示一个使用PowerMock的非常简单的模拟示例。 它将向我们展示创建模拟和验证方法调用的基本语法。

PowerMock Installation With Dependencies

PowerMock是一个正在积极开发的开源模拟框架。 您可以按照以下步骤在计算机中进行设置。

1)首先下载PowerMock 1.5。 访问http://code.google.com/p/powermock/ PowerMock主页。

2)点击页面上的下载选项卡,您应该看到如下内容:

3)由于我们将使用Mockito扩展和JUnit测试框架来开发所有示例,因此请下载powermock-mockito-junit-1.6.zip文件。

4)将ZIP文件解压缩到某个文件夹中。 该ZIP文件包含使用PowerMock编写单元测试所需的所有从属JAR文件。

5)将所有jar文件复制到项目的lib文件夹中,并将其添加为依赖项。

You are done !!

You may need to additionally include “hamcrest-core-1.3.jar” if you do not have already.

Participant Classes

为了完整起见,让我首先在下面的类中写下我们将在测试示例中使用的类。

Employee.javapublic class Employee {

private String fName;

public String getfName() {

return fName;

}

public void setfName(String fName) {

this.fName = fName;

}

}

EmployeeService.javapublic class EmployeeService

{

public int getEmployeeCount() {

throw new UnsupportedOperationException();

}

public void saveEmployee(Employee employee) {

//return nothing

}

}

EmployeeController.javapublic class EmployeeController

{

private EmployeeService employeeService;

public EmployeeController(EmployeeService employeeService) {

this.employeeService = employeeService;

}

public int getProjectedEmployeeCount()

{

final int actualEmployeeCount = employeeService.getEmployeeCount();

return actualEmployeeCount * 2;

}

public void saveEmployee(Employee employee) {

employeeService.saveEmployee(employee);

}

}

First Test without PowerMock

让我们编写一个简单的测试来获得不使用模拟的员工人数。 如您所见, EmployeeService.getEmployeeCount()方法抛出UnsupportedOperationException ,测试应该失败。@Test

public void shouldGetCountOfEmployees()

{

EmployeeController employeeController =new EmployeeController(new EmployeeService());

Assert.assertEquals(10,employeeController.getProjectedEmployeeCount());

}

在运行测试时,它肯定会失败,并带有以下异常。java.lang.UnsupportedOperationException

at com.howtodoinjava.powermock.examples.service.EmployeeService.getEmployeeCount(EmployeeService.java:8)

at com.howtodoinjava.powermock.examples.controller.EmployeeController.getProjectedEmployeeCount(EmployeeController.java:16)

at com.howtodoinjava.powermock.examples.test.EmployeeControllerTestOne.shouldGetCountOfEmployees(EmployeeControllerTestOne.java:15)

在不允许您使用某种方法的任何应用程序中都可能出现这种情况,并且原因可以是无限的。 在这种情况下,您可能希望模拟上述方法,以便可以测试应用程序的其他部分。

Mock a simple method using PowerMock

在上面的示例中,不支持getEmployeeCount()方法,但我们想使用它。 在这种情况下,我们可以使用powermock模拟它。@Test

public void firstMockTest()

{

//Creating a mock using the PowerMockito.mock

//method for the EmployeeService class.

EmployeeService mock =PowerMockito.mock(EmployeeService.class);

//Next statement essentially says that when getProjectedEmployeeCount method

//is called on the mocked EmployeeService instance, return 8.

PowerMockito.when(mock.getEmployeeCount()).thenReturn(8);

EmployeeController employeeController = new EmployeeController(mock);

Assert.assertEquals(16, employeeController.getProjectedEmployeeCount());

}

以上测试将成功执行。 在这里,当调用employeeController.getProjectedEmployeeCount()时,它依次从模拟对象中调用方法getEmployeeCount() ,该对象返回值8。控​​制器将其乘以2,返回值为16。该返回值等于assert中的期望值。语句,因此测试通过了。

Verify if mock method was really invoked?

有时,单元测试只需要调用一个方法而忘记它。 主要是因为method不返回任何值。 您肯定可以通过再次从数据源获取值来测试DB中是否存在值。 但是,如果您的测试仅需要验证是否已调用某个方法,就可以使用powermock进行操作。@Test

public void verifyMethodInvokationTest()

{

EmployeeService mock =PowerMockito.mock(EmployeeService.class);

EmployeeController employeeController = new EmployeeController(mock);

Employee employee = new Employee();

employeeController.saveEmployee(employee);

//Verifying that controller did call the

//saveEmployee() method on the mocked service instance.

Mockito.verify(mock).saveEmployee(employee);

}

在上面的测试示例中,我们使用verify(mock)方法来验证saveEmployee(employee)是否确实已被调用。 如果您通过测试,则将通过。

为了验证上面的代码是否工作正常,请在EmployeeController.java下面一行中注释掉。public void saveEmployee(Employee employee) {

//employeeService.saveEmployee(employee); //Comment this line

}

现在,如果您再次运行测试,它将失败并显示此错误。Wanted but not invoked:

employeeService.saveEmployee(

com.howtodoinjava.powermock.examples.model.Employee@7808b9

);

-> at com.howtodoinjava.powermock.examples.test.EmployeeControllerTestOne.verifyMethodInvokationTest(EmployeeControllerTestOne.java:47)

Actually, there were zero interactions with this mock.

您可以在单元测试中拥有非常好的功能。

Using mock settings for extra information about test run

这些模拟设置很少使用,但在某些情况下很有用。 如果您想为模拟命名,以供将来调试之用,请使用它们。 或者您想启用详细日志记录以获取更多信息。 当您要注册一个侦听器来通知该模拟方法的调用时,可以使用它。 甚至在尚未实现实际对象的模拟对象上实现一些额外的接口。@Test

public void mockSettingsTest() {

EmployeeService mock =PowerMockito.mock(EmployeeService.class, Mockito

.withSettings()

.name("EmployeeServiceMock")

.verboseLogging());

EmployeeController employeeController = new EmployeeController(mock);

Employee employee = new Employee();

employeeController.saveEmployee(employee);

//Verifying that controller did call the

//saveEmployee method on the mocked service

//instance.

Mockito.verify(mock).saveEmployee(employee);

}

运行以上测试以在控制台中获得以下结果:############ Logging method invocation #1 on mock/spy ########

employeeService.saveEmployee(

com.howtodoinjava.powermock.examples.model.Employee@c9131c

);

invoked: -> at com.howtodoinjava.powermock.examples.controller.EmployeeController.saveEmployee(EmployeeController.java:21)

has returned: "null"

############ Logging method invocation #2 on mock/spy ########

employeeService.saveEmployee(

com.howtodoinjava.powermock.examples.model.Employee@c9131c

);

invoked: -> at com.howtodoinjava.powermock.examples.test.EmployeeControllerTestOne.mockSettingsTest(EmployeeControllerTestOne.java:64)

has returned: "null"

这就是关于powermock的初学者教程的全部内容,可帮助您入门。 我将在下一组教程中介绍一些复杂的主题。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值