gpt4 book ai didi

java - 了解 Java/Spring Web 应用程序中的 junit.framework.AssertionFailedError

转载 作者:行者123 更新时间:2023-12-01 11:15:31 25 4
gpt4 key购买 nike

场景:

我有一个 ProductManager 界面:

public interface ProductManager extends Serializable{
public void increasePrice(int percentage);
public List<Product> getProducts();
}

还有一个实现类SimpleProductManager

public class SimpleProductManager implements ProductManager {
private List<Product> products;

public void increasePrice(int percentage) {
throw new UnsupportedOperationException();
}
public List<Product> getProducts() {
return products;
}
public void setProducts(List<Product> products) {
this.products = products;
}
}

我正在使用Spring official docs并试图理解以下是对increasePrice()方法的3个测试:

public void testIncreasePriceWithNullListOfProducts() {
try {
productManager = new SimpleProductManager();
productManager.increasePrice(POSITIVE_PRICE_INCREASE);
}
catch(NullPointerException ex) {
fail("Products list is null.");
}
}

public void testIncreasePriceWithEmptyListOfProducts() {
try {
productManager = new SimpleProductManager();
productManager.setProducts(new ArrayList<Product>());
productManager.increasePrice(POSITIVE_PRICE_INCREASE);
}
catch(Exception ex) {
fail("Products list is empty.");
}
}

public void testIncreasePriceWithPositivePercentage() {
productManager.increasePrice(POSITIVE_PRICE_INCREASE);
double expectedChairPriceWithIncrease = 22.55;
double expectedTablePriceWithIncrease = 165.11;

List<Product> products = productManager.getProducts();
Product product = products.get(0);
assertEquals(expectedChairPriceWithIncrease, product.getPrice());

product = products.get(1);
assertEquals(expectedTablePriceWithIncrease, product.getPrice());
}

问题:

为什么当我对这 3 种方法运行 ant 测试时,测试用例 testIncreasePriceWithEmptyListOfProducts 失败并显示 junit.framework.AssertionFailedError: Products list is空

[junit] Testcase: testIncreasePriceWithNullListOfProducts(springapp.service.SimpleProductManagerTests): Caused an ERROR
[junit] null
[junit] java.lang.UnsupportedOperationException
[junit] at springapp.service.SimpleProductManager.increasePrice(SimpleProductManager.java:12)
[junit] at springapp.service.SimpleProductManagerTests.testIncreasePriceWithNullListOfProducts(SimpleProductManagerTests.java:67)
[junit]
[junit]
[junit] Testcase: testIncreasePriceWithEmptyListOfProducts(springapp.service.SimpleProductManagerTests): FAILED
[junit] Products list is empty.
[junit] junit.framework.AssertionFailedError: Products list is empty.
[junit] at springapp.service.SimpleProductManagerTests.testIncreasePriceWithEmptyListOfProducts(SimpleProductManagerTests.java:81)
[junit]
[junit]
[junit] Testcase: testIncreasePriceWithPositivePercentage(springapp.service.SimpleProductManagerTests): Caused an ERROR
[junit] null
[junit] java.lang.UnsupportedOperationException
[junit] at springapp.service.SimpleProductManager.increasePrice(SimpleProductManager.java:12)
[junit] at springapp.service.SimpleProductManagerTests.testIncreasePriceWithPositivePercentage(SimpleProductManagerTests.java:86)

由于 increasePrice 方法尚未实现,它不应该抛出 java.lang.UnsupportedOperationException(像其他 2 个测试用例一样)吗?

最佳答案

testIncreasePriceWithEmptyListOfProducts() 中,您捕获所有异常。因此您还捕获了 UnsupportedOperationException。

而在 testIncreasePriceWithNullListOfProducts() 中,您只能捕获 NullPointerExceptions 并抛出 UnsupportedOperationException。

关于java - 了解 Java/Spring Web 应用程序中的 junit.framework.AssertionFailedError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31878414/

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