Monday, November 3, 2025

The best way to take a look at your Java purposes with JUnit 5

Itemizing 5. Logging the invocations of JUnit 5 lifecycle strategies (LifecycleDemoTest.java)


bundle com.javaworld.geekcap.lifecycle;

import org.junit.jupiter.api.*;

public class LifecycleDemoTest {

    @BeforeAll
    static void beforeAll() {
        System.out.println("Hook up with the database");
    }

    @BeforeEach
    void beforeEach() {
        System.out.println("Load the schema");
    }

    @AfterEach
    void afterEach() {
        System.out.println("Drop the schema");
    }

    @AfterAll
    static void afterAll() {
        System.out.println("Disconnect from the database");
    }

    @Check
    void testOne() {
        System.out.println("Check One");
    }

    @Check
    void testTwo() {
        System.out.println("Check Two");
    }
}

The output from working this take a look at prints the next:


Hook up with the database
Load the schema
Check One
Drop the schema
Load the schema
Check Two
Drop the schema
Disconnect from the database

As you possibly can see from this output, the beforeAll methodology is named first and should do one thing like connect with a database or create a big knowledge construction into reminiscence. Subsequent, the beforeEach methodology prepares the information for every take a look at; for instance, by populating a take a look at database with an anticipated set of information. The primary take a look at then runs, adopted by the afterEach methodology. This course of (beforeEach—> take a look at—>afterEach) continues till all of the assessments have accomplished. Lastly, the afterAll methodology cleans up the take a look at surroundings, probably by disconnecting from a database.

Earlier than wrapping up this preliminary introduction to testing with JUnit 5, I’ll present you how one can use tags to selectively run completely different sorts of take a look at circumstances. Tags are used to determine and filter particular assessments that you simply need to run in numerous situations. For instance, you may tag one take a look at class or methodology as an integration take a look at and one other as growth code. The names and makes use of of the tags are all as much as you.

We’ll create three new take a look at lessons and tag two of them as growth and one as integration, presumably to distinguish between assessments you need to run when constructing for various environments. Listings 6, 7, and eight present these three easy assessments.

Itemizing 6. JUnit 5 tags, take a look at 1 (TestOne.java)


bundle com.javaworld.geekcap.tags;

import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Check;

@Tag("Improvement")
class TestOne {
    @Check
    void testOne() {
        System.out.println("Check 1");
    }
}

Itemizing 7. JUnit 5 tags, take a look at 2 (TestTwo.java)


bundle com.javaworld.geekcap.tags;

import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Check;

@Tag("Improvement")
class TestTwo {
    @Check
    void testTwo() {
        System.out.println("Check 2");
    }
}

Itemizing 8. JUnit 5 tags, take a look at 3 (TestThree.java)


bundle com.javaworld.geekcap.tags;

import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Check;

@Tag("Integration")
class TestThree {
    @Check
    void testThree() {
        System.out.println("Check 3");
    }
}

Tags are applied by annotations, and you may annotate both a whole take a look at class or particular person strategies in a take a look at class; moreover, a category or a way can have a number of tags. On this instance, TestOne and TestTwo are annotated with the “Improvement” tag, and TestThree is annotated with the “Integration” tag. We are able to filter take a look at runs in several methods primarily based on tags. The only of those is to specify a take a look at in your Maven command line; for instance, the next solely executes assessments tagged as “Improvement”:


mvn clear take a look at -Dgroups="Improvement"

The teams property lets you specify a comma-separated record of tag names for the assessments that you really want JUnit 5 to run. Executing this yields the next output:


[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Operating com.javaworld.geekcap.tags.TestOne
Check 1
[INFO] Assessments run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.029 s - in com.javaworld.geekcap.tags.TestOne
[INFO] Operating com.javaworld.geekcap.tags.TestTwo
Check 2
[INFO] Assessments run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.001 s - in com.javaworld.geekcap.tags.TestTwo

Likewise, we may execute simply the mixing assessments as follows:


mvn clear take a look at -Dgroups="Integration"

Or, we may execute each growth and integration assessments:


mvn clear take a look at -Dgroups="Improvement, Integration"

Along with the teams property, JUnit 5 lets you use an excludedGroups property to execute all assessments that wouldn’t have the desired tag. For instance, in a growth surroundings, we don’t need to execute the mixing assessments, so we may execute the next:


mvn clear take a look at -DexcludedGroups="Integration"

That is useful as a result of a big software can have actually 1000’s of assessments. If you happen to needed to create this environmental differentiation and add some new manufacturing assessments, you wouldn’t need to have to return and add a “Improvement” tag to the opposite 10,000 assessments.

Lastly, you possibly can add these similar teams and excludedGroups fields to the surefire plugin in your Maven POM file. It’s also possible to management these fields utilizing Maven profiles. I encourage you to overview the JUnit 5 person information to be taught extra about tags.

Conclusion

This text launched among the highlights of working with JUnit 5. I confirmed you how one can configure a Maven mission to make use of JUnit 5 and how one can write assessments utilizing the @Check and @ParameterizedTest annotations. I then launched the JUnit 5 lifecycle annotations, adopted by a take a look at the use and advantages of filter tags.

Related Articles

Latest Articles