java 球衣/Mockito:NullInsteadOfMockException on client.post 调用验证

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28895450/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 14:22:30  来源:igfitidea点击:

jersey/Mockito: NullInsteadOfMockException on client.post call verification

javajerseymockito

提问by Joanna Derks

I've run into a strange issue with unit testing the following jersey client call:

我在对以下 jersey 客户端调用进行单元测试时遇到了一个奇怪的问题:

WebResource webResource = _client.resource(url);
ClientResponse response = webResource
                        .accept("application/json")
                        .type("application/x-www-form-urlencoded")
                        .post(ClientResponse.class, postBody);

PostBody is a MultivaluedMap.

PostBody 是一个多值映射。

The unit test verifies fine the acceptand typecalls but fails on the postone with this exception:

单元测试验证了accepttype调用,但在post此异常中失败:

org.mockito.exceptions.misusing.NullInsteadOfMockException: 
Argument passed to verify() should be a mock but is null!

Here's the test code:

这是测试代码:

_client = Mockito.mock(Client.class);
        _webResource = Mockito.mock(WebResource.class);
        _builder = Mockito.mock(WebResource.Builder.class);
        _response = Mockito.mock(ClientResponse.class);

        Mockito.when(_client.resource(Mockito.anyString())).thenReturn(_webResource);

        Mockito.when(_response.getEntity(Mockito.any(Class.class))).thenReturn(new RefreshTokenDto());
        Mockito.when(_response.getStatus()).thenReturn(200);

        Mockito.when(_builder.post(Mockito.eq(ClientResponse.class), Mockito.anyObject())).thenReturn(_response);
        Mockito.when(_builder.type(Mockito.anyString())).thenReturn(_builder);
        Mockito.when(_webResource.accept(Mockito.anyString())).thenReturn(_builder);

        RefreshTokenDto response = _clientWrapper.exchangeAuthorizationCodeForToken(_token);

        Assert.assertNotNull(response);

        Mockito.verify(_client.resource(_baseUrl + "token"));
        Mockito.verify(_webResource.accept("application/json"));
        Mockito.verify(_builder.type("application/x-www-form-urlencoded"));

        // TODO: this line throws NullRefExc for some unknown reason
        Mockito.verify(_builder.post(Mockito.any(Class.class), Mockito.any(MultivaluedMap.class)));

Can you see anything wrong with this code?

你能看出这段代码有什么问题吗?

回答by Dawood ibn Kareem

Yes. You've misused verify. The argument to verifyhas to be the mock itself. Then you call the method you want to verify on the value that's returned by verify. So in this case, the first verifycall should be

是的。你误用了verify. 的论点verify必须是模拟本身。然后调用要验证的方法,该方法由 返回的值verify。所以在这种情况下,第一个verify调用应该是

Mockito.verify(_client).resource(_baseUrl + "token");

and similarly for the other verifycalls.

其他verify电话也类似。

回答by entpnerd

I ran into this issue, but for when()instead of verify(). I found this question by googling. For me, I had forgotten to add MockitoAnnotations.initMocks()in my test class' constructor.

我遇到了这个问题,但when()不是verify(). 我通过谷歌搜索发现了这个问题。对我来说,我忘记添加MockitoAnnotations.initMocks()测试类的构造函数。