java Java中的Mockito“此处检测到错位参数”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32909129/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Mockito 'Misplaced argument detected here' in Java
提问by stack man
So I have this Mockito unit test:
所以我有这个 Mockito 单元测试:
@Test
public void createCard() {
when(jwtServiceMock.getId(anyString())).thenReturn(validUserToken);
when(profileServiceMock.getProfile(validUserToken)).thenReturn(mock(Profile.class));
when(cardServiceMock.countViewableCardsCreatedOrOwnedBy(anyObject())).thenReturn(5L);
when(cardServiceMock.countCardsCreatedOrOwned(anyObject())).thenReturn(10L);
final Card expectedCard = getCard();
when(cardServiceMock.createCard(anyString(), anyListOf(String.class), anyListOf(String.class),
any(CreatorRecipientCriteria.class), anyListOf(ImageMask.class))).thenReturn(expectedCard);
when(imageService.createCardImage(any(MultipartFile.class), anyString(), any(ImageMask.class))).thenReturn(any(Orientation.class));
final Card receivedCard = cardControllerMock.createCard(validUserToken, mock(MultipartFile.class), "card");
assertEquals(receivedCard, expectedCard);
}
It looks fine for me, but for some reason it says:
对我来说看起来不错,但出于某种原因,它说:
org.mockito.exceptions.misusing.InvalidUseOfMatchersException:
Misplaced argument matcher detected here:
-> at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallStatic(CallSiteArray.java:53)
You cannot use argument matchers outside of verification or stubbing.
Examples of correct usage of argument matchers:
when(mock.get(anyInt())).thenReturn(null);
doThrow(new RuntimeException()).when(mock).someVoidMethod(anyObject());
verify(mock).someMethod(contains("foo"))
I have been trying to find out what's wrong for a long time, but still not sure what's causing the issue. Any hint please?
很长一段时间以来,我一直试图找出问题所在,但仍然不确定是什么导致了问题。请问有什么提示吗?
Thanks.
谢谢。
回答by Adam Arold
The culprit is this part:
罪魁祸首是这部分:
.thenReturn(any(Orientation.class))
any()
is supposed to be used in conjunction with When
.
any()
应该与When
.
Do something like this:
做这样的事情:
@Mock
private Orientation orientationMock;
// ...
.thenReturn(orientationMock);