servicecomb-java-chassis icon indicating copy to clipboard operation
servicecomb-java-chassis copied to clipboard

TestRestClientInvocation的单元测试无法有效验证功能

Open yhs0092 opened this issue 3 years ago • 2 comments

问题出在 TestRestClientInvocation 的 testSetCseContext_enable_unicode 方法和 testSetCseContext_disable_unicode 方法中, 两个方法都是开头对invocation.getContext()进行mock, 结尾又调用invocation.getContext()获取结果进行校验, 获取的结果就是mock返回, 所以达不到测试的预期效果. 1.3.x 和 2.8.x 分支均有此问题, 建议优化一下UT代码.

https://github.com/apache/servicecomb-java-chassis/blob/21d4b4863b71332f151eb63020f4e9ac51583002/transports/transport-rest/transport-rest-client/src/test/java/org/apache/servicecomb/transport/rest/client/http/TestRestClientInvocation.java#L282-L346

yhs0092 avatar Mar 31 '23 06:03 yhs0092

这个测试本身我试过了是可以通过的 我没发现问题在哪儿 你是说这个测试方法本身要验证的点就是不对的吗

lbc97 avatar Apr 20 '23 03:04 lbc97

你是说这个测试方法本身要验证的点就是不对的吗

是的, invocation.getContext() 方法本身被 mock 了, 所以你在单元测试方法最后取的测试值是mock数据contextMap里面的值, 而非UT中经过计算转换的值.

你可以试一下把org.apache.servicecomb.transport.rest.client.http.RestClientInvocation#setCseContext 和 org.apache.servicecomb.common.rest.AbstractRestInvocation#setContext都注释掉, 不会影响你这个单元测试的结果.

yhs0092 avatar May 08 '23 12:05 yhs0092