所以我有这个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);
}
对我来说这很好,但出于某种原因,它说:
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"))
我一直试图找出问题很长一段时间,但仍然不确定是什么导致了这个问题。有什么提示吗?
感谢。
答案 0 :(得分:6)
罪魁祸首就是这部分:
.thenReturn(any(Orientation.class))
any()
应与When
一起使用。
做这样的事情:
@Mock
private Orientation orientationMock;
// ...
.thenReturn(orientationMock);