2023-05-03 11:06:20 +02:00
== Why is this an issue?
2021-04-28 16:49:39 +02:00
JUnit5 is more tolerant regarding the visibilities of Test classes and methods than JUnit4, which required everything to be public. JUnit5 supports default package, public and protected visibility, even if it is recommended to use the default package visibility, which improves the readability of code.
But JUnit5 ignores without any warning:
* private classes and private methods
* static methods
* methods returning a value without being a TestFactory
2021-04-28 18:08:03 +02:00
2023-05-03 11:06:20 +02:00
=== Noncompliant code example
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,java]
2021-04-28 16:49:39 +02:00
----
import org.junit.jupiter.api.Test;
class MyClassTest {
@Test
private void test1() { // Noncompliant - ignored by JUnit5
// ...
}
@Test
static void test2() { // Noncompliant - ignored by JUnit5
// ...
}
@Test
boolean test3() { // Noncompliant - ignored by JUnit5
// ...
}
@Nested
private class MyNestedClass { // Noncompliant - ignored by JUnit5
@Test
void test() {
// ...
}
}
}
----
2021-04-28 18:08:03 +02:00
2023-05-03 11:06:20 +02:00
=== Compliant solution
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,java]
2021-04-28 16:49:39 +02:00
----
import org.junit.jupiter.api.Test;
class MyClassTest {
@Test
void test1() {
// ...
}
@Test
void test2() {
// ...
}
@Test
void test3() {
// ...
}
@Nested
class MyNestedClass {
@Test
void test() {
// ...
}
}
}
----
2021-04-28 18:08:03 +02:00
2021-06-02 20:44:38 +02:00
2021-06-03 09:05:38 +02:00
ifdef::env-github,rspecator-view[]
2021-09-20 15:38:42 +02:00
'''
== Implementation Specification
(visible only on this page)
2023-05-25 14:18:12 +02:00
=== Message
Remove this 'private' modifier.
Remove this 'static' modifier.
Replace the return type by void.
=== Highlighting
The modifier or the return type
2021-09-20 15:38:42 +02:00
2021-06-08 15:52:13 +02:00
'''
2021-06-02 20:44:38 +02:00
== Comments And Links
(visible only on this page)
2023-05-25 14:18:12 +02:00
=== on 20 May 2020, 14:17:02 Damien Urruty wrote:
There is a typo in this rule's description.
It should read: "But in any case, the 'private' modifier should not be used" instead of "But in any case, the 'private' modifier should be used".
2021-06-03 09:05:38 +02:00
endif::env-github,rspecator-view[]