0

我可以在这里找到很多关于 arraylist 是否可以等于 null 的问答,这以它自己的方式很有帮助,但是如果 arraylist 中的任何字段为空,我找不到抛出错误的答案。当我将对象添加到数组列表时,如果用户尝试传入任何为空的内容,我想抛出异常。这是代码:

    void addInvoiceItem(Item item, Integer quantity, double discount) throws Exception {    
for (InvoiceItem thing: invoiceList) {
    if (thing == null) {
        throw new IllegalArgumentException("Part of the invoice item is blank (null). Please review your invoice items and ensure you have specified values for the item.");
    }
    else {
        invoiceList.add(thing);
        thing.setItemQuantity(quantity);
        thing.setItemDiscount(discount);
        System.out.println(invoiceList);
    }
}
}

这是项目类:

final class Item {

String itemDescription;
double itemPrice;
Integer itemSKU;

Item (String description, double price, Integer sku) {
    this.itemDescription = description;
    this.itemPrice = price;
    this.itemSKU = sku;
}

}

以下是让我知道我肯定遗漏了一些东西的测试方法。一种是测试有效的 InvoiceItem,另一种是测试无效的 InvoiceItem(包含空值):

public class InvoiceTest {
//create the static values to be used
//for InvoiceItem
String goodDescription = "wheel";
double goodPrice = 500.00;
Integer goodSku = 0002;
Item goodInvoiceItem = new Item(goodDescription, goodPrice, goodSku);

String emptyDescription = null;
double emptyPrice = 0;
Integer emptySku = 0;
Item badInvoiceItem = new Item(emptyDescription, emptyPrice, emptySku);

Integer itemQuantity = 0;
double itemDiscount = 0.05;

@Test
public void invalidItemAddTest() {
    Invoice badInvoice = new Invoice(null);
    try {
        badInvoice.addInvoiceItem(badInvoiceItem, itemQuantity, itemDiscount);
        System.out.println(badInvoice);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

@Test
public void validItemAddTest() {
    Invoice goodInvoice = new Invoice(null);
    try {
        goodInvoice.addInvoiceItem(goodInvoiceItem, itemQuantity, itemDiscount);
        System.out.println(goodInvoice);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

提前感谢您的所有帮助

编辑添加:所以梅尔的回答是我的出发点,我做了一些补充,让它按照我需要的方式工作。我的添加方法现在看起来像这样:

    void addInvoiceItem(Item item, Integer quantity, double discount) {
    if(item == null || quantity == 0 || discount == 0) {
        throw new IllegalArgumentException("Part of the invoice item is blank (null). Please review your invoice items and ensure you have specified values for the item.");
    } else {
    InvoiceItem invoice = new InvoiceItem(item, quantity, discount);
    invoiceList.add(invoice);
}
}

我的测试方法如下所示:

public class InvoiceTest {
//create the static values to be used
//for InvoiceItem
String goodDescription = "wheel";
double goodPrice = 500.00;
int goodSku = 0002;
Item goodInvoiceItem = new Item(goodDescription, goodPrice, goodSku);

String emptyDescription = null;
double emptyPrice = 0;
int emptySku = 0;
Item badInvoiceItem = new Item(emptyDescription, emptyPrice, emptySku);

int badItemQuantity = 0;
double badItemDiscount = 0;
int goodItemQuantity = 1;
double goodItemDiscount = 0.05;


/**
 * @Before - initialize what we need for the test
 * @throws Exception
 */
@Before
public void setUp() {
    //things needed for testInvalidItemAdd()


}

/**
 * @throws Exception 
 * @Test - confirm you cannot add an item that is null
 */
@Test
public void invalidItemAddTest() {
    boolean exceptionThrown = false;
    Invoice badInvoice = new Invoice(null);
    try {
        badInvoice.addInvoiceItem(badInvoiceItem, badItemQuantity, badItemDiscount);
        System.out.println(badInvoice);
    } catch (Exception e) {
        e.printStackTrace();
        exceptionThrown = true;
    }
    assertTrue(exceptionThrown);
}

@Test
public void validItemAddTest() {
    boolean exceptionThrown = false;
    Invoice goodInvoice = new Invoice(null);
    try {
        goodInvoice.addInvoiceItem(goodInvoiceItem, goodItemQuantity, goodItemDiscount);
        System.out.println(goodInvoice);
    } catch (Exception e) {
        e.printStackTrace();
        exceptionThrown = true;
    }
    assertFalse(exceptionThrown);
}
4

1 回答 1

1

您的添加例程有点偏离。它尝试使用列表中已经存在的项目,而不是创建一个新项目。尝试这个:

void addInvoiceItem(Item item, Integer quantity, double discount) {
    if(
        item == null || 
        quantity == null || 
        item.sku == null ||
        item.description == null
    ) {
        throw new NullPointerException();
    }
    InvoiceItem invoice = new InvoiceItem(item, quantity, discount);
    invoiceList.add(invoice);
}

还可以查看 google 库中的 checkNotNull() 以减少输入。

在 InvoiceItem 构造函数中而不是在加法器中检查 null 可能很有用,除非您想在其他地方允许 null。

于 2012-12-11T02:03:28.820 回答