Mockito:使用 List<String> 作为参数

目前我使用 Mockito 编写了一个 JUnit 测试。String当列表中的一个具有定义的值(例如"00123")时,它应该通过:

Mockito.when(myMock.isContractAvailable("0815", Arrays.asList(new String[] {ANYVALUE, "00123", ANYVALUE})).thenReturn(Boolean.TRUE);

我怎样才能做到这一点?谢谢!


哈士奇WWW
浏览 381回答 1
1回答

噜噜哒

您可以为参数匹配提供自定义匹配器when(myMock.isContractAvailable(eq("0815"), argThat(new ArgumentMatcher<List<String>>() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public boolean matches(List<String> list) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return "00123".equals(list.get(1)); // essentially ```list -> "00123".equals(list.get(1))``` in java 8&nbsp; &nbsp; &nbsp; &nbsp; }})).thenReturn(true);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java