-2

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?

lfk
  • 2,423
  • 6
  • 29
  • 46

1 Answers1

3

No. each test is independent with others. since the junit will creates a brand-new instance for each @Test method. so they don't affect the outcomes in each test unless the test access a static field.

holi-java
  • 29,655
  • 7
  • 72
  • 83
  • I think your answer isn't fully correct; there is more than one problem with it. – GhostCat Jun 25 '17 at 18:49
  • @GhostCat Hi, I'm not say the OP using junit4, I just use an annotation make make the `test` distinguished with test class. – holi-java Jun 25 '17 at 18:51
  • You are using an annotation that only works for JUnit4; whilst answering a question that uses JUnit3. Again: you are throwing two things together that do not make sense together. You should at least make that more explicit! – GhostCat Jun 25 '17 at 18:54
  • @GhostCat sir, is that important? I only uses it for the distinguish purpose. – holi-java Jun 25 '17 at 18:59
  • Obviously I found it important enough to tell you. What is the point of writing incomplete, misleading answers!? – GhostCat Jun 25 '17 at 19:00
  • @GhostCat I'm sorry I confused you. thanks for your feedback too much, sir. but you can see I have never wrote down the word `junit4` in my answer. – holi-java Jun 25 '17 at 19:02