【问题标题】:Invalid use of argument matchers! 0 matchers expected, 1 recorded参数匹配器的使用无效!预期 0 个匹配器,1 个记录
【发布时间】:2014-12-12 15:28:45
【问题描述】:

虽然错误描述性很强,但我无法理解它。 对于线路:

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

错误是:

[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.

为什么需要 0 个匹配器?

【问题讨论】:

    标签: java unit-testing junit mockito


    【解决方案1】:

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

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

    【讨论】:

    • 任何理由,为什么不使用匹配器?
    猜你喜欢
    • 2020-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-17
    • 1970-01-01
    • 1970-01-01
    • 2011-09-26
    相关资源
    最近更新 更多