在这种情况下,
我正在使用Java Spring Boot,并尝试在我的Untest中为AWS S3存储桶写一个模拟。以下是我执行测试
时引起某些问题的代码 @Mock
AmazonS3 s3client;
when(s3client.getObject(new GetObjectRequest(Mockito.any(String.class),
and(Mockito.any(String.class),Mockito.endsWith(".txt"))
))).thenReturn(RawText);
when(s3client.getObject(new GetObjectRequest(Mockito.any(String.class),
and(Mockito.any(String.class),Mockito.endsWith(".png"))
))).thenReturn(RawImage);
我要做的是,我需要从S3存储桶中读取PNG文件和文本文件。根据内容类型,我试图返回对象。当我执行测试时,我会得到
org.mockito.exceptions.misusing.InvalidUseOfMatchersException:
Invalid use of argument matchers!
1 matchers expected, 2 recorded:
注意RawImage
和RawText
是我创建的S3Object
。您能帮我帮助我,我的代码中出了什么问题?
匹配器应用作getObject
的参数。因此,如果您的实际参数不是模拟,则可能需要实现自定义匹配器:
org.hamcrest.Matcher<GetObjectRequest> objectRequestMatcher =
new BaseMatcher<GetObjectRequest>() {
@Override
public void describeTo(Description arg0) {
}
@Override
public boolean matches(Object arg0) {
return ((GetObjectRequest) arg0).getName().endsWith("txt"); //just an example
}
};
,然后:
when(s3client.getObject(org.mockito.Matchers.argThat(objectRequestMatcher)))
.thenReturn(RawText);