I'm new to unit test. About the purpose of using @Before
annotation in JUnit 4. I just don't know the point of using it:
public class FoodTestCase {
static private Food sandwich;
@BeforeClass
public static void initialise(){
sandwich = new Sandwich();
}
}
vs
public class FoodTestCase {
static private Food sandwich = new Sandwich();
}
What's the difference?
@BeforeClass
is for static initializations.Instances created here will be reused across all of your
@Test
sWhereas
@Before
is per@Test
.Usually
@BeforeClass
is reserved for objects which are relatively expensive to instantiate.e.g. Database connections