gradle - 如何正确运行gradle测试任务?

标签 gradle intellij-idea

当我从Intellij Idea运行gradle测试时,我尝试使用gradle构建项目,一切都很好-所有测试都通过了,但是当我使用gradle test命令在命令行中执行相同操作时,某些单元测试失败。那是我的build.gradle文件:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:2.0.5.RELEASE")
    }
}

apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

bootJar {
    baseName = 'myProjectName'
}

repositories {
    mavenCentral()
    maven {
        url "my_repo_url"
    }
    maven {
        url "my_repo_url"
        credentials {
            username = System.getenv("MVN_USER")
            password = System.getenv("MVN_PASS")
        }
    }
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

dependencies {

    compile ('org.apache.poi:poi-ooxml:4.0.1')
    compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.7'

    compile ('org.springframework.boot:spring-boot-starter:2.0.5.RELEASE') {
        exclude module : 'spring-boot-starter-logging'
    }

    compile group: 'org.apache.poi', name: 'poi-ooxml-schemas', version: '4.0.1'

    testCompile('org.springframework.boot:spring-boot-starter-test:2.0.1.RELEASE')
    testCompile('com.jayway.jsonpath:json-path')
    testCompile group: 'commons-io', name: 'commons-io', version: '2.6'
    testCompile group: 'pl.pragmatists', name: 'JUnitParams', version: '1.1.1'

    compile group: 'joda-time', name: 'joda-time', version: '2.9.4'

}


还有我的junit测试类:
public class MyTest {

    @Test
    public void testSimpleIfParsing() throws IOException {
        String complicatedStructure = "testString";
        IfTemplateElement MyClass = new MyClass(
                complicatedStructure // This variable NOT null, but I have NullPointerException
        );
        Object response = MyClass.evaluate();
        Assert.assertNotNull( response );
    }
}

MyClass构造函数:
public class MyClass {
public MyClass( String s ) {

        s = s.substring( 0, 5 );    // there I have NullPointerException.

    }
}

异常(exception):
com.mypackage.MyTest > testSimpleIfParsing FAILED
    java.lang.NullPointerException at MyTest.java:28


但是如果在构造函数的MyClass中,我插入System.out.println(s),则在两种情况下都通过了所有测试(从CLI和Intellij Idea)。

谁能帮我忙什么?
我在哪里弄错了?

最佳答案

听起来很奇怪。

我建议您尝试使用intelij调试UT,然后再尝试使用gradle。

如何在inteliJ中更改单元测试运行器?

转到设置->构建,执行,部署>构建工具> Gradle> Runner

enter image description here

关于gradle - 如何正确运行gradle测试任务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57551878/

相关文章:

java - IntelliJ IDEA : Jump to interface implementation if there is only one implementation available

java - GOOGLE PLAY 服务异常 : com. google.android.gms.common.GooglePlayServicesUtil

plugins - 在gradle插件中,如何用人工插件 'uploadArchives'替换maven插件 'artifactoryPublish'?

java - Maven 需要额外的依赖来构建项目,而 Gradle 不需要

gradle - 在gradle脚本中调用java函数

maven - 如何使用Gradle将依赖 Artifact 上传到Maven存储库(NEXUS)?

intellij-idea - 如何在 intellij 中更快地纠正拼写错误?

javascript - WebStorm,使用 Node Supervisor(所以每次代码更改后不必重新启动)?

spring-boot - 如何在IntelliJ IDEA社区中使Spring Boot的自动重启工作? (出/建目录问题)

scala - playframework2 和 Scala - 如何从 IDEA 启动测试?