Home > Back-end >  JUnit 5 test visibility and typing
JUnit 5 test visibility and typing

Time:10-01

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.

CodePudding user response:

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.

CodePudding user response:

In the book I'm currently studying, the first line was explained. But the second statement which wasn't was my major concern. After other searches, I've figured out it means that the developer gets to type less, as the method is defined with less words.

  • Related