Using Annotations

@Before method

Methods annotated with the @Before annotation are run before each test.

As we add more tests for a given set of related behaviors, it becomes clear that many of them have the same initialization needs. We’ll want to take advantage of the @Before method to help ensure that our tests don’t become a maintenance nightmare of redundant code.

It’s important to understand the order in which JUnit executes @Before and @Test methods.

The AssertTest class below in which the @Before method resides has two tests, hasPositiveBalance and depositIncreasesBalance.

Get hands-on with 1400+ tech skills courses.