Transcripted Summary

So far, we have created a few basic tests.

However, we know that in many cases, our tests will require some sort of setup or some sort of cleanup to be performed after the tests are run.

For this purpose, in JUnit 5, we can use some of the annotations that will mark our methods as either setup methods or cleanup methods.

For this purpose, we have two types of before annotations and two types of after annotations.

Let's take a look at what these are.

I am now back in my test class.

You remember that initially we created two test methods here, firstMethod() and secondMethod().

So let's assume that we want to do some sort of setup.

So we want to have some methods which hold the code that needs to execute either before anything else executes, or before each of these methods execute.

In order to do that, we will need to create a method and we will need to specify an annotation, which will say what we want that setup to be - a setup that runs before everything or a setup that needs to run before each test method.


# Before Lifecycle Methods

The two annotations that we have available for the setup part are @BeforeEach and @BeforeAll.

Let's take @BeforeAll first.

When we annotate the method with @BeforeAll, we specify that we want the code inside that method to run before anything else in the test class runs - so, before any of the methods execute.

I will just create the method and in this case, just as with the test methods, we don't need to specify an accessModifier - it's very important not to specify private.

Otherwise, we don't need to specify it at all.

The @BeforeAll method will also have a void return type just as the other test methods do.

I will name this method beforeAll(), because I wanted to reflect the name of the annotation so that we know what the purpose of this method is.

Again, I will not do anything in this method - I will just do a System.out and I will just simulate an indentation here by typing --, so that we can see the order of how the test methods run and how the setup methods run.

So I will just say "--This is the before All method".

So this method, annotated with @BeforeAll will be the setup that will run before anything else.

To give you an example similar to what you might be doing, if you were doing for example, Selenium tests, in this @BeforeAll, you could initialize the browser.

So this is where you would start the browser.

Now let's assume we also need some setup to run every time, before a test method starts running.

In order to mark a method as having to run before each method runs, we will use the @BeforeEach annotation.

We will also create this method with void return type and I will call it beforeEach().

Now, when It comes to the setup methods, some people prefer to name these methods either init or setup or something similar to that.

I prefer to give them this name because this way I can actually see that they refer to a certain annotation.

I will also do a System.out here again, just to see some output and I will do a larger indentation - I will use four dashes ---- and I will say "This is the before Each method".


package junit5tests;

import org.junit.jupiter.api.*;

public class FirstTestClass {

    @BeforeAll
    void beforeAll() {
        System.out.println("--This is the before All method");
    }


    @BeforeEach
    void beforeEach() {
        System.out.println("----This is the before Each method");
    }

    @Test
    void firstMethod() {
        System.out.println("This is the first test method");
    }

    @Test
    @DisplayName("US1234 - TC12 - this method is the second one")
    void secondMethod() {
        System.out.println("This is the second test method");
    }
}

So, up to this point, we already have two setup methods.


# After Lifecycle Methods

What about cleanup?

For example, if we started the browser in the beforeAll method, we might also need to close the browser and we would probably need to search for something similar to beforeAll.

Of course, we also have after methods, or after annotations.

So in this case, I want to start with creating the @AfterAll annotated method - this is the cleanup that will be done after everything else in the test runs - namely, all of the test methods and all of the setup.

This method will be called - in my example - afterAll().

I will also do a very basic System.out, and again, I would use the same indentation as with the beforeAll method, and I will say, "This is the after All method".

Now, of course you've guessed, we also have an @AfterEach method, which means this will be a cleanup that will be done after each test method runs.

I will also give it a void return type and I will call it afterEach(), and I will do a System.out, where I will use a larger indentation and I will say that "This is the after Each method".


package junit5tests;

import org.junit.jupiter.api.*;

public class FirstTestClass {

    @BeforeAll
    void beforeAll() {
        System.out.println("--This is the before All method");
    }


    @BeforeEach
    void beforeEach() {
        System.out.println("----This is the before Each method");
    }

    @AfterAll
    void afterAll() {
        System.out.println("--This is the after All method");
    }


    @AfterEach
    void afterEach() {
        System.out.println("----This is the after Each method");
    }

    @Test
    void firstMethod() {
        System.out.println("This is the first test method");
    }

    @Test
    @DisplayName("US1234 - TC12 - this method is the second one")
    void secondMethod() {
        System.out.println("This is the second test method");
    }
}

So, at this point, I've shown you all the types of setup and cleanup methods we have.

By the way, regarding the after methods, some people also prefer to give them a different name, like teardown or something like that.

You can name them the way you prefer.

As I said, for me, these names just make more sense.

Before we run the test, there is something else we need to consider.

Namely, in order for us not to have to mark all of these setup and cleanup methods as static, we will need to also add a class level annotation.

We want it to say - @TestInstance(TestInstance.Lifecycle.PER_CLASS).


package junit5tests;

import org.junit.jupiter.api.*;

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class FirstTestClass {

    @BeforeAll
    void beforeAll() {
        System.out.println("--This is the before All method");
    }


    @BeforeEach
    void beforeEach() {
        System.out.println("----This is the before Each method");
    }

    @AfterAll
    void afterAll() {
        System.out.println("--This is the after All method");
    }


    @AfterEach
    void afterEach() {
        System.out.println("----This is the after Each method");
    }

    @Test
    void firstMethod() {
        System.out.println("This is the first test method");
    }

    @Test
    @DisplayName("US1234 - TC12 - this method is the second one")
    void secondMethod() {
        System.out.println("This is the second test method");
    }
}

At this point, we are done with writing this code.

Let's now run it and see the output in the console.

We will run the entire class because we want to see how the tests have run and in what order.



And let's say we go to the class level result, and here you will see that the "before All method" ran first, just as we expected, then the "before Each method" ran, then we had the "first test".



Then the "after Each method" ran and then again, the "before Each method" ran and then the "second test method" ran and then again, the "after Each method" ran.

In some cases, as you noticed, we might not even need to have, for example, the "after Each" because we could have set up everything and we can perform the cleanup also in the "before Each" methods.

But that is all up to you and the way you prefer to write your tests.

This concludes our chapter on lifecycle methods.

So in this chapter, we've taken a look at the following lifecycle methods - namely, the beforeAll, beforeEach, afterAll and afterEach methods with their corresponding annotations.

These are all available also in the JUnit Jupiter API package.



Resources



© 2024 Applitools. All rights reserved. Terms and Conditions Privacy Policy GDPR