简体   繁体   English

java mockito 匹配器 InvalidUseOfMatchersException

[英]java mockito matcher InvalidUseOfMatchersException

I am using java spring boot and trying to write a mock for AWS s3 bucket in my unittest.我正在使用 java spring boot 并尝试在我的单元测试中为 AWS s3 存储桶编写一个模拟。 Following is a code that cause some issue in when i execute the test以下是在我执行测试时导致一些问题的代码

    @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);

What i am trying to do is, I need to read png file and a text file from S3 bucket.我想要做的是,我需要从 S3 存储桶中读取 png 文件和文本文件。 based on the content type i am trying to return the object.基于我试图返回对象的内容类型。 When i execute the test i am getting当我执行测试时,我得到

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: 
Invalid use of argument matchers!
1 matchers expected, 2 recorded:

Note RawImage and RawText are the S3Object i created.注意RawImageRawText是我创建的S3Object Can you help me in this and what went wrong in my code?你能帮我解决这个问题吗?我的代码出了什么问题?

Matchers are expected to be used as arguments to getObject in this case.在这种情况下,匹配器将被用作getObject参数。 So you may want to implement a custom matcher if your actual argument isn't a mock:因此,如果您的实际参数不是模拟,您可能想要实现自定义匹配器:

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

And then:进而:

when(s3client.getObject(org.mockito.Matchers.argThat(objectRequestMatcher)))
    .thenReturn(RawText);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 org.mockito.exceptions.misusing.InvalidUseOfMatchersException错误放置的参数匹配器 - org.mockito.exceptions.misusing.InvalidUseOfMatchersException Misplaced argument matcher 当我没有匹配器时org.mockito.exceptions.misusing.InvalidUseOfMatchersException - org.mockito.exceptions.misusing.InvalidUseOfMatchersException when i have no matcher org.mockito.exceptions.misusing.InvalidUseOfMatchersException:错位的参数匹配器 - org.mockito.exceptions.misusing.InvalidUseOfMatchersException:Misplaced argument matcher Mockito-InvalidUseOfMatchersException - Mockito - InvalidUseOfMatchersException Mockito InvalidUseOfMatchersException - Mockito InvalidUseOfMatchersException Mockito:InvalidUseOfMatchersException - Mockito: InvalidUseOfMatchersException Java Mockito InvalidUseOfMatchersException when mocking 方法 - Java Mockito InvalidUseOfMatchersException when mocking method Mockito 和 Powermockito org.mockito.exceptions.misusing.InvalidUseOfMatchersException:检测到错误放置的参数匹配器 - Mockito and Powermockito org.mockito.exceptions.misusing.InvalidUseOfMatchersException: Misplaced argument matcher detected Mockito.when 给出 InvalidUseOfMatchersException:此处检测到错误放置或误用的参数匹配器 - Mockito.when giving InvalidUseOfMatchersException: Misplaced or misused argument matcher detected here java 8 个函数的 Mockito 参数匹配器 - Mockito argument matcher for java 8 functions
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM