2013-08-07 55 views
0

我有一个测试,其仅包含此行:的Mockito抛出InvalidUseOfMatchersException而嘲笑的方法

when(service.getDTO(Matchers.<DatePeriod>any(), anyLong())).thenReturn(getMockedDTO); 

当我运行它引发以下错误测试:

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

该方法的接口我嘲笑如下:

DTO getDTO(DatePeriod period, long id); 

我根本无法看到我在做什么错在这里..希望有人可以指引我走向正确的方向! :)

+4

为了更好地尽快提供帮助,将您的代码发布为可显示您问题的[SSCCE](http://www.sscce.org)。这允许用户复制/粘贴并重现您的问题。 –

+3

如果您替换Matchers会发生什么。 任何()'与'任何(DatePeriod.class)'? –

+1

乍一看,我看不出有什么明显的错误。仔细检查getDTO是不是“最终”的 - 这些不能被Mockito嘲笑 - 并且它实际上不是前一个或后一个存根导致该错误。 –

回答

0

由于我的错误是PEBKAC和我解决它在我自己,我会回答我的问题

编辑

instead of mocking the dependant services and dao's I was trying to mock the very service i'm testing. I removed the line and mocked the other dependant services and now it seems to work as intended

(从Mockito throws InvalidUseOfMatchersException while mocking a method复制)

+1

为了让它成为答案,您应该回答“键盘和椅子之间存在的问题”将成为大多数问题的答案,如果这将是一个答案。 .. – bummi

+0

我已经做了答案.. http://stackoverflow.com/questions/18097757/mockito-throws-invaliduseofmatchersexception-while-mocking-a-method/20241651?noredirect=1#comment26533537_18097757 – Herter

+0

更新的答案后,以包括从评论中回答,以使读者更清楚 – Herter