2

I'm working on a project in college in which I am to create my own objects, along with private data, methods, etc. This isn’t a complete working system with a user interface; it’s just a chance for to create classes, then instantiate and test them.

The issue is that I'm trying to create a test method which tests a thrown IllegalArgumentException in a constructor called DinetteStore:

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

The code in the test class is listed here:

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

    }

I'm running into an issue where I'm getting either a .class expected error or illegal start of expression error. The IDE I'm using is BlueJ 4.1.0 Is there something I'm missing syntax-wise here? Any help would be surely appreciated.

Davis Peixoto
  • 5,695
  • 2
  • 23
  • 35

3 Answers3

3

You are not instantiating the class that will throw the exception. And the syntax you have is not correct, it should be something like:

@Test (expected = IllegalArgumentException.class) 
public void testIllegalArgumentChair() {
        DinetteStore  willFail = new DinetteStore(-1, -1, -1);
}
Eddie Martinez
  • 13,582
  • 13
  • 81
  • 106
2
@Test (expected = IllegalArgumentException.class) 
public void testIllegalArgumentChair() {
    DinetteStore d = new DinetteStore(-1,-1,-1);
}

In the test you have to call the method which will throw the exception, if not you'll never get the expected exception

azro
  • 53,056
  • 7
  • 34
  • 70
2

Your test class is oddly formatted. You're trying to instantiate a DinetteStore with illegal parameters, so you want:

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