这样的类应该被测试吗? (已测试功能的聚合)
我在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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的。您必须测试
A.createSession()
方法:你将能够安全地做到这一点,因为你将通过测试检查正确性,
否则开发人员可能会无意中破坏某些东西。
没有测试,你的结局将会很糟糕。你自己也提到过:
至于测试的努力 - 这意味着你的测试类中有糟糕的设计,你必须重构并保持它们(测试)像主代码一样清晰。
您可以在问题。
Yes. You definetely have to test
A.createSession()
method:you will able to do it safely, because you will check correctness by test,
otherwise developer can break something unintentionally.
without tests, you will end badly. You mention it yourself:
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.