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