Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why JUnit 5 default access modifier changed to package-private

Why is the default access modifier in JUnit 5 package-private?

Tests in JUnit 4 had to be public.

What is the benefit of changing it to package-private?

like image 889
wojtek1902 Avatar asked Mar 18 '19 06:03

wojtek1902


People also ask

Is package-Private same as default?

If a class has no modifier (the default, also known as package-private), it is visible only within its own package (packages are named groups of related classes — you will learn about them in a later lesson.)

What is difference between package and private access modifiers?

Private modifier is the most restricted modifier among all modifiers. Protected modifier is more accessible than the package and private modifier but less accessible than public modifier. Package modifier is more restricted than the public and protected modifier but less restricted than the private modifier.

Should JUnit 5 tests be public?

2.3. Test classes, test methods, and lifecycle methods are not required to be public , but they must not be private .

What is the default access specifier for package?

The default access modifier is also called package-private, which means that all members are visible within the same package but aren't accessible from other packages: package com.


1 Answers

Why is the default access modifier in JUnit 5 package-private?

It's not the "default". There technically is no default. Rather, in JUnit Jupiter you have a choice: public, protected or package-private.

What is the benefit of changing it to package-private?

The benefit is that you don't have type public anymore. If your IDE automatically generates test methods and test classes for you that are public, feel free to leave them public.

But... if you are typing in the methods on your own, then just leave off public unless you are designing your test classes for subclassing from other packages, in which case you'd want to make your overrideable test methods either public or protected. And of course, interface default methods must be public.

Long story, short: we (the JUnit 5 team) believe in the principle "Less is more", meaning the less you have to type to achieve your goal, the better!

like image 167
Sam Brannen Avatar answered Oct 09 '22 23:10

Sam Brannen