I was reading an article by Martin Fowler (Mocks Aren't Stubs) and there's this Java test at the beginning:
public class OrderStateTester extends TestCase {
private static String TALISKER = "Talisker";
private static String HIGHLAND_PARK = "Highland Park";
private Warehouse warehouse = new WarehouseImpl();
protected void setUp() throws Exception {
warehouse.add(TALISKER, 50);
warehouse.add(HIGHLAND_PARK, 25);
}
public void testOrderIsFilledIfEnoughInWarehouse() {
Order order = new Order(TALISKER, 50);
order.fill(warehouse);
assertTrue(order.isFilled());
assertEquals(0, warehouse.getInventory(TALISKER));
}
public void testOrderDoesNotRemoveIfNotEnough() {
Order order = new Order(TALISKER, 51);
order.fill(warehouse);
assertFalse(order.isFilled());
assertEquals(50, warehouse.getInventory(TALISKER));
}
My understanding of this code is that a WarehouseImpl
is created at the beginning (setUp()
) and used in two tests, both of which manipulate the WarehouseImpl
object. So the tests are affecting each other and the order in which they are run could potentially affect the outcomes.
Am I missing something here?