Refactoring

We know that we'll need to instantiate Ship for every spec, so we might as well refactor the specification class by adding the @BeforeMethod annotation. The code can be the following:

@Test
public class ShipSpec {

private Ship ship; private Location location; @BeforeMethod public void beforeTest() { Location location = new Location(new Point(21, 13), Direction.NORTH); ship = new Ship(location); } public void whenInstantiatedThenLocationIsSet() { // Location location = new Location(new Point(21, 13), Direction.NORTH); // Ship ship = new Ship(location); assertEquals(ship.getLocation(), location); } }

No new behavior has been introduced. We just moved part of the code to the @BeforeMethod annotation in order to avoid duplication, which would be produced by the rest of the specifications that we are about to write. Now, every time a test is run, the ship object will be instantiated with location as the argument.

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

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