Why Junit test cases(Methods) should be public? [duplicate]

北慕城南 提交于 2021-02-04 15:25:21

问题


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?


回答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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!