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?
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?
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!
This is JUnit 5 feature which produce a better encapsulation for test classes and methods
Make Jupiter tests package private #679
Test class mostly located in the same package of the class tested:
better way is to place the tests in a separate parallel directory structure with package alignment.
main/ test/ com/ com/ xyz/ xyz/ 📜 SomeClass.java 🔨 SomeClassTests.java
This approach allows test code to access all the public and package visible members of the classes under test.
Test code is as important as production code. Give it the same level of care and attention. "this is only test code" is not a valid excuse to justify sloppy code
martinfowler.com/articles/… –
Quaquaversal © 2022 - 2024 — McMap. All rights reserved.
public
everywhere. – Mandrake