有以下类:
public class A {
@Autowired
private Set<IClient> clients;
}
public class B implements IClient { }
public class C implements IClient { }
@RunWith(MockitoJUnitRunner.class)
public class Atest {
@InjectMocks
A a;
@Mock
IClient clients;
}
如何使用模拟来为将包括B
和C
类的接口的Set
?
使用注入现场注入的构造函数注入,然后创建两个模拟,将它们放入一个集合中,然后用该集合调用构造函数。例如:
public class Atest {
private A a;
@Mock
private IClient mockAClient;
@Mock
private IClient mockBClient;
@Before
public void prepare() {
a = new A(new HashSet<>(Arrays.asList(mockAClient, mockBClient));
}
}