问题
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
What is happening behind the scenes?
Is it using reflection or what?
回答1:
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).
回答2:
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.
来源:https://stackoverflow.com/questions/37019972/why-junit-test-casesmethods-should-be-public