当前位置: 代码迷 >> java >> 了解Java / Spring Web应用程序中的junit.framework.AssertionFailedError
  详细解决方案

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

热度:51   发布时间:2023-07-25 20:01:54.0

场景:

我有一个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;
    }
}

我使用的是并尝试了解针对递增价格()方法的以下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)

难道不应该抛出java.lang.UnsupportedOperationException (像其他2测试用例),因为该方法increasePrice尚未执行?

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

testIncreasePriceWithNullListOfProducts()中时,您仅捕获NullPointerExceptions,并且抛出UnsupportedOperationException。

  相关解决方案