簡體   English   中英

如何正確運行gradle測試任務?

[英]How to correctly run gradle test task?

當我從Intellij Idea運行gradle測試時,我嘗試使用gradle構建項目,一切都很好-所有測試都通過了,但是當我使用command 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.

    }
}

例外:

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

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM