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 {
    @Test
    public void testAllocateBlock() {
        // Test logic for allocating a block
        BlockManager blockManager = new BlockManager();
        blockManager.allocateBlock(new Block());
    }

    @Test
    public void testOtherFunctionality() {
    }
}

I am trying to see if i can use code coverage to determine which tests in TestBlockManager.java 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

<plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.8.7</version>
                <executions>
                    <!-- Execution for preparing agent -->
                    <execution>
                        <id>default-prepare-agent</id>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                        <configuration>
                            <propertyName>jaCoCoArgLine</propertyName>
                            <includes>
                                <!-- Include all classes in your project -->
                                <include>**/home/x/dsys/hadoop3.0/hadoop-3.0.0-src/hadoop-hdfs-project/hadoop-hdfs/target/test-classes/**/*.class</include>
                            </includes>
                        </configuration>
                    </execution>
                    <!-- Execution for generating coverage report -->
                    <execution>
                        <id>default-report</id>
                        <phase>test</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <systemPropertyVariables>
                        <startKdc>${startKdc}</startKdc>
                        <kdc.resource.dir>${kdc.resource.dir}</kdc.resource.dir>
                        <runningWithNative>${runningWithNative}</runningWithNative>
                    </systemPropertyVariables>
                    <properties>
                        <property>
                            <name>listener</name>
                            <value>org.apache.hadoop.test.TimedOutTestsListener</value>
                        </property>
                    </properties>
                    <argLine>-Xmx1024m ${jaCoCoArgLine}</argLine>
                    <testFailureIgnore>true</testFailureIgnore>
                </configuration>
            </plugin>

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 


T E S T S
-------------------------------------------------------
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] 
[INFO] --- maven-antrun-plugin:1.7:run (hdfs-test-bats-driver) @ hadoop-hdfs ---
[INFO] Executing tasks

main:
     [exec] 
     [exec] 
     [exec] ERROR: bats not installed. Skipping bash tests.
     [exec] ERROR: Please install bats as soon as possible.
     [exec] 
     [exec] 
[INFO] Executed tasks
[INFO] 
[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] BUILD SUCCESS
[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 !

LEAVE A COMMENT