2017-07-20 6 views
1

私は自分のオブジェクトを個人的なデータやメソッドなどとともに作成する大学のプロジェクトに取り組んでいます。これはユーザーインターフェイスを備えた完全な作業システムではありません;クラスを作成し、インスタンス化してテストするだけのチャンスです。テストクラスのコードは以下のとおりですスローされたIllegalArgumentExceptionをテストする方法

public DinetteStore(int tableInventory, int chairInventory, int leafInventory){ 
     if (tableInventory < 0 || leafInventory < 0 || chairInventory < 0){ 
      throw new IllegalArgumentException ("Inventory must not be out of range of required order"); 
     } 

     this.tableInventory = tableInventory; 
     this.chairInventory = chairInventory; 
     this.leafInventory = leafInventory; 
     this.totalSales = 0; 
     this.numSales = 0; 
    } 

@Test (expected = IllegalArgumentException.class) 
    public void testIllegalArgumentChair() { 
     int tableInventory = -1 || int leafInventory = -1 || chairInventory = -1; 

    } 

問題は、私は、コンストラクタと呼ばれるDinetteStoreに投げ、IllegalArgumentExceptionを試験する試験方法を作成しようとしているということです

私は、.classの予想されるエラーまたは不正な式の開始エラーが発生している問題に遭遇しています。私が使用しているIDEはBlueJ 4.1.0です。ここでは構文上の欠点がありますか?どんな援助も確実に高く評価されます。あなたは

答えて

2
@Test (expected = IllegalArgumentException.class) 
public void testIllegalArgumentChair() { 
    DinetteStore d = new DinetteStore(-1,-1,-1); 
} 

。あなたは違法なパラメータを持つDinetteStoreをインスタンス化しようとしているので、あなたがしたい:あなたはexceptionをスローしますclassをインスタンス化されていません

@Test (expected = IllegalArgumentException.class) 
public void testIllegalArgumentChair() { 
    int tableInventory = -1; 
    int leafInventory = -1; 
    int chairInventory = -1; 
    DinetteStore creationWillFail = new DinetteStore(tableInventory, 
                leafInventory, 
                chairInventory); 
} 
2

あなたのテストクラスが妙にフォーマットされ、予想される例外を得ることは決してないだろうでない場合は、例外をスローするメソッドを呼び出す必要があり、テストで

3

。あなたが持っている構文が正しくない場合は、次のようになります。

@Test (expected = IllegalArgumentException.class) 
public void testIllegalArgumentChair() { 
     DinetteStore willFail = new DinetteStore(-1, -1, -1); 
} 
関連する問題