Powermock sdk的安装

Powermock是对mockito的封装和扩展,从sdk的角度,powermock需要依赖mockito的sdk.

下载sdk

powermock的下载地址,

zip包中包含了需要的依赖


Powermock sdk的安装_第1张图片
dowload zip

除了上面的zip包,还有一个powermock的扩展包
,OK,把以上所有的jar包导入。

    testCompile files('libs/byte-buddy-1.2.1.jar')
    testCompile files('libs/cglib-nodep-2.2.2.jar')
    testCompile files('libs/hamcrest-core-1.3.jar')
    testCompile files('libs/javassist-3.21.0-GA.jar')
    testCompile files('libs/junit-4.12.jar')
    testCompile files('libs/mockito-core-2.0.42-beta.jar')
    testCompile files('libs/objenesis-2.4.jar')
    testCompile files('libs/powermock-mockito2-1.6.6-full.jar')
    testCompile files('libs/powermock-api-mockito-common-1.6.6.jar')

测试sdk的安装

源码中加入下面的三个文件

package rommel.mockitodemo.powermock;


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);
    }
}

package rommel.mockitodemo.powermock;

/**
 * Created by yuan on 2017/6/1.
 */

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

    public void saveEmployee(Employee employee) {
        throw new UnsupportedOperationException();
    }
}
package rommel.mockitodemo.powermock;


public class Employee {
}

测试文件中加入

package rommel.mockitodemo.powermock;

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

import static org.junit.Assert.assertEquals;


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);
    }

}


运行结果显示sdk安装成功。

Powermock sdk的安装_第2张图片
运行结果

参考

https://my.oschina.net/u/1433482/blog/645155

你可能感兴趣的:(Powermock sdk的安装)