0

配列リストがnullに等しくなるかどうかについては、ここで多くのQ&Aを見つけることができます。これは、それ自体が役に立ちましたが、配列リストのいずれかのフィールドがnullの場合にエラーをスローするための答えを見つけることができません。配列リストにオブジェクトを追加しているので、ユーザーがnullであるものを渡そうとすると、例外をスローしたいと思います。コードは次のとおりです。

    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);
    }
}
}

Itemクラスは次のとおりです。

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;
}

}

これが私が間違いなく何かを省略していることを私に知らせているテスト方法です。1つは有効なInvoiceItemをテストし、もう1つは無効なInvoiceItem(nullを含む)をテストすることです。

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();
    }
}

よろしくお願いします

追加で編集:Melの答えが私の出発点であり、必要な方法で機能させるためにいくつかの追加を行いました。私の追加メソッドは次のようになります。

    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);
}

また、グーグルライブラリのcheckNotNull()を見て、入力を少し減らしてください。

他の場所でnullを許可する場合を除いて、加算器ではなくInvoiceItemコンストラクターでnullをチェックすると便利な場合があります。

于 2012-12-11T02:03:28.820 に答える