PowerMockito mock 注入列表
Class Test{
@InjectMocks
private Person ps;
@Mock
private List<Address> list;
@Test
public void rs() {
List<Address> addresses = Arrays.asList(new Country(), new City());
PowerMockito.doCallRealMethod().when(list).forEach(Mockito.any(Consumer.class));
PowerMockito.when(list.iterator()).thenReturn(addresses.iterator());
List<String> as = ps.listAddr();
System.out.println(as);
}
}
public class Person {
private List<Address> addresses;
public List listAddr(){
List<String> list = new ArrayList<>();
this.addresses.forEach(x->{
String addr = x.getAddr();
list.add(addr);
});
return list;
}
}
public interface Address {
String getAddr();
}
public class City implements Address{
@Override
public String getAddr() {
return "成都";
}
}
public class Country implements Address{
@Override
public String getAddr() {
return "中国";
}
}