Log in from a computer to take this course

You'll need to log in from a computer to start Learn Intermediate Java. But you can practice or keep up your coding streak with the Codecademy Go app. Download the app to get started.

apple storegoogle store

The @Before annotation can be used to accompany the @Test methods. Methods tagged with @Before will run before each test method and look like the following:

@Before public void beforeEachTest() { // Something multiple tests use }

They’re typically used if multiple test methods share a similarly created object or set of variables. Placing those inside the @Before annotated method ensures those objects are always freshly created and have the same starting attributes upon instantiation. This allows each test to work with those objects and variables without needing to create them themselves inside their own test bodies.

If we look at the example we covered in the previous exercise, we created the Human named “Sam” twice in the exact same fashion. Now that we know about the @Before annotation, we could instead place the creation of sam inside of the @Before method:

Human sam; @Before public void createSam() { sam = new Human(“Sam”, 100); } @Test public void testHuman() { assertEquals(100, sam.getHitpoints()); assertEquals(“Sam”, sam.getName()); } @Test public void testHumanTakeDamage() { assertEquals(100, sam.getHitpoints()); sam.takeDamage(10); assertEquals(90, sam.getHitpoints()); }

This is different from just making sam a class variable, which is also visible by all tests because the @Before will re-run before each test. This means if tests modify the sam variable in any way, it ensures sam gets reset before the next test runs.

There’s another variant of the @Before annotation that will let us create a method that will run one time before all tests instead of multiple times before each test. This is done by using the @BeforeClass annotation.

@Before and @BeforeClass both have their unique uses, making them valuable tools for JUnit testing.



TestExample.java has multiple test methods that all interact and alter the same integer variable within the test class.

Note: This simple test class has no associated java class.

To compile and run your code, enter the following into the terminal:

$ ./run.bat

Run the test class and Check Work. Notice how some tests fail despite being written properly. Ask yourself why this might be the case.


Write an @Before method at the top of the test class and call it beforeEachTest(). Leave int a; in the global scope of the class, but set its value to 10 inside of the @Before method.

Note: Don’t forget the import for the @Before annotation!

To compile and run your code, enter the following into the terminal:

$ ./run.bat

Run the test class again, then Check Work, and watch all the tests pass as expected.

Take this course for free

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?