15

I just want to know why the test cases (test methods) should be public like this

public class SpiceLoginTest {
    @Test
    public void testShouldVerifyLoginRequest() {
    }  
}

but if I remove public access specifier from this method

    @Test
    void testShouldVerifyLoginRequest() {
    }

Output: java.lang.Exception: Method testShouldVerifyLoginRequest() should be public

so

  1. What is happening behind the scenes?

  2. Is it using reflection or what?

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Gurinder
  • 943
  • 2
  • 9
  • 19

2 Answers2

14

Yes, the test runner is using reflection behind the scenes to find out what your test methods are and how to call them.

If the methods were not public, calling them might fail (because the SecurityManager gets to veto that).

Thilo
  • 257,207
  • 101
  • 511
  • 656
0

This is almost certainly because JUnit creates some main class which calls all of your test methods for you. To call them, they need to be public.

nhouser9
  • 6,730
  • 3
  • 21
  • 42