Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

spring boot - Get jacoco report from running acceptance test on real application

I'm new to sonar and jacoco and I wasn't able to find an ansewer to following question

We are going to use sonar and jacoco to analyze our test coverage.

We are going to have three kind of test: unit test, integration test (use spring boot test) and acceptance test that we will run on real application instance. We want to merge test results. To generating jacoco files for unit and integration tests is not a problem as they have access to source code. And we can merge this reports in sonar.

My question is it possible to generate jacoco file for acceptance test which interacts with real application? And maybe you will have a link to how to do it?

My best idea for now is to run acceptance tests twice on both real application and embedded one, and get report from embedded. But maybe there is better solution. Thanks,

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

JaCoCo is able to record any kind of execution of Java application - you just need to start this application with agent, for example:

java -javaagent:jacoco-0.8.0/lib/jacocoagent.jar -cp classes Main

By default at termination of application this will produce file jacoco.exec with execution data. There are also ways to get execution data from running application.

And there are plenty of examples all over the internet of using agent in various cases - with Spring Boot, Tomcat, Weblogic, etc.

After that report can be generated using this execution data jacoco.exec and exactly the same class files that were used at runtime for generation of execution data. For example using JaCoCo command line interface (there are also Ant tasks, Gradle plugin, Maven plugin):

java -jar jacoco-0.8.0/lib/jacococli.jar 
    report 
    jacoco.exec 
    --classfiles classes 
    --html report

Report can be generated without source files, but in this case you won't be able to drill down lower than method level granularity:

report

report for class without sources

Report with source files:

jacoco-0.8.0/lib/jacococli.jar 
  report 
  jacoco.exec 
  --classfiles classes 
  --html report 
  --sourcefiles src

report for class with sources

report for source file

Now about SonarQube: it performs analysis of exec and class files, and so also requires that class files are the same as where used at runtime for generation of exec file. So you need to guarantee that SonarQube analysis is performed for exactly the same class files as under testing.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...