rspec/rules/S5790/java/rule.adoc

68 lines
1.7 KiB
Plaintext
Raw Normal View History

2021-04-28 16:49:39 +02:00
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.
2021-04-28 16:49:39 +02:00
== Noncompliant Code Example
----
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() { /* ... */ }
}
}
----
2021-04-28 16:49:39 +02:00
== Compliant Solution
----
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)
include::message.adoc[]
include::highlighting.adoc[]
endif::env-github,rspecator-view[]