.getBody() 方法没有 return 响应 - SpringBoot,Java

.getBody() method doesn't return response - SpringBoot, Java

HttpDataClient.java class 正在向外部服务发送 dataId 并且应该会收到一些响应。对于我的测试,我应该测试 RestTemplate 并检查我是否得到良好的响应。

public class HttpDataClient implements DataClient{
    
        private final static Logger LOGGER = LoggerFactory.getLogger(HttpDataClient.class);
    
        private final RestTemplate restTemplate;
        private final ObjectMapper objectMapper = new ObjectMapper();
    
        public HttpDataClient(RestTemplate restTemplate) {
            this.restTemplate = restTemplate;
        }
    
        @Override
        public DataResponse getData(String dataId) {
            try{
                JsonNode node = restTemplate.exchange(
                        String.format("/data/{0}", dataId),
                        HttpMethod.POST,
                        new HttpEntity<>(buildRequest(dataId), headers()),
                        JsonNode.class
                ).getBody();
                return dataResponse(node);
            }catch (HttpStatusCodeException e) {
                String msg = String.format(
                        "Error getting data for dataId: {0}",
                        dataId,
                        e.getStatusCode(),
                        e.getResponseBodyAsString());
                LOGGER.error(msg);
                return dataResponse.failed();
            }
        }
    
        private MultiValueMap<String, String> headers() {
            final LinkedMultiValueMap<String, String> mv = new LinkedMultiValueMap<>();
            mv.set(HttpHeaders.CONTENT_TYPE, "application/json");
            return mv;
        }
    
        private DataResponse dataResponse(JsonNode node) {
            return DataResponse.dataResponse(
                    asString(node, "dataId"),
                    asString(node, "author"),
                    asString(node, "authorDataId"),
                    asString(node, "serverSideDataId")
            );
        }
    
        private JsonNode buildRequest(String dataId) {
            ObjectNode root = objectMapper.createObjectNode();
            root.put("dataId", dataId);
            return root;
        }
    }

DataResponse.java

public class DataResponse {

    public final String dataId;
    public final String author;
    public final String authorDataId;
    public final String serverSideDataId;

    public DataResponse(
            String dataId,
            String author,
            String authorDataId,
            String serverSideDataId) {
        notNull(dataId, "dataId must be set");
        notNull(author, "author must be set");
        notNull(authorDataId, "authorDataId must be set");
        notNull(serverSideDataId, "serverSideDataId must be set");
        this.dataId = dataId;
        this.author = author;
        this.authorDataId = authorDataId;
        this.serverSideDataId = serverSideDataId;
    }

    public static GameDataResponse gameDataResponse(
            String gameId,
            String vendor,
            String vendorGameId,
            String serverSideGameId){
        return new GameDataResponse(
                gameId,
                vendor,
                vendorGameId,
                serverSideGameId);
    }

    //TODO PD-849 - add faild method
    public static GameDataResponse failed() {
        return new GameDataResponse(
                failed().gameId,
                failed().vendor,
                failed().vendorGameId,
                failed().serverSideGameId
        );
    }
}

接口:

public interface DataService {

    DataResponse getData(String dataId);
}

这里是测试class:

@RunWith(MockitoJUnitRunner.class)
public class HttpDataServiceTest {

    RestTemplate restTemplate = Mockito.mock(RestTemplate.class);
    private final HttpDataService httpDataService = new HttpDataService(restTemplate);

    @Test
    public void getData(){

        ObjectNode responseNode = JsonNodeFactory.instance.objectNode();
        responseNode.put("dataId", "");
        responseNode.put("author", "");
        responseNode.put("authorDataId", "");
        responseNode.put("serverSideDataId", "");

        Mockito
            .when(restTemplate.exchange(
                    ArgumentMatchers.eq("/data/dataIdTest"),
                    ArgumentMatchers.eq(HttpMethod.POST),
                    ArgumentMatchers.any(),
                    ArgumentMatchers.<Class<JsonNode>>any()))
            .thenReturn(new ResponseEntity<>(responseNode, HttpStatus.OK));

        httpDataService.getData("dataIdTest");


    }
}

所以,当我调试时,我进行了测试 class 并且一切正常,然后它进入 HttpDataClient.java class 到 getData() method and when I am in restTemplate.exchangeI can see thatdataIdis sent, then it goes down to.getBody();` 然后它中断了。我在 return.

中什么也没有得到

我在这里错过了什么? 任何建议表示赞赏。

基于快速浏览 - 尝试更改

String.format("/data/{0}", dataId)

String.format("/data/%s", dataId)

在您的 DataResponse getData(String dataId) 方法中