gradle - 如何正确运行 gradle 测试任务?
问题描述
当我从 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.
}
}
例外:
com.mypackage.MyTest > testSimpleIfParsing FAILED
java.lang.NullPointerException at MyTest.java:28
但是如果在MyClass
构造函数中,我插入System.out.println(s)
在两种情况下都通过的所有测试(来自 CLI 和 Intellij Idea)。
谁能帮我怎么回事?我哪里错了?
解决方案
推荐阅读
- graphql - 尝试使用 GraphQL 从 Monday.com 获取特定的行值
- javascript - 具有相同类且没有唯一 id 的多个按钮,如何仅使用 vanilla JS 识别单击了哪个按钮
- python-3.x - Python 3 一次可以写入的最大数据量是多少?
- c++ - 带有类的 switch 语句
- javascript - jQuery追加最后一项
- c++ - 如何移动到智能指针数组中的下一个指针
- google-apps-script - 查询 Google Admin User 目录比较参数
- java - 为什么 double 到 int 的转换在 java 中不能按预期工作?
- angular - 我如何以角度显示这些对象
- json - 如何在 VBA/Json Ms Access 2016 中添加嵌套字典和集合