Sunday, June 15, 2025

How you can check 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("Connect 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");
    }

    @Take a look at
    void testOne() {
        System.out.println("Take a look at One");
    }

    @Take a look at
    void testTwo() {
        System.out.println("Take a look at Two");
    }
}

The output from operating this check prints the next:


Connect with the database
Load the schema
Take a look at One
Drop the schema
Load the schema
Take a look at Two
Drop the schema
Disconnect from the database

As you may see from this output, the beforeAll methodology is named first and will do one thing like connect with a database or create a big knowledge construction into reminiscence. Subsequent, the beforeEach methodology prepares the info for every check; for instance, by populating a check database with an anticipated set of knowledge. The primary check then runs, adopted by the afterEach methodology. This course of (beforeEach—> check—>afterEach) continues till all of the checks have accomplished. Lastly, the afterAll methodology cleans up the check setting, presumably by disconnecting from a database.

Earlier than wrapping up this preliminary introduction to testing with JUnit 5, I’ll present you the right way to use tags to selectively run completely different sorts of check instances. Tags are used to determine and filter particular checks that you just need to run in varied situations. For instance, you would possibly tag one check class or methodology as an integration check and one other as improvement code. The names and makes use of of the tags are all as much as you.

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

Itemizing 6. JUnit 5 tags, check 1 (TestOne.java)


bundle com.javaworld.geekcap.tags;

import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Take a look at;

@Tag("Growth")
class TestOne {
    @Take a look at
    void testOne() {
        System.out.println("Take a look at 1");
    }
}

Itemizing 7. JUnit 5 tags, check 2 (TestTwo.java)


bundle com.javaworld.geekcap.tags;

import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Take a look at;

@Tag("Growth")
class TestTwo {
    @Take a look at
    void testTwo() {
        System.out.println("Take a look at 2");
    }
}

Itemizing 8. JUnit 5 tags, check 3 (TestThree.java)


bundle com.javaworld.geekcap.tags;

import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Take a look at;

@Tag("Integration")
class TestThree {
    @Take a look at
    void testThree() {
        System.out.println("Take a look at 3");
    }
}

Tags are applied by annotations, and you’ll annotate both a whole check class or particular person strategies in a check class; moreover, a category or a technique can have a number of tags. On this instance, TestOne and TestTwo are annotated with the “Growth” tag, and TestThree is annotated with the “Integration” tag. We will filter check runs in numerous methods primarily based on tags. The best of those is to specify a check in your Maven command line; for instance, the next solely executes checks tagged as “Growth”:


mvn clear check -Dgroups="Growth"

The teams property permits you to specify a comma-separated listing of tag names for the checks 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
Take a look at 1
[INFO] Checks 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
Take a look at 2
[INFO] Checks 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 checks as follows:


mvn clear check -Dgroups="Integration"

Or, we may execute each improvement and integration checks:


mvn clear check -Dgroups="Growth, Integration"

Along with the teams property, JUnit 5 permits you to use an excludedGroups property to execute all checks that would not have the desired tag. For instance, in a improvement setting, we don’t need to execute the mixing checks, so we may execute the next:


mvn clear check -DexcludedGroups="Integration"

That is useful as a result of a big utility can have actually hundreds of checks. If you happen to wished to create this environmental differentiation and add some new manufacturing checks, you wouldn’t need to have to return and add a “Growth” tag to the opposite 10,000 checks.

Lastly, you may 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 study extra about tags.

Conclusion

This text launched a number of the highlights of working with JUnit 5. I confirmed you the right way to configure a Maven undertaking to make use of JUnit 5 and the right way to write checks utilizing the @Take a look at and @ParameterizedTest annotations. I then launched the JUnit 5 lifecycle annotations, adopted by a have a look at the use and advantages of filter tags.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles