这样的类应该被测试吗? (已测试功能的聚合)

发布于 2025-01-19 18:47:58 字数 2063 浏览 3 评论 0原文

我在A类中有以下代码,其中两个requestService.CreateGetSessionRequest()httpservice.sendasyncandreceive()已经在其他单元测试中进行了测试:

public GetSessionResponse createSession(String endpointBaseUrl, String endpointId, String salt, String endpointSecret) throws JsonProcessingException, ExecutionException, InterruptedException, HttpException {
    final HttpRequest request = requestService.createGetSessionRequest(endpointBaseUrl,endpointId,salt,endpointSecret);
    final GetSessionResponse response = httpService.sendAsyncAndReceive(request, GetSessionResponse.class);
    return response;
}

我想知道,我是否想知道,我是否应该想知道,我是否想知道创建A.Createsession()的测试?

基本上,看来开发人员在调用requestService.CreateGetSessionRequest() in a.CreateSession() 在他们'''''''''''''''''时,它似乎很有用,因为开发人员可能会意外混合参数。所有字符串。

另一方面,测试的努力似乎很高,这让我想知道方法设计(只需将多个调用汇总到一个方法中,所以我有一个更清洁的界面)

样本测试用例看起来像这样:

void createSessionSuccessfullyTest() throws HttpException, ExecutionException, InterruptedException, JsonProcessingException {
    String endpointBaseUrl = "http://te.st";
    String endpointId = "abc";
    String salt = "salt";
    String endpointSecretHash = "hash";
    HttpRequest request = requestService.createGetSessionRequest(endpointBaseUrl,endpointId,salt,endpointSecretHash);
    String endpoint_session_id = "0TbKHn9MsZKJYhfQ0FZ0W2y0RHVwxTOY";
    GetSessionResponse expected = new GetSessionResponse();
    expected.setEndpointSessionId(endpoint_session_id);
    HttpService httpService = mock(HttpService.class);
    when(httpService.sendAsyncAndReceive(request, GetSessionResponse.class)).thenReturn(expected);
    AuthenticationFlow authenticationFlow = new AuthenticationFlowImpl(requestService,httpService);
    GetSessionResponse actual = authenticationFlow.createSession(endpointBaseUrl,endpointId,salt,endpointSecretHash);
    assertThat(actual).usingRecursiveComparison().isEqualTo(expected);
}

I have the following code in Class A where both requestService.createGetSessionRequest() and httpService.sendAsyncAndReceive() are tested already in other unit tests:

public GetSessionResponse createSession(String endpointBaseUrl, String endpointId, String salt, String endpointSecret) throws JsonProcessingException, ExecutionException, InterruptedException, HttpException {
    final HttpRequest request = requestService.createGetSessionRequest(endpointBaseUrl,endpointId,salt,endpointSecret);
    final GetSessionResponse response = httpService.sendAsyncAndReceive(request, GetSessionResponse.class);
    return response;
}

I am wondering, should I even create tests for A.createSession()?

Basically it seems that it could be useful as a developer might accidentally mix up the parameters when calling requestService.createGetSessionRequest() in A.createSession()'s implementation as they're all strings.

On the other side, the efforts for a test seem pretty high, which makes me wonder about the method design (just aggregating multiple calls into one single method so I have a cleaner interface for users)

A sample test case looks like this:

void createSessionSuccessfullyTest() throws HttpException, ExecutionException, InterruptedException, JsonProcessingException {
    String endpointBaseUrl = "http://te.st";
    String endpointId = "abc";
    String salt = "salt";
    String endpointSecretHash = "hash";
    HttpRequest request = requestService.createGetSessionRequest(endpointBaseUrl,endpointId,salt,endpointSecretHash);
    String endpoint_session_id = "0TbKHn9MsZKJYhfQ0FZ0W2y0RHVwxTOY";
    GetSessionResponse expected = new GetSessionResponse();
    expected.setEndpointSessionId(endpoint_session_id);
    HttpService httpService = mock(HttpService.class);
    when(httpService.sendAsyncAndReceive(request, GetSessionResponse.class)).thenReturn(expected);
    AuthenticationFlow authenticationFlow = new AuthenticationFlowImpl(requestService,httpService);
    GetSessionResponse actual = authenticationFlow.createSession(endpointBaseUrl,endpointId,salt,endpointSecretHash);
    assertThat(actual).usingRecursiveComparison().isEqualTo(expected);
}

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

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

发布评论

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

评论(1

久随 2025-01-26 18:47:58

是的。您必须测试 A.createSession() 方法:

  1. 没有人可以保证代码将保持不变。如果您或其他开发人员必须重构或更改您的代码,
    你将能够安全地做到这一点,因为你将通过测试检查正确性,
    否则开发人员可能会无意中破坏某些东西。
  2. 当你编写测试时,你实际上会考虑一个好的设计,
    没有测试,你的结局将会很糟糕。你自己也提到过:

这让我对方法设计感到好奇

至于测试的努力 - 这意味着你的测试类中有糟糕的设计,你必须重构并保持它们(测试)像主代码一样清晰。

您可以在问题。

Yes. You definetely have to test A.createSession() method:

  1. Nobody can guarantee you that code will remain the same. If you or other developer will have to refactor or change your code,
    you will able to do it safely, because you will check correctness by test,
    otherwise developer can break something unintentionally.
  2. When you write tests, you actually think about a good design,
    without tests, you will end badly. You mention it yourself:

which makes me wonder about the method design

As for efforts for a testing - it means you have the bad design in your test classes and you have to refactor and keep them (tests) clear like the main code.

You can read more why you have to test and why it so important in that question.

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