Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/392.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Mockito参数匹配器的使用无效_Java_Junit_Mockito - Fatal编程技术网

Java Mockito参数匹配器的使用无效

Java Mockito参数匹配器的使用无效,java,junit,mockito,Java,Junit,Mockito,我有以下代码 public MessageProcessorTest() { _logger = mock(Logger.class); _context = mock(ExecutionContext.class); when(_context.getLogger()).thenReturn(_logger); doNothing().when(_logger).log(any(), anyString()); _mapper = mock(IMap

我有以下代码

public MessageProcessorTest() {
    _logger = mock(Logger.class);
    _context = mock(ExecutionContext.class);

    when(_context.getLogger()).thenReturn(_logger);
    doNothing().when(_logger).log(any(), anyString());

    _mapper = mock(IMapper.class);

    _processor = new MessageProcessor(_mapper, _context);
}

@Test
public void testOutputSourceIsMapped() throws SentenceException
{
    String inputString = "some string";
    when(_mapper.Map(any())).thenReturn(any());

    _inputMessage = new ProcessMessage(inputString, new Date().toString());

    ProcessResult result = _processor.Process(_inputMessage);

    assertNotNull(result);
    assertNotNull(result.GetOriginalMessage());
    assertNotNull(result.OutputMessage);
    assertTrue(result.IsSuccessful);

    Raw rawResult = new Gson().fromJson(result.OutputMessage, Raw.class);

    assertEquals(FeedSources.S.toString(), rawResult.GetSource());
}
我得到以下错误

detailMessage:"\nInvalid use of argument matchers!\n0 matchers expected, 1 recorded:\n-> at com.emsa.hpims.processor.MessageProcessorTest.testOutputSourceIsMappedToSatAis(MessageProcessorTest.java:61)\n\nThis exception may occur if matchers are combined with raw values:\n    //incorrect:\n    someMethod(anyObject(), "raw String");\nWhen using matchers, all arguments have to be provided by matchers.\nFor example:\n    //correct:\n    someMethod(anyObject(), eq("String by matcher"));\n\nFor more info see javadoc for Matchers class.\n"
执行以下行时

_context.getLogger().log(Level.FINE, "Some log message");
我读过一些关于它的文章,但我不明白我到底做错了什么。有人能帮我吗


谢谢。

问题很可能是这一行:

when(_mapper.Map(any())).thenReturn(any());
在这里,当用任何东西调用
mapper.map()
时,您告诉Mockito返回
any()
(它返回一个匹配器)

模拟的全部意义在于允许用户精确地指定模拟方法返回的内容。您基本上已经告诉Mockito在调用方法时返回任何内容,这是不允许的


该方法应该返回什么来允许您实现测试用例?用它替换第二个
any()

问题很可能是这一行:

when(_mapper.Map(any())).thenReturn(any());
在这里,当用任何东西调用
mapper.map()
时,您告诉Mockito返回
any()
(它返回一个匹配器)

模拟的全部意义在于允许用户精确地指定模拟方法返回的内容。您基本上已经告诉Mockito在调用方法时返回任何内容,这是不允许的


该方法应该返回什么来允许您实现测试用例?用它替换第二个
any()

Hum。。。问题是我真的不在乎它返回什么,因此any()。关于如何实现这一点有什么想法吗?
Any()
不代表“返回任何东西”。它返回
null
,只需使用它即可。同样,使用
any()
进行此操作可能会导致mockito出错,因为它可能无法解析自己的模拟语法,所以不要以这种方式使用它。@FEST如果您不关心返回的内容,您可能根本不应该模拟它。然后它将返回一个默认值(例如,如果模拟方法返回“常规”对象,则返回
null
,如果返回
列表,则返回一个空列表)。。。问题是我真的不在乎它返回什么,因此any()。关于如何实现这一点有什么想法吗?
Any()
不代表“返回任何东西”。它返回
null
,只需使用它即可。同样,使用
any()
进行此操作可能会导致mockito出错,因为它可能无法解析自己的模拟语法,所以不要以这种方式使用它。@FEST如果您不关心返回的内容,您可能根本不应该模拟它。然后,它将返回一个默认值(例如,如果模拟方法返回“常规”对象,则返回
null
,如果返回
列表,则返回一个空列表
)。