当前位置: 首页 > 知识库问答 >
问题:

Java中的Mockito“此处检测到错误位置的参数”

谷越
2023-03-14
@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"))

多谢了。

共有1个答案

公冶翰池
2023-03-14

罪魁祸首就是这一部分:

.thenReturn(any(Orientation.class))

any()应与when结合使用。

这样做:

@Mock
private Orientation orientationMock;

// ...

.thenReturn(orientationMock);
 类似资料: