Software Craftsmanship
Test Driven Development
November 8, 2016
Requirements must be completely understood through use cases and user stories.
Test structure
Avoid
@Before
public void setUp() {
bill = new Bill(2, new Server("gtb012", "Reid"));
calculator = new BillCalculator(.05);
}
@After
public void tearDown() {
bill = null;
calculator = null;
}
@Test
public void testCalculateTax() {
bill.addItem(new Item("milk", 1.23));
bill.addItem(new Item("cereal", 2.13));
double tax = calculator.calculateTax(bill);
assertEquals(0.17, tax);
}Mock objects can simulate the behavior of complex, real objects when the real object is impractical or impossible to incorporate in a unit test.
Creating a mock object will stub the method implementations.
Usage of a mock object by the unit under test (UUT) allows control and verification of the invocation of the mock by the UUT.