The @After annotation acts very similarly to @Before but will run after each test method.

@After public void afterEachTest() { // Cleanup }

@After is majorly used for cleaning up variables and objects, and freeing memory. If a file needs to be closed, an object needs to be removed, or an environment or array needs to be emptied, this is perfect.

Particularly if this cleanup needs to occur after each test.

If the cleanup needs to happen only once after all of the tests conclude, much like @BeforeClass, an @AfterClass annotation exists for this purpose. This will ensure the method runs after all tests within the class have already run.



TestExample.java has multiple test methods that all create and alter a test file.

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. The tests pass, however notice how much duplicate code we have across the two test methods. For each method, we open a file, open a writer, write to the file, close the writer, open a scanner, scan the file, and then close the scanner.

While the tests do pass, notice that we can move a lot of this duplicated code into a combination of @Before and @After methods.


Write an @Before method at the top of the test class (you may need to throw an IOException due to the nature of the FileWriter and Scanner). You can keep the same naming convention as before and use beforeEachTest().

Add these static variables for the file, writer, and scanner objects in the global scope of the class:

static File file; static FileWriter w; static Scanner s;

Now inside your @Before method, instantiate those variables the same way they’re instantiated inside of the test methods. You can remove all declarations and instantiations now from those test methods once they’re in the @Before method.

The test methods should now only have to worry about performing the write, closing the writer, performing the scan, and closing the scanner.

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

$ ./run.bat

Run the test class again and Check Work. The tests should still pass. We haven’t changed much, but we have at least moved the duplicated opening of the file, writer, and scanner into the @Before method for maintainability down the line.


Now let’s handle the closing of the scanner object.

Write an @After method at the bottom of the test class (you can name this one afterEachTest()) that closes the scanner (you’ll probably need to throw another IOException here too). We can’t move the closing of the writer out because the act of “closing” it is what saves the written information to the file, which needs to happen before the scanner tries to perform its scan.

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

$ ./run.bat

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

We not only do this for good testing practice but also to ensure the future maintainability of the code itself. For instance, should we want to create a new test down the line, we no longer have to open and close so many objects. Now, the code handles all of that, and we can get right to testing what we want to test.

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?