O'Reilly logo

Java 11 Cookbook by Mohamed Sanaulla, Nick Samoylov

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required