Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JUnit 5 test visibility and typing

I'm currently studying a JUnit 5 book and I need assistance in understanding this line:

A test method can be either protected or package protected. The preferred is to use package protected as that leads to less typing.

like image 203
divad Avatar asked Oct 18 '25 03:10

divad


1 Answers

If the citation is accurate, it is wrong. A Jupiter test method (there’s no such thing as a JUnit 5 test method) can be anything but private, so it can be public, protected or package private. Package private means that it has no accessibility modifier.

That means that running the following test class:

import org.junit.jupiter.api.Test;

class MyTests {
    @Test
    public void test1() {
    }

    @Test
    protected void test2() {
    }

    @Test
    void test3() {
    }

    @Test
    private void test4() {
    }
}

will execute test1, test2 and test3, but not test4. test3 being the preferred one.

Mind that the same holds for the class' accessibility modifier: package private and public are possible. Private classes are not being executed, protected classes do not exist in Java.

like image 169
johanneslink Avatar answered Oct 19 '25 19:10

johanneslink