0

When I try to run a test written as follows:

@Test
private void testGet() {
    assertTrue(true);
}

I receive the following exception - java.lang.Exception: Method testGet() should be public

Why can a private method not execute as a unit test?

Tabish Mir
  • 717
  • 6
  • 26
  • 3
    Why would you make a test private anyway? – Federico klez Culloca Sep 25 '20 at 10:40
  • 4
    In JUnit5, they do not have to be public and are normally declared package-private. – Turing85 Sep 25 '20 at 10:40
  • 1
    Does this answer your question? [Why should Test methods in Junit be defined public?](https://stackoverflow.com/questions/19286403/why-should-test-methods-in-junit-be-defined-public) – Arnaud Claudel Sep 25 '20 at 10:40
  • 2
    @ArnaudClaudel I disagree with this duplicate. It is quite old and - as I have mentioned above - the limitation is weakened in JUnit5. – Turing85 Sep 25 '20 at 10:41
  • @Turing85 I don't see what this question adds compared to the old one, it actually contains even less details. If there is a new answer, then let's post it on the old question as an update. – Arnaud Claudel Sep 25 '20 at 10:45
  • @ArnaudClaudel again, those post are quite old. JUnit5 is relatively new. The questions and their answers are related to older JUnit versions, thus the statements do no longer hold. Discrepancies like those are unavoidable. – Turing85 Sep 25 '20 at 10:51

1 Answers1

0

It can't be a private , but need not be public. It can be a default access method. It can't be private method because the method has to be invoked from out side the class by the junit framework.