如何在Mockito测试中修复RestTemplate.exchange的空值响应?

时间:2017-08-14 12:04:07

标签: spring-boot junit mockito spring-test spring-boot-test

我的服务类如下,其后是测试 -

$('#panNumId').change(function () {
  var amount = $("#panNumId").val();
  alert("value appended : " + amount);
});

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

@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的空值 -

@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);


    }

}

3 个答案:

答案 0 :(得分:5)

这条指令......

'labels': '/labels/'

......应替换为:

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调用。

答案 1 :(得分:0)

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

我不会在String []

的情况下工作

喜欢

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

答案 2 :(得分:0)

因为接受的答案是正确的。我在已经接受的答案中添加了一些内容。

似乎有点奇怪,但我通过查看接受的答案并提出问题的用户添加了注释来解决了这个问题。

替换此

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)
);

如果仍然出现错误,请不要使用多行。仅使用一行并按如下所示替换它。

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