Java gradlew生成使用clover插件的任务测试失败

Java gradlew生成使用clover插件的任务测试失败,java,junit,mockito,gradlew,clover,Java,Junit,Mockito,Gradlew,Clover,这是一个gradle项目,与clover插件集成以实现测试覆盖率。我们将Mockito用于单元测试用例。 渐变版本 ------------------------------------------------------------ Gradle 4.10.2 ------------------------------------------------------------ Build time: 2018-09-19 18:10:15 UTC Revision: b4d

这是一个gradle项目,与clover插件集成以实现测试覆盖率。我们将Mockito用于单元测试用例。 渐变版本

------------------------------------------------------------
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
格雷德尔先生

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

apply plugin: 'com.bmuschko.clover'

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


    report {
        html = true
        xml = true
    }
}
问题是,只要我运行./gradlew clean build而不包括coverage.gradle,它就会通过所有测试用例并构建项目。 但是,如果我包含coverage.gradle则某些测试用例失败。 在我深入研究之后,发现Mockito有类DefaultStubbingLookupListener 并将NPE掷向以下代码:

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;
    }
私有静态列表潜在arg不匹配(调用、集合存根){
List matchingStubbings=新建LinkedList();
用于(存根:存根){
如果(未使用的存根报告。应报告)
&&s.getInvocation().getMethod().getName().equals(invocation.getMethod().getName())
//如果存根和调用在同一个源文件中,我们假设它们在测试代码中,
//我们不会将其标记为不匹配:
&&!s.getInvocation().getLocation().getSourceFile().equals(invocation.getLocation().getSourceFile())){
matchingStubbings.add(s.getInvocation());
}
}
返回匹配管道;
}
在上面的代码中,s.getInvocation().getLocation().getSourceFile()将为null,因此它将NPE抛出为相等。 而不使用三叶草(或不包括覆盖率.gradle) 这为我提供了正确的源文件信息,例如s.getInvocation().getLocation().getSourceFile()=SomeControllerTest.java

如果我删除了失败的测试用例,那么通过上面的gradle和Clover配置,我将获得colver.db文件,甚至还会生成覆盖率报告。 但这些测试用例无法删除,因为如果没有clover,它将运行平稳。此外,我还关注了关于配置源代码集的几个文档,但结果是一样的

由于显而易见的原因,我无法提供完整的测试用例代码,但是,下面的代码片段有助于进一步了解:

@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)

}
@测试
public void demoestCaseCodeSnippet(){
ArrayList结果=getSomeArrayList();
字符串s1=“text1”;
字符串s2=“text2”;
给定(commandHelper.getObjectList(any(HashMap.class)、eq(String.class)、eq(“payloadName”))
.willReturn(结果);
给定(commandHelper.getObject(any(HashMap.class)、eq(String.class)、eq(“payloadName”))
.willReturn(s1);
给定(commandHelper.getObject(any(HashMap.class)、eq(String.class)、eq(“payloadName”))
.willReturn(s2);//-----(3)
验证(//一些代码)
}
我知道上面的测试用例没有太大的帮助,但正如我之前所说的,Mockito给我NPE是因为sourceFile是空的,我认为这对所有使用clover插件的测试用例都是正确的,一些测试用例通过只是因为它没有运行上面的Mockito代码片段。gradle版本4.10.1的结果也相同。 寻求帮助/建议