5

I'm trying to mock a restTemplate.exchange method through mockito but it always returns a null value no matter what i return through mock , even if i throw an exception:

this is the actual code :

ResponseEntity<List<LOV>> response = restTemplate.exchange(url, GET, null,new ParameterizedTypeReference<List<LOV>>() {});

mockito code:

 ResponseEntity<List<LOV>> mockResponse = new ResponseEntity<List<LOV>>(mockLovList() ,HttpStatus.ACCEPTED);

Mockito.when(restTemplate.exchange(any(), eq(GET), any(), ArgumentMatchers.<ParameterizedTypeReference<List<LOV>>>any())).thenReturn(mockResponse);

Every argument is of type ArgumentMatchers in the exchange mock , mockLovList() returns a list of LOV

it should return whatever i mocked , but it always returns null

4

4 回答 4

3

这是一个RestTemplate.exchange()模拟测试的工作示例:

import org.junit.Assert;
import org.junit.Test;
import org.mockito.Mockito;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.util.Arrays;
import java.util.List;

import static org.mockito.Mockito.*;

public class MyTest {

    @Test
    public void testRestTemplateExchange() {
        RestTemplate restTemplate = mock(RestTemplate.class);

        HttpEntity<String> httpRequestEntity = new HttpEntity<>("someString");

        List<String> list = Arrays.asList("1", "2");
        ResponseEntity mockResponse = new ResponseEntity<>(list, HttpStatus.ACCEPTED);
        when(restTemplate.exchange(anyString(), any(), any(), any(ParameterizedTypeReference.class), any(Object[].class)))
                .thenReturn(mockResponse);


        final ResponseEntity<List<String>> exchange = restTemplate.exchange("/someUrl", HttpMethod.GET, httpRequestEntity, new ParameterizedTypeReference<List<String>>() {
        });

        Assert.assertEquals(list, exchange.getBody());

    }

}
于 2019-04-09T12:11:55.480 回答
2

在设置或注入服务之前,您需要确保您的模拟已初始化。像这样-

@Mock
RestTemplate restTemplate;
@Before
public void init() {
    MockitoAnnotations.initMocks(this);
    userData = new UserService(restTemplate);
}
于 2021-07-02T14:35:36.043 回答
0

首先,您可以使用验证方法调用是否正确

 Mockito.verify(restTemplate).exchange(any(), eq(GET), any(), any(ParameterizedTypeReference.class)))

Mockito 显示了一个非常好的输出,包括实际的方法调用。

此外,您可以参考Deep Stubbing测试匿名类

于 2019-04-09T11:46:26.807 回答
0

我有同样的问题(至少 2 个涉及这个问题),在寻找一个合理的答案后,我可以成功解决它。

在我解释之前,我将分享两个对我有帮助的解释。如何模拟 REST 模板交换?Mocking RestTemplate.exchange() 方法给出空值

所以,基本上,关键是每个涉及任何 RestTemplate 方法的测试用例都必须正确地给出每个参数。如果不行,Mockito 会失败并抛出如下异常:

org.mockito.exceptions.misusing.PotentialStubbingProblem

或使用 NullPointerException 从 restTemplate.exchange 获取 ResponseEntity (这是你的情况)

之前(我隐藏了一些实现。Mockito 抛出异常)

when(restTemplate.exchange(anyString(), any(HttpMethod.class), any(), 
                    ArgumentMatchers.<ParameterizedTypeReference<FilterBean>>any())).thenReturn(responseEntityFilterBean);

模拟异常

org.mockito.exceptions.misusing.PotentialStubbingProblem:严格的存根参数不匹配。请检查:

  • 'exchange' 方法的调用:restTemplate.exchange("/filter", GET, <[Content-Type:"application/json"]>, class br.com.test.bean.FilterBean); -> 在 br.com.test.service.FilterService.getFilterStatus(FiltroService.java:60)
  • 具有以下具有不同参数的存根:
    1. restTemplate.exchange("", null, null, null);

这表明参数不正确!

更改我的代码它解决了我的问题。

                when(restTemplate.exchange(ArgumentMatchers.anyString(), 
                    ArgumentMatchers.any(HttpMethod.class),
                    ArgumentMatchers.any(), 
                    ArgumentMatchers.<Class<FilterBean>>any())).thenReturn(responseEntityFilterBean);

在你的情况下,尝试这样做,它会解决:

        when(restTemplate.exchange(ArgumentMatchers.anyString(), 
                    ArgumentMatchers.any(HttpMethod.class),
                    ArgumentMatchers.any(), ArgumentMatchers.<Class<List<String>>>any())))
            .thenReturn(mockResponse);
于 2021-10-14T20:49:19.263 回答