了解Java/Spring web应用程序中的junit.framework.AssertionFailedError



场景:

我有一个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官方文档,并试图理解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 tests时,测试用例testIncreasePriceWithEmptyListOfProducts会以junit.framework.AssertionFailedError: Products list is empty失败。

[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(像其他两个测试用例一样(吗?

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

testIncreasePriceWithNullListOfProducts((中,您只捕获NullPointerException,并抛出UnsupportedOperationException。

相关内容

  • 没有找到相关文章

最新更新