Invalid use of argument matchers! 0 matchers expected, 1 recorded

时间秒杀一切 提交于 2019-12-21 19:56:03

问题


Although the error is quite descriptive I could not get a hang of it. For lines:

    PowerMockito.when(
            mockStringMessageService.lookupString(Matchers.eq("XYZ")))
            .thenReturn(Matchers.eq("XYZ"));

Error is:

[junit] Invalid use of argument matchers!
[junit] 0 matchers expected, 1 recorded:
[junit] -> at com.amazon.kilvish.types.StatusTableTest.setUp(StatusTableTest.java:61)
[junit] 
[junit] This exception may occur if matchers are combined with raw values:
[junit]     //incorrect:
[junit]     someMethod(anyObject(), "raw String");
[junit] When using matchers, all arguments have to be provided by matchers.
[junit] For example:
[junit]     //correct:
[junit]     someMethod(anyObject(), eq("String by matcher"));
[junit] 
[junit] For more info see javadoc for Matchers class.

Why are 0 matchers expected?


回答1:


You cannot use matchers in the thenReturn clause. Just use the string literal instead:

PowerMockito.when(
        mockStringMessageService.lookupString(Matchers.eq("XYZ")))
        .thenReturn("XYZ");


来源:https://stackoverflow.com/questions/26418376/invalid-use-of-argument-matchers-0-matchers-expected-1-recorded

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!