gpt4 book ai didi

java - Gradle "test"任务没有在应该失败的地方失败

转载 作者:太空宇宙 更新时间:2023-11-04 07:09:07 24 4
gpt4 key购买 nike

您好,我是 Gradle 新手。我们刚刚从 Maven 切换到 Gradle,我在理解 Gradle 测试任务方面遇到了一些困难。这是我的类(class):

public class Money   {
private final int amount;
private final String currency;

public Money(int amount, String currency) {
if (amount < 0) {
throw new IllegalArgumentException("illegal amount: [" + amount + "]");
}
if (currency == null || currency.isEmpty()) {
throw new IllegalArgumentException("illegal currency: [" + currency + "]");
}
//this.amount = 15;
this.amount = amount;
this.currency = currency;
}

public int getAmount() {
return amount;
}

public String getCurrency() {
return currency;
}
}

这是我的测试(使用 TestNG):

@Test
public class MoneyManyValuesTest {
public void testConstructor() {
Money money = new Money(10, "USD");
assertEquals(money.getAmount(), 10);
assertEquals(money.getCurrency(), "USD");
money = new Money(20, "EUR");
assertEquals(money.getAmount(), 20);
assertEquals(money.getCurrency(), "EUR");
}
}

当我运行测试(使用 gradle“test”任务)时,一切看起来都正常。

但是我希望我的测试失败,所以我像这样调整测试的最后一行:

assertEquals(money.getCurrency(), "EUR want error here");

再次运行 gradle“test”,测试仍然通过,并显示 msg:

2:40:26 PM: Executing external task 'test'...
money:compileJava UP-TO-DATE
money:processResources UP-TO-DATE
money:classes UP-TO-DATE
money:compileTestJava
money:processTestResources UP-TO-DATE
money:testClasses
money:test

BUILD SUCCESSFUL

这是怎么回事?

这是我的构建脚本

apply plugin: 'idea'
apply plugin: 'eclipse'

apply plugin: 'java';
apply plugin: 'eclipse'

repositories {
mavenCentral()
}

dependencies {
testCompile 'org.testng:testng:6.3.1'
testCompile 'org.mockito:mockito-all:1.9.0'
testCompile 'org.easytesting:fest-assert:1.4'
testCompile 'org.hamcrest:hamcrest-all:1.1'
}

test {
useTestNG()
}
}

最佳答案

我正在使用 IntelliJ IDEA。我的 IDE 会自动保存我的源代码。我已经习惯了这个功能,所以我什至忘记了 IDEA 为我做了这个。看起来每当我运行 Gradle“测试”任务时,IDEA 都不会自动保存我的源代码。但如果我手动保存它一切正常。

关于java - Gradle "test"任务没有在应该失败的地方失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20904063/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com