Exercise Solutions: Unit Testing

testGasTankAfterDriving()

Add a test for the third TODO, “gasTankLevel is accurate after driving within tank range”.

After following the steps, your test should look like the following:

@Test
public void testGasTankAfterDriving() {
   test_car.drive(50);
   assertEquals(9, test_car.getGasTankLevel(),.001);
}

Back to the exercises

testGasTankAfterExceedingTankRange()

Add a test for the fourth TODO, “gasTankLevel is accurate after attempting to drive past tank range”.

@Test
public void testGasTankAfterExceedingTankRange() {
   test_car.drive(501);
   assertEquals(test_car.getGasTankLevel(), 0, .001);
}

Back to the exercises

testGasOverfillException()

The test for our last TODO is a little different. We are going to perform an action on our car object, and we are expecting the object to throw an error. In this case, we are going to attempt to add gas to our car that exceeds the gas tank size.

After following the steps, your test should look like the following:

@Test(expected = IllegalArgumentException.class)
public void testGasOverfillException() {
   test_car.addGas(5);
   fail("Shouldn't get here, car cannot have more gas in tank than the size of the tank");
}

Back to the exercises