Eager Test

Definition:

  • A test case that checks or uses more than one method of the class under test. Since its introduction, this smell has been somewhat broadly defined. It is left to interpretation which method calls count towards the maximum. Either all methods invoked on the class under test could count, or only the methods invoked on the same instance under test, or only the methods of which the return value is eventually used within an assertion.

Also Known As:

  • The Test It All, Split Personality, Many Assertions, Multiple Assertions, The Free Ride, The One, Piggyback, Silver Bullet

Code Example:

public void testFlightMileage_asKm2() throws Exception {
     // setup fixture
     // exercise contructor
     Flight newFlight = new Flight(validFlightNumber);
     // verify constructed object
     assertEquals(validFlightNumber, newFlight.number);
     assertEquals("", newFlight.airlineCode);
     assertNull(newFlight.airline);
     // setup mileage
     newFlight.setMileage(1122);
     // exercise mileage translater
     int actualKilometres = newFlight.getMileageAsKm();
     // verify results
     int expectedKilometres = 1810;
     assertEquals( expectedKilometres, actualKilometres);
     // now try it with a canceled flight:
     newFlight.cancel();
     try {
         newFlight.getMileageAsKm();
         fail("Expected exception");
     } catch (InvalidRequestException e) {
         assertEquals( "Cannot get cancelled flight mileage", e.getMessage());
     }
 }

References:

Quality attributes

  • - Code Example

  • - Cause and Effect

  • - Frequency

  • - Refactoring