Home » Java » java – gradlew build failed for the task test with clover plugin-Exceptionshub

java – gradlew build failed for the task test with clover plugin-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

This is a gradle project and integrate with clover plugin for the test coverage. We are using Mockito for unit test cases.
gradle version

------------------------------------------------------------
Gradle 4.10.2
------------------------------------------------------------

Build time:   2018-09-19 18:10:15 UTC
Revision:     b4d8d5d170bb4ba516e88d7fe5647e2323d791dd

Kotlin DSL:   1.0-rc-6
Kotlin:       1.2.61
Groovy:       2.4.15
Ant:          Apache Ant(TM) version 1.9.11 compiled on March 23 2018
JVM:          1.8.0_241 (Oracle Corporation 25.241-b07)
OS:           Linux 4.14.35-1902.10.4.1.el7uek.x86_64 amd64

build.gradle

buildscript {
    dependencies {
        classpath 'com.bmuschko:gradle-clover-plugin:2.2.3'
    }
//....
}
apply from: "${rootDir}/build-support/coverage.gradle"

coverage.gradle

apply plugin: 'com.bmuschko.clover'

dependencies {
    clover 'org.openclover:clover:4.3.1'
}
clover {
    testIncludes = ['**/*Test.java']


    report {
        html = true
        xml = true
    }
}

The issue is whenever if I run ./gradlew clean build without including coverage.gradle , it passes all the test cases and builds the project.
however, if I include the coverage.gradle it is failing for some of the test cases.
After I dbugged a bit deeper, found out that Mockito has class DefaultStubbingLookupListener
and throwing NPE at the below code:

private static List<Invocation> potentialArgMismatches(Invocation invocation, Collection<Stubbing> stubbings) {
        List<Invocation> matchingStubbings = new LinkedList<Invocation>();
        for (Stubbing s : stubbings) {
            if (UnusedStubbingReporting.shouldBeReported(s)
                && s.getInvocation().getMethod().getName().equals(invocation.getMethod().getName())
                //If stubbing and invocation are in the same source file we assume they are in the test code,
                // and we don't flag it as mismatch:
                && !s.getInvocation().getLocation().getSourceFile().equals(invocation.getLocation().getSourceFile())) {
                    matchingStubbings.add(s.getInvocation());
            }
        }
        return matchingStubbings;
    }

on the above code s.getInvocation().getLocation().getSourceFile() is coming null and so it throws the NPE on equality.
While without using clover (or excluding coverage.gradle)
this gives me proper source file information, e.g. s.getInvocation().getLocation().getSourceFile() = SomeControllerTest.java

If I remove failing test cases, then with the above gradle and Clover configuration I’m getting the colver.db files and even the coverage report is also generated.
But those test cases cannot be removed as without clover it runs smoothly. Further I followed couple of documentations on configuring sourcesets, but its the same result.

I cannot provide the complete test cases code because of the obvious reasons, however, below is the snippet if it helps to look further:

@Test
public void demoTestCaseCodeSnippet() {
ArrayList<String> result = getSomeArrayList();
String s1 = "text1";
String s2 = "text2";


given(commandHelper.getObjectList(any(HashMap.class), eq(String.class), eq("payloadName")))
                .willReturn(result);
given(commandHelper.getObject(any(HashMap.class), eq(String.class), eq("payloadName")))
                .willReturn(s1);
given(commandHelper.getObject(any(HashMap.class), eq(String.class), eq("payloadName")))
                .willReturn(s2); //-----(3)
verify(//some code)

}

I know the above test case is not much of help, but as I stated earlier, that Mockito is giving me NPE because sourceFile is null, I think it is true for all the test cases with clover plugin, some test cases which are passed just because , it did not run those Mockito code snippet above. The results are same for gradle versions 4.10.1 also.
Looking for help/suggestions.

How to&Answers: