It’s possible, however unlikely, that getTotalTravelTime()could alter the values of the
start and end objects. If it did manage to alter the values, the test might appear to succeed
even though it would not return the value you expected when you wrote the test method.
In other words, you would get a false positive.
Listing 10-2.testGetTotalCost Method
public void testGetTotalTravelTimeOneLeg() throws Exception {
Date start = sdf.parse("2005-01-01 06:00");
Date end = sdf.parse("2005-01-01 12:00");
List<FlightLeg> legs = new ArrayList<FlightLeg>();
legs.add(new FlightLeg(fooCity, start, barCity, end));
flight = new Flight(legs, new BigDecimal(40));
assertEquals((6*60*60*1000), flight.getTotalTravelTime());
}
To put it all together, Listing 10-3 contains the entire FlightTest.
Listing 10-3.Unit Test for Flight Class
public class FlightTest extends TestCase {
private Flight flight;
private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
private Airport fooCity;
private Airport barCity;
public void setUp() throws Exception {
super.setUp();
fooCity = new Airport("foo", "F");
barCity = new Airport("bar", "B");
List<FlightLeg> legs = createSingleLeg();
flight = new Flight(legs, new BigDecimal(40));
}
public void testGetTotalCost() {
assertEquals(40, flight.getTotalCost());
}
public void testGetDepartFrom() {
assertEquals(fooCity, flight.getDepartFrom());
}
public void testGetArriveAt() {
assertEquals(barCity, flight.getArrivalAt());
}
286 CHAPTER 10 ■TESTING SPRING MVC APPLICATIONS