记一次代码覆盖率JaCoCo问题排查

java  -javaagent:jacocoagent.jar=includes=*,classdumpdir=classes,output=tcpserver,port=6888,address=127.0.0.1

采用这种方式启动tomcat,用于代码覆盖率统计
启动过程中遇到问题,提示:

org.springframework.web.context.support.XmlWebApplicationContext] - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'springServiceManager' defined in class path resource [applicationContext.xml]: Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.cnc.dna.platform.core.SpringServiceManager]: Constructor threw exception; nested exception is java.lang.RuntimeException: java.lang.IllegalAccessException: Class ……

百度寻求解决方案,看到一个差不多的案例https://blog.csdn.net/kp783491236/article/details/80648063
然后进入了源码分析https://blog.csdn.net/ohcezzz/article/details/78416125
没找到解决方案,下载最新的JaCoCo包也还是一样报错

在认真看报错,有一个"private static transient",于是查找到相关资料https://zhidao.baidu.com/question/422083927.html

http://www.alanzeng.cn/2016/06/eclemma-test-refelect-failed/

==================以上都是废话可以不看================

最后解决了,解决方案是启动命令中的includes参数不用*了,要明确到包,命令改成如下:

java  -javaagent:jacocoagent.jar=includes=com.cn.*,classdumpdir=classes,output=tcpserver,port=6888,address=127.0.0.1

原因是Jacoco 引起反射异常也叫EclEmma引起反射相关测试失败的问题,Jacoco 会利用编译器在编译期间加入 JacocoData成员变量,如果使用反射循环成员变量进而拼凑set或者get方法,会产生NoSuchMethodException,建议在循环中利用isSynthetic()方法检查成员变量(参考https://blog.csdn.net/stanleyw2014/article/details/83414950)

你可能感兴趣的:(记一次代码覆盖率JaCoCo问题排查)