java Mockito Matcher InvalidUseOfMatchersException

时间:2019-02-23 09:07:34

标签: java spring-boot junit mockito

我正在使用Java Spring Boot并尝试在单元测试中编写适用于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:

注意RawImageRawText是我创建的S3Object。您可以在这方面帮助我吗,我的代码出了什么问题?

1 个答案:

答案 0 :(得分:1)

在这种情况下,应该将匹配器用作x1 = (((-b) + cmath.sqrt(float(d))) / (2*a)) x2 = (((-b) - cmath.sqrt(float(d))) / (2*a)) if x1 == x2: print "x = ", x1.real else: print "x = ", x1.real, "or", x2.real 的参数。因此,如果您的实际参数不是模拟值,则可能要实现自定义匹配器:

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
    }
};