We will start with testing the InvoiceItem
class.
Add a field that we will test against:
private InvoiceItem item;
Initialise the field in the setUp
method:
item = new InvoiceItem(); item.setProductName("Polkadot Widget"); item.setSalePrice(3.45); item.setQuantityPurchased(2.0);
The setUp
code is a little more complicated since we need to provide values for the initial state of the object.
Add test code to the testGetItemTotal
method:
Double result = item.getItemTotal(); assertThat(result, is(6.90));
Fix the imports using <
Fix Imports">>.Run the test. It should pass.
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.
Fix the method again so that the test passes.