我有一个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 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
(与其他2个测试用例一样)吗?
答案 0 :(得分:1)
在 testIncreasePriceWithEmptyListOfProducts() 中,您将捕获所有异常。所以你也捕获UnsupportedOperationException。
在 testIncreasePriceWithNullListOfProducts() 中,您只捕获NullPointerExceptions并抛出UnsupportedOperationException。