有时候,需要测试的方法内有collections结构,就需要同时mock多个对象
被测方法:
public class EmployeeService { public List<Integer> getTotalLIst(){ List<Integer> list = new ArrayList<Integer>(); for (int i=0;i<10;i++){ list.add(employeeDao.getTotal()); } return list; } }
测试类:
@Test public void getTotalLIst(){ PowerMockito.when(employeeDao.getTotal()).thenReturn(1,2,3,4,5,6,7,8,9,10); List<Integer> list = employeeService.getTotalLIst(); List<Integer> listnew = new ArrayList<Integer>(); for(int i=0;i<10;i++){ listnew.add(i+1); } Assert.assertEquals(listnew, list); }
时间: 2024-10-12 08:52:16