@ First of all, it makes sense to use it in some cases, because it gets the name AFTER the constructor for the class. This difference is important when you use a mock structure like Mockito with @Mock annotations because your @Before method will be called after mocks is initialized. You can then use your mocks to provide constructor arguments for the class you are testing.
I find this to be a very common pattern in my unit tests when using beans collaboration.
Here is an example (admittedly far-fetched):
@RunWith(MockitoJUnitRunner.class) public class CalculatorTest { @Mock Adder adder; @Mock Subtractor subtractor; @Mock Divider divider; @Mock Multiplier multiplier; Calculator calculator; @Before public void setUp() { calculator = new Calculator(adder,subtractor,divider,multiplier); } @Test public void testAdd() { BigDecimal value = calculator.add(2,2); verify(adder).add(eq(2),eq(2)); } }
HopefullyHelpful Sep 10 '12 at 19:24 2012-09-10 19:24
source share