0

So test runner uses reflection behind the scenes to find out all the test classes and test methods. That is the reason we are suggested to keep our test class public. In my case I noticed JUnit is able to detect all my test classes and methods even if they are package-private. Since JUnit belongs to a different package, it should be not able to figure out my test classes and methods. Can somebody explain this. My JUnit version is 5.1.

Ravi Chaudhary
  • 660
  • 6
  • 22
  • Thanks for pointing that out. Turns out reflection won't work if SecurityManager doesn't allow it. Having your test classes and methods public is more of a recommendation, not mandate, to make sure your test are always detected and executed. – Ravi Chaudhary Apr 06 '18 at 07:05
  • Test classes are not recommended to be public (in Jupiter engine at least), and are recommended to be package private. – mkobit Apr 06 '18 at 20:09

0 Answers0