Expected Before Actual Value

 class​ CruiseControlTest {
 
  @Test
 void​ setPlanetarySpeedIs7667() {
  CruiseControl cruiseControl = ​new​ CruiseControl();
 
  cruiseControl.setPreset(SpeedPreset.PLANETARY_SPEED);
 
» Assertions.assertEquals(cruiseControl.getTargetSpeedKmh(), 7667);
  }
 }

The test above looks correct, doesn’t it? You can see its structuring into different parts, it uses assertEquals() to compare the results, and it has a meaningful name. You need to look closely to find a problem in the code.

It gets more obvious when you run the test and the assertion fails. If that happens, you’ll get a message like this one:

expected: <1337> but was <7667>

The message tells you that the two values in the assertEquals() method are different. ...

Get Java By Comparison now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.