Get jacoco report from running acceptance test on real application
Asked Answered
P

1

5

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,

Pyrometallurgy answered 17/1, 2018 at 11:33 Comment(0)
W
9

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.

Whomsoever answered 18/1, 2018 at 12:49 Comment(3)
Thanks, it's exactly what I was looking for.Pyrometallurgy
@Pyrometallurgy please consider not just accepting answer, but also upvoting at the same timeWhomsoever
Actually I did), but I upvoted it before accepting, so it may appear that it wasn't me)Pyrometallurgy

© 2022 - 2024 — McMap. All rights reserved.