本文介绍了如何在Mockito测试中修复RestTemplate.exchange的空值响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Service类在下面,然后是它的测试-

My Service class is below, followed by its test -

@Service
public class MyServiceImpl implements MyService {

        @Autowired
        private RestTemplate restTemplate;

        @Override
        public StudentInfo getStudentInfo(String name) {
            HttpHeaders headers = new HttpHeaders();
            headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);

            HttpEntity entity = new HttpEntity(headers);

            StudentInfo student = null;

            URI uri = new URI("http:\\someurl.com");

           ResponseEntity<String> responseEntity = restTemplate.exchange(uri,
                        HttpMethod.GET, entity,
                        String.class);

           if (responseEntity.getStatusCode().equals(HttpStatus.NO_CONTENT)) {
                   throw new Exception("Student absent");
            }else {
              ObjectMapper mapper = new ObjectMapper();
              StudentInfo student = mapper.readValue(responseEntity.getBody(), StudentInfo.class);

           }

            return student;
        }
    }

测试类:在下面的测试类中,我在调试时看到ResponseEntity对象为null,这会导致NPE.

Test class: In my test class below, I see ResponseEntity object as null while debugging which causes a NPE.

@RunWith(MockitoJUnitRunner.class)
public class MyServiceImplTest {

    @InjectMocks
    private MyService service = new MyServiceImpl();

    @Mock
    private RestTemplate restTemplate;

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
    }

    @Test
    public void testStudentGetterResponse() {

        ResponseEntity<String> mockEntity = Mockito.spy(new ResponseEntity({"id" : 1, "name" : "Rutzen"}, HttpStatus.OK));

        doReturn(mockEntity).when(restTemplate).exchange(any(URI.class), any(HttpMethod.class), any(ResponseEntity.class),
                any(Class.class));

        StudentInfo info = service.getStudentInfo("testuser");

        Assert.assertNotNull(info);


    }

}

调试测试时,在主服务类的以下行中,我获得responseEntity的空值-

When I debug the test, I get a null value for responseEntity at the following line in the main service class -

 ResponseEntity<String> responseEntity = restTemplate.exchange(uri,
                        HttpMethod.GET, entity,
                        String.class);

推荐答案

此说明...

doReturn(mockEntity).when(restTemplate).exchange(
    any(URI.class),
    any(HttpMethod.class),
    any(ResponseEntity.class),
    any(Class.class)
);

...应替换为:

doReturn(mockEntity).when(restTemplate).exchange(
    any(URI.class),
    any(HttpMethod.class),
    any(HttpEntity.class),
    any(Class.class)
);

因为getStudentInfo()创建HttpEntity的实例(不是 ResponseEntity),然后将其传递给restTemplate.exchange()调用.

Because getStudentInfo() creates an instance of HttpEntity (not ResponseEntity) which is then passed to the restTemplate.exchange() invocation.

这篇关于如何在Mockito测试中修复RestTemplate.exchange的空值响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 20:43