How to do it...

Let's look at the Vehicle class closer. Testing the getters would be of little value, but we can still do it, making sure that the value passed to the constructor is returned by the corresponding getter. The exception in the constructor belongs to the must-test features as well as the getSpeedMph() method. There is also an object of the Engine class that has the getHorsePower() method. Can it return null? To answer this question, let's look at the Engine class: 

public class Engine {  private int horsePower;  public int getHorsePower() {    return horsePower;  }  public void setHorsePower(int horsePower) {    this.horsePower = horsePower;  }}

The getHorsePower() method cannot return null. The horsePower field will be initiated to the ...

Get Java 11 Cookbook now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.