java - pitest 无法定位 junit 测试

标签 java gradle junit pitest

我的 gradle pitest 无法给我正确的结果。看起来它无法找到我的测试文件。

我有以下 build.gradle 文件:

apply plugin: "java" apply plugin: "maven" apply plugin: "info.solidsoft.pitest"

group = "myorg" version = 1.0

repositories {
    mavenCentral() }

sourceSets.all { set ->
    def jarTask = task("${set.name}Jar", type: Jar) {
        baseName = baseName + "-$set.name"
        from set.output
    }

    artifacts {
        archives jarTask
    } }

sourceSets {
    api
    impl    main{       java {          srcDir 'src/api/java'           srcDir 'src/impl/java'      }   }   test {      java {          srcDir 'src/test/java'      }   } }

buildscript {
    repositories {
        mavenCentral()
        //Needed only for SNAPSHOT versions
        //maven { url "http://oss.sonatype.org/content/repositories/snapshots/" }
    }
    dependencies {
        classpath 'info.solidsoft.gradle.pitest:gradle-pitest-plugin:1.1.6'
    } }

dependencies {
    apiCompile 'commons-codec:commons-codec:1.5'

    implCompile sourceSets.api.output
    implCompile 'commons-lang:commons-lang:2.6'

    testCompile 'junit:junit:4.9'
    testCompile sourceSets.api.output
    testCompile sourceSets.impl.output
    runtime configurations.apiRuntime
    runtime configurations.implRuntime }

jar {
    from sourceSets.api.output
    from sourceSets.impl.output }

pitest { println sourceSets.main

    targetClasses = ['doubler.*']       targetTests  = ['doubler.*']    verbose="on" }

输出存储在正确的文件夹中。当我运行 gradle test 时,它也运行良好。

最佳答案

pitest 用户组中提供了有关此问题的一些其他信息。

https://groups.google.com/forum/#!topic/pitusers/8C7BHh-Vb6Y

正在运行的测试看起来像这样。

@Test
public void testIt2() {
    assert new DoublerImpl().testIt(1) == 2;
}

Pitest 正确地报告这些测试提供了 0% 的类覆盖率。没有覆盖,因为使用了 assert 关键字。

除非在运行测试的 JVM 中设置了 -ea 标志,否则断言将被禁用。编译器生成的这段代码周围基本上隐藏了 if block

@Test
public void testIt2() {
    if (assertionsEnabled) {
      assert new DoublerImpl().testIt(1) == 2;
    }
}

由于未启用断言,因此不会执行任何代码。

要解决此问题,请改用内置的 JUnit 断言。

http://junit.sourceforge.net/javadoc/org/junit/Assert.html

关于java - pitest 无法定位 junit 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34948524/

相关文章:

java - 具有多个接受答案的 JUnit 测试

java - 如何在 Java 中测试 Restful Web 服务

unit-testing - 使用 influxdb 的程序的单元测试

在 ubuntu 中下载 gradle 插件时出现 Java 安全问题

java - 为什么我的应用程序名称与 Activity 名称相同?

java - 在java中模拟一个不是参数的方法中的对象,类似于下面的python代码?

Java - 类存在两次(在类路径和应用程序 jar 上)。链接错误: ClassCastException

android - 错误 : Program type already present: android. support.v4.app.FragmentTransitionCompat21$1

java - 意外输出 java.lang.NoSuchMethodError : while building project

java - Apache CXF 方法中使用的时间单位