Refactoring

Our specifications forced us to create the second constructor, since changing the original one would break the existing tests. However, now that everything is green, we can do some refactoring and get rid of the single argument constructor. The specification class already has the beforeTest method that is run before each test. We can move everything, but the assert itself to this method:

public class ShipSpec {
...
  private Planet planet;

@BeforeMethod public void beforeTest() { Point max = new Point(50, 50); location = new Location(new Point(21, 13), Direction.NORTH); planet = new Planet(max); // ship = new Ship(location); ship = new Ship(location, planet); }

public void whenInstantiatedThenPlanetIsStored() { // Point max = new Point(50, 50); // Planet planet = new Planet(max); // ship = new Ship(location, planet); assertEquals(ship.getPlanet(), planet); } }

With this change, we effectively removed the usage of the Ship single argument constructor. By running all specifications, we should confirm that this change worked.

Now, with a single argument constructor that is not in use anymore, we can remove it from the implementation class, as well:

public class Ship {
...
  // public Ship(Location location) {
  //   this.location = location;
  // }
  public Ship(Location location, Planet planet) {
    this.location = location;
    this.planet = planet;
  }
...
}

By using this approach, all specifications were green all the time. Refactoring did not change any existing functionality, nothing got broken, and the whole process was done quickly.

Now, let's move into wrapping itself.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.137.67