我是在方法中处理NulpoInterException还是修复了单元测试?

发布于 2025-01-29 08:40:41 字数 923 浏览 1 评论 0 原文

我的Java应用程序中有此方法:

@Override
public List<Client> getClients(String username) {
    ParameterizedTypeReference<List<Client>> tRef = new ParameterizedTypeReference<List<Client>>() {
    };
    HttpHeaders headers = createHeaders();
    headers.add(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE);
    HttpEntity<String> request = new HttpEntity<>(headers);
    ResponseEntity<List<Client>> response = restTemplate.exchange(aPIEndpoint + Constants.GET_CLIENTS,
            HttpMethod.GET, request, tRef, username);
    if (response.getStatusCode().is2xxSuccessful()) {
        return response.getBody();
    } else {
        return new ArrayList<>();
    }
}

我在我的单元测试之一中注意到该行 if(wissy.getStatusCode()。is2xxsuccessful()){导致 nullpointerexception 。我想知道我该如何处理。我应该在getClients()方法中处理此例外,还是我的测试需要以某种方式解决?

I have this method in my Java application:

@Override
public List<Client> getClients(String username) {
    ParameterizedTypeReference<List<Client>> tRef = new ParameterizedTypeReference<List<Client>>() {
    };
    HttpHeaders headers = createHeaders();
    headers.add(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE);
    HttpEntity<String> request = new HttpEntity<>(headers);
    ResponseEntity<List<Client>> response = restTemplate.exchange(aPIEndpoint + Constants.GET_CLIENTS,
            HttpMethod.GET, request, tRef, username);
    if (response.getStatusCode().is2xxSuccessful()) {
        return response.getBody();
    } else {
        return new ArrayList<>();
    }
}

I noticed in one of my unit tests that line if (response.getStatusCode().is2xxSuccessful()) { causes a NullPointerException. I'm wondering how I should deal with this. Should I handle this exception in my getClients() method or does my test need fixed in some way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小嗲 2025-02-05 08:40:41

仅修复单元测试不会有助于处理实际错误案例。

对我来说,您的方法肯定会从尝试 /捕获中受益,因为它执行了无法预测的外部呼叫。

然后,您可以模拟API调用并模拟各种方案,以确保通过单位测试处理所有情况(成功或失败)。

Just fixing your unit test won’t help handling real error cases.

To me, your method would definitely benefit from a try / catch as it performs an external call which you can’t predict.

Then, you could mock your API call and simulate various scenarios to make sure you handle all cases (success or failure) with unit tests.

你与清晨阳光 2025-02-05 08:40:41

您会得到NPE原因 RESTTEMPLATE.EXCHANGE(..)返回null响应。

RESTTEMPLATE可以是模拟对象,您可以返回模拟响应。

将RESTTEMPLAPE用作模拟。 https://wwwww.baeldung.com/spring-mock-mock-mock-rest-template

或使用 testRestTempate

You get NPE cause restTemplate.exchange(..) returns Null response.

RestTemplate can be mock object and you can return mock response.

using RestTemplate as mock. https://www.baeldung.com/spring-mock-rest-template

or use TestRestTempate. https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/test/web/client/TestRestTemplate.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文