@Before is executed before each test,
@BeforeClass runs once before the entire test fixture.
If your test class has ten tests, @Before code will be executed ten times, but @BeforeClass will be executed only once.
In general, you use @BeforeClass when multiple tests need to share the same computationally expensive setup code. Establishing a database connection falls into this category. You can move code from @BeforeClass into @Before, but your test run may take longer. Note that the code marked @BeforeClass is run as static initializer, therefore it will run before the class instance of your test fixture is created.
One thought on "JUnit Annotations Cheat Sheet"