参数匹配器的无效使用!预计 0 个匹配者,记录 1 个

Ama*_*tam 4 java junit unit-testing mockito

尽管该错误具有很强的描述性,但我无法掌握它。对于线路:

    PowerMockito.when(
            mockStringMessageService.lookupString(Matchers.eq("XYZ")))
            .thenReturn(Matchers.eq("XYZ"));
Run Code Online (Sandbox Code Playgroud)

错误是:

[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.
Run Code Online (Sandbox Code Playgroud)

为什么需要 0 个匹配器?

Mur*_*nik 5

您不能在thenReturn子句中使用匹配器。只需使用字符串文字:

PowerMockito.when(
        mockStringMessageService.lookupString(Matchers.eq("XYZ")))
        .thenReturn("XYZ");
Run Code Online (Sandbox Code Playgroud)