Getting Coverage on TestClasses With Jacoco

  Kiến thức lập trình

I am trying to get coverage on Hadoop Test Classes to see what’s going on with the tests. Specifically, I want to use code coverage to determine which tests within HDFS native test classes are exercising specific functionalities within the HDFS source code (or the class the test class is associated with).
For example if there is a block manager class

public class BlockManager {
    public void allocateBlock(Block block) {
        // Logic for allocating a block

// Hypothetical Test Class

public class TestBlockManager {
    public void testAllocateBlock() {
        // Test logic for allocating a block
        BlockManager blockManager = new BlockManager();
        blockManager.allocateBlock(new Block());

    public void testOtherFunctionality() {

I am trying to see if i can use code coverage to determine which tests in are exercising allocateBlock() of the BlockManager class and potentially get other useful information. I have never used coverage tools before, but Does this make sense or is this even possible ?

Currently i am working with Jacoco, here is my pom.xml

                    <!-- Execution for preparing agent -->
                                <!-- Include all classes in your project -->
                    <!-- Execution for generating coverage report -->
                    <argLine>-Xmx1024m ${jaCoCoArgLine}</argLine>

After much struggles, everything finally works but i don’t get coverage on the test classes. even with the <Include> tags

mvn -Dtest=TestAbandonBlock test // what i execute 

Running org.apache.hadoop.hdfs.TestAbandonBlock
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 34.046 sec - in org.apache.hadoop.hdfs.TestAbandonBlock

Results :

Tests run: 2, Failures: 0, Errors: 0, Skipped: 0

[INFO] --- maven-antrun-plugin:1.7:run (hdfs-test-bats-driver) @ hadoop-hdfs ---
[INFO] Executing tasks

     [exec] ERROR: bats not installed. Skipping bash tests.
     [exec] ERROR: Please install bats as soon as possible.
[INFO] Executed tasks
[INFO] --- jacoco-maven-plugin:0.8.7:report (default-report) @ hadoop-hdfs ---
[INFO] Loading execution data file /home/x/dsys/hadoop3.0/hadoop-3.0.0-src/hadoop-hdfs-project/hadoop-hdfs/target/jacoco.exec
[INFO] Analyzed bundle 'Apache Hadoop HDFS' with 1913 classes
[INFO] ------------------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  43.451 s
[INFO] Finished at: 2024-05-15T18:13:00-07:00
[INFO] ------------------------------------------------------------------------

Sorry, the output is long

I’m able to look up the results in the browser

On Trying to solve this, I read that you use jacoco from CLI (command line) ,and as trial I’m making an attempt with this command (nothing is refined at the moment)

java -javaagent:/home/x/.m2/repository/org/jacoco/org.jacoco.agent/0.8.7/org.jacoco.agent-0.8.7-runtime.jar=destfile=jacoco.exec -cp /home/x/dsys/hadoop3.0/hadoop-3.0.0-src/hadoop-hdfs-project/hadoop-hdfs/target/test-classes:home/x/.m2/repository/junit/junit/4.12/junit-4.12.jar org.junit.runner.JUnitCore org.apache.hadoop.hdfs.TestAbandonBlock

i get this error, the jar dependencies exist (i explicitly specified their path )

Error: Could not find or load main class org.junit.runner.JUnitCore

Will appreciate any help, ideas on fixing this or if there are other strategies to achieving my goal.

Note: I not working specifically with TestAbandonBlock. i am just using it to describe what i am trying to acheive in general.

Thanks in advance !