rspec/rules/S5790/java/rule.adoc
Fred Tingaud 16f6c0aecf
Inline adoc when include has no additional value (#1940)
Inline adoc files when they are included exactly once.

Also fix language tags because this inlining gives us better information
on what language the code is written in.
2023-05-25 14:18:12 +02:00

80 lines
1.9 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

== Why is this an issue?
If not annotated with ``++@Nested++``, an inner class containing some tests will never be executed during tests execution. While you could still be able to manually run its tests in an IDE, it wont be the case during the build. By contrast, a static nested class containing some tests should not be annotated with ``++@Nested++``, JUnit5 will not share setup and state with an instance of its enclosing class.
This rule raises an issue on inner classes and static nested classes containing JUnit5 test methods which has a wrong usage of ``++@Nested++`` annotation.
Note: This rule does not check if the context in which JUnit 5 is running (e.g. Maven Surefire Plugin) is properly configured to execute static nested classes, it could not be the case using the default configuration.
=== Noncompliant code example
[source,java]
----
import org.junit.jupiter.api.Test;
class MyJunit5Test {
@Test
void test() { /* ... */ }
class InnerClassTest { // Noncompliant, missing @Nested annotation
@Test
void test() { /* ... */ }
}
@Nested
static class StaticNestedClassTest { // Noncompliant, invalid usage of @Nested annotation
@Test
void test() { /* ... */ }
}
}
----
=== Compliant solution
[source,java]
----
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Nested;
class MyJunit5Test {
@Test
void test() { /* ... */ }
@Nested
class InnerClassTest {
@Test
void test() { /* ... */ }
}
static class StaticNestedClassTest {
@Test
void test() { /* ... */ }
}
}
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Add @Nested to this inner test class
Remove @Nested from this static nested test class or convert it into an inner class
=== Highlighting
the nested class name identifier
endif::env-github,rspecator-view[]