PowerMockito mock 注入列表

PowerMockito mock 注入列表

Class Test{
   @InjectMocks
    private Person ps;

    @Mock
    private List
list; @Test public void rs() { List
addresses = Arrays.asList(new Country(), new City()); PowerMockito.doCallRealMethod().when(list).forEach(Mockito.any(Consumer.class)); PowerMockito.when(list.iterator()).thenReturn(addresses.iterator()); List as = ps.listAddr(); System.out.println(as); } } public class Person { private List
addresses; public List listAddr(){ List 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 "中国"; } }

你可能感兴趣的:(PowerMockito mock 注入列表)