labs / tiddlers / content / labs / lab02 / _Labs_02_Testing

We will start with testing the InvoiceItem class.

  1. Add a field that we will test against:

    private InvoiceItem item;
  2. Initialise the field in the setUp method:

    item = new InvoiceItem();
    item.setProductName("Polkadot Widget");

    The setUp code is a little more complicated since we need to provide values for the initial state of the object.

  3. Add test code to the testGetItemTotal method:

    Double result = item.getItemTotal();
    assertThat(result, is(6.90));
  4. Fix the imports using <

    Fix Imports">>.

  5. Run the test. It should pass.

  6. Validate the test by breaking the getItemTotal method in the InvoiceItem class and run the test again. You can change the * operator to something else. The test should now fail.

  7. Fix the method again so that the test passes.