Can you tell if this test is correct?

208: @Test public void testIncrement_existingKey() { 209: assertEquals(9, tally .get("key1")); 210: }

It’s impossible to know

tally

1: private final Tally tally = new Tally(); 2: @Before public void setUp() { 3: tally.increment("key1", 8); 4: tally.increment("key2", 100); 5: tally.increment("key1", 0); 6: tally.increment("key1", 1); 7: } // 200 lines away 208: @Test public void testIncrement_existingKey() { 209: assertEquals(9, tally .get("key1")); 210: }

key1

the cause is hidden far away from the effect

write tests where the effects immediately follow the causes

1: private final Tally tally = new Tally(); 2: @Test public void testIncrement_newKey() { 3: tally.increment("key", 100); 5: assertEquals(100, tally .get("key")); 6: } 7: @Test public void testIncrement_existingKey() { 8: tally.increment("key", 8); 9: tally.increment("key", 1); 10: assertEquals(9, tally .get("key")); 11: } 12: @Test public void testIncrement_incrementByZeroDoesNothing() { 13: tally.increment("key", 8); 14: tally.increment("key", 0); 15: assertEquals(8, tally .get("key")); 16: }

The payback is in more readable code and lower maintenance costs.



