欢迎访问昆山宝鼎软件有限公司网站! 设为首页 | 网站地图 | XML | RSS订阅 | 宝鼎邮箱 | 宝鼎售后问题提交 | 后台管理


新闻资讯

MENU

软件开发知识

department.getName()); } @Test public void shouldSuppressTh

点击: 次  来源:宝鼎软件 时间:2017-06-01

原文出处: 磁针石

简介

mock是模仿工具,用于模仿真实工具的行为。

Powermock主要用于打桩。好比:要领A的参数需要传入实例B,要领A需要挪用B的某个要领B.C()。要领C因为耗时长可能基础没有实现可能其他不利便在单位测试中实现等原因,需要伪造返回,此时Powermock即可派上用场。

PowerMock扩展了EasyMock和Mockito框架,增加了对static和final要领mock支持等成果。这里主要基于PowerMock Mockito API举办先容。

PowerMock支持JUnit和TestNG,这里基于JUnit。

先容

英文原版书籍下载:https://bitbucket.org/xurongzhong/python-chinese-library/downloadss。

佳构文章推荐:

python 2.7 中文教程及自动化测试先容

利用Python进修selenium测试东西

机能测试艺术
Java单位测试之模仿利器-利用PowerMock举办Mock测试

安装

下载地点:https://github.com/jayway/powermock/wiki/Downloads。下载” Mockito and JUnit including dependencies”版本。当前版本为”powermock-mockito-junit-1.6.3.zip“。

IntelliJ IDEA的配置如下:

右击工程,选择“Open Module Settings”

 department.getName()); } @Test public void shouldSuppressTheInitializerForBaseEntity() { Pow 劳务调派打点系统 erMockito.suppress(PowerMockito.constructor(BaseEntity.class)); assertNotNull(new Department(18)); }} 留意测试代码并未试验乐成

按下“ALT + Insert”,选择“Jars or directories…”, 插入jar包:

 department.getName()); } @Test public void shouldSuppressTheInitializerForBaseEntity() { Pow 劳务调派打点系统 erMockito.suppress(PowerMockito.constructor(BaseEntity.class)); assertNotNull(new Department(18)); }} 留意测试代码并未试验乐成

点击OK。

在”Module Settings”对话框中点击“Sources”标签,右击右边底部面板,选择“New Folder…”, 定名为test。

在”Module Settings”对话框中选择test,软件开发,标识为Test Sources,封锁”Module Settings”对话框。

Eclipse中只需要上述jar包放在工程下的lib目次即可。

Maven在pom.xml添加如下内容:

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-module-junit4</artifactId>
        <version>1.6.3</version>
        <scope>test</scope>
    </dependency>    
    <dependency>    
        <groupId>org.powermock</groupId>    
        <artifactId>powermock-api-mockito</artifactId>    
        <version>1.6.3</version>    
        <scope>test</scope>    
    </dependency>        
  </dependencies>

快速入门

下面建设EmployeeController类用于给Employee类执行Create, Read, Update, and Delete (CRUD)。实际事情由EmployeeService完成。getProjectedEmployeeCount要领估量公司员工每年增加20%,并返回近似取整。

public class EmployeeController {

    private EmployeeService employeeService;

    public EmployeeController(EmployeeService employeeService) {

        this.employeeService = employeeService;
    }

    public int getProjectedEmployeeCount() {

        final int actualEmployeeCount = employeeService.getEmployeeCount();
        return (int) Math.ceil(actualEmployeeCount * 1.2);
    }

    public void saveEmployee(Employee employee) {

        employeeService.saveEmployee(employee);
    }    
}
public class EmployeeService {

    public int getEmployeeCount() {
        throw new UnsupportedOperationException();
    }

    public void saveEmployee(Employee employee) {
        throw new UnsupportedOperationException();
    }    
}

由于getEmployeeCount等要领没有真正实现,我们需要mock:

public class Employee {

}
import static org.junit.Assert.*;

import org.junit.Test;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;

public class EmployeeControllerTest {

    @Test
    public void shouldReturnProjectedCountOfEmployeesFromTheService() {

        EmployeeService mock = PowerMockito.mock(EmployeeService.class);
        PowerMockito.when(mock.getEmployeeCount()).thenReturn(8);
        EmployeeController employeeController = new EmployeeController(mock);
        assertEquals(10, employeeController.getProjectedEmployeeCount());
    }

    @Test
    public void
    shouldInvokeSaveEmployeeOnTheServiceWhileSavingTheEmployee() {

        EmployeeService mock = PowerMockito.mock(EmployeeService.class);    
        EmployeeController employeeController = new EmployeeController(mock);
        Employee employee = new Employee();
        employeeController.saveEmployee(employee);
        Mockito.verify(mock).saveEmployee(employee);
    }        
}

留意假如上述代码呈现莫名其妙的错误,发起先确认所有文件已经生存,再不可重启Eclipse。