0

具有 kotlin 功能,

private fun getContentTypeFromResponse(response: Response): String? {
        return response.header(ELEM_HEADER_CONTENT_TYPE)?.toLowerCase()
            .also{
                println("+++ --- exit getContentTypeFromResponse(). contentTypet:$it")
            }
    }

它调用了:

val contentType: String? = getContentTypeFromResponse(response)

但是覆盖测试总是失败并指向它(尽管使用具有 contentType 设置或未设置的响应模拟测试了这两种情况)

在此处输入图像描述

当查看反编译的 java 代码时,似乎有一个死代码,这是 jacoco 覆盖范围抱怨,

if (var2 == null)看起来永远不会是真的,所以块永远不会被切除:

private final String getContentTypeFromResponse(Response response) {
      String var10000 = response.header("Content-Type");
      String var2;
      boolean var3;
      if (var10000 != null) {
         var2 = var10000;
         var3 = false;
         if (var2 == null) {
            throw new NullPointerException("null cannot be cast to non-null type java.lang.String");
         }

         var10000 = var2.toLowerCase();
         Intrinsics.checkNotNullExpressionValue(var10000, "(this as java.lang.String).toLowerCase()");
      } else {
         var10000 = null;
      }

      var2 = var10000;
      var3 = false;
      boolean var4 = false;
      int var6 = false;
      String var7 = "+++ --- exit getContentTypeFromResponse(). contentTypet:" + var2;
      boolean var8 = false;
      System.out.println(var7);
      return var2;
   }

如何使覆盖测试正常工作?

4

0 回答 0