想要但不被调用时被调用

时间:2018-11-02 12:52:26

标签: java testing junit mockito verify

我正在尝试通过Mockito.verify验证OAuth2RestTemplate patchForObject方法,但是它根本无法正常工作。我还有其他模拟方法无法正常工作,例如getForObject,该方法未正确存根,而是返回默认null。 这是我的代码:

@RunWith(MockitoJUnitRunner.class)
public class ClientPaasTest {
    @Mock
    private OAuth2RestTemplate clientCredentialsRestTemplateMock;

    @InjectMocks
    private ClientPaas clientPaas;

    @Before
    public void setup() {
        Mockito.reset(clientCredentialsRestTemplateMock);
    }
    @Test
    public void testPatchCarteJetonInterac() {
        clientPaas.patchCarteJetonInterac(null, "carte1");   
        Mockito.verify(clientCredentialsRestTemplateMock).patchForObject(null, null, Object.class);
    }
}

并且我收到以下错误:

Wanted but not invoked:
clientCredentialsRestTemplateMock.patchForObject(
    null,
    null,
    class java.lang.Object
);
-> at com.pmtcartes.util.ClientPaasTest.testPatchCarteJetonInterac
(ClientPaasTest.java:91)

However, there were other interactions with this mock:
clientCredentialsRestTemplateMock.patchForObject(
    null,
    null,
    class java.lang.Object
);
-> at
com.pmtcartes.util.ClientPaas.patchCarteJetonInterac(ClientPaas.java:70)

对我来说,哪个看起来完全一样...有人有解决方案吗?我已经在许多其他项目上做了多次。我不确定发生了什么。

1 个答案:

答案 0 :(得分:0)

我发现了原因。该模拟正在对具有不同签名的OAuth2RestTemplate.getForObject或patchForObject进行存根。 嘲笑:

public <T> T patchForObject(String url, Object request, Class<T> responseType,
        Object... uriVariables) throws RestClientException {

而不是:

public <T> T patchForObject(URI url, Object request, Class<T> responseType) 
        throws RestClientException {