PowerMock笔记-局部变量

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @author chenjianfei
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Employee {
    private String name;
    private Integer age;
}

import vip.fkandy.powermock.common.Employee;

/**
 * @author chenjianfei
 */
public class EmployeeDao {
    /**
     * 查询员工数
     * @return
     */
    public int getEmployeeCount() {
        throw new UnsupportedOperationException();
    }

    /**
     * 添加员工
     * @param employee
     */
    public void addEmployee(Employee employee) {
        throw new UnsupportedOperationException();
    }
}
import vip.fkandy.powermock.common.Employee;

/**
 * @author chenjianfei
 */
public class EmployeeService {

    public int getEmployeeCount() {
        EmployeeDao employeeDao = new EmployeeDao();
        return employeeDao.getEmployeeCount();
    }

    public void addEmployee(Employee employee) {
        EmployeeDao employeeDao = new EmployeeDao();
        employeeDao.addEmployee(employee);
    }
}
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import vip.fkandy.powermock.common.Employee;

/**
 * 局部变量
 */
@RunWith(PowerMockRunner.class)
@PrepareForTest(EmployeeService.class)
public class EmployeeServiceTest {

    /**
     * EmployeeDao要想注入到EmployeeService中,
     * 必须添加@RunWith(PowerMockRunner.class)和@PrepareForTest(EmployeeService.class)
     */
    @Test
    public void getEmployeeCount() {
       try{
           EmployeeDao employeeDao = PowerMockito.mock(EmployeeDao.class);
           //当调用EmployeeDao无参构造器的时候,返回一个虚拟的employeeDao
           PowerMockito.whenNew(EmployeeDao.class).withNoArguments().thenReturn(employeeDao);
           PowerMockito.doReturn(10).when(employeeDao).getEmployeeCount();

           EmployeeService employeeService = new EmployeeService();
           int result = employeeService.getEmployeeCount();
           Assert.assertEquals(10,result);
       }catch (Exception e){
           Assert.fail();
       }
    }
    /**
     * EmployeeDao要想注入到EmployeeService中,
     * 必须添加@RunWith(PowerMockRunner.class)和@PrepareForTest(EmployeeService.class)
     */
    @Test
    public void addEmployee() {
        try{
            EmployeeDao employeeDao = PowerMockito.mock(EmployeeDao.class);
            //当调用EmployeeDao任意参数构造器的时候,返回一个虚拟的employeeDao
            PowerMockito.whenNew(EmployeeDao.class).withAnyArguments().thenReturn(employeeDao);
            Employee employee = new Employee();
            //当虚拟的对象employeeDao调用addEmployee方法的时候什么都不做
            PowerMockito.doNothing().when(employeeDao).addEmployee(employee);

            EmployeeService employeeService = new EmployeeService();
            employeeService.addEmployee(employee);
            //验证employeeDao.addEmployee(employee)执行一次
            Mockito.verify(employeeDao,Mockito.times(1)).addEmployee(employee);
        }catch (Exception e){
            Assert.fail();
        }
    }
}




你可能感兴趣的:(PowerMock)