O'Reilly logo

Java 9 Cookbook by Nick Samoylov, Mohamed Sanaulla

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? We should look in the Engine class too: 

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

There is not much behavior in this class to test and it cannot return null. But returning a negative value is ...

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