UnsupportedOperationException

UnsupportedOperationException

场景:

我有一个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并试图了解
下面的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尚未实现,它是否应该抛出increasePrice(与其他两个测试用例一样)?

最佳答案

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

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

07-26 09:29