5

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

@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 对象为空,这会导致 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 的空值 -

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

3 回答 3

9

这个指令...

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( not ResponseEntity ) 的实例,然后将其传递给restTemplate.exchange()调用。

于 2017-08-14T12:38:47.583 回答
1

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

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

替换这个

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)
);
于 2020-07-16T11:27:54.193 回答
0
 ResponseEntity<String> responseEntity = restTemplate.exchange(uri,
                        HttpMethod.GET, entity,
                        String.class);

我不会在 String[] 的情况下工作

ResponseEntity<String[]> responseEntity = restTemplate.exchange(uri,
                        HttpMethod.GET, entity,
                        String[].class);
于 2018-01-30T10:41:00.917 回答