Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,85 @@ Kotlin::
----
======

If a `@Nested` test class should not inherit the configuration of its enclosing class,
annotate it with `@NestedTestConfiguration(EnclosingConfiguration.OVERRIDE)`. In
`OVERRIDE` mode, none of the Spring test annotations on the enclosing class are inherited,
including `@SpringJUnitConfig` and `@ContextConfiguration`, so the nested class has to
declare all of the configuration it needs. In the following example, `EnglishGreetings`
inherits the `@SpringJUnitConfig(TestConfig.class)` and `@ActiveProfiles("lang_en")`
configuration from `GreetingServiceTests`, while `FrenchGreetings` overrides both by
declaring its own configuration class and active profile.

[tabs]
======
Java::
+
[source,java,indent=0,subs="verbatim,quotes"]
----
@SpringJUnitConfig(TestConfig.class)
@ActiveProfiles("lang_en")
class GreetingServiceTests {
Comment thread
sbrannen marked this conversation as resolved.
Resolved

@Nested
class EnglishGreetings {

@Test
void hello(@Autowired GreetingService service) {
assertThat(service.greetWorld()).isEqualTo("Hello World");
}
}

@Nested
@NestedTestConfiguration(EnclosingConfiguration.OVERRIDE)
@SpringJUnitConfig(FrenchConfig.class)
@ActiveProfiles("lang_fr")
class FrenchGreetings {

@Test
void hello(@Autowired GreetingService service) {
assertThat(service.greetWorld()).isEqualTo("Bonjour le monde");
}
}
}
----

Kotlin::
+
[source,kotlin,indent=0,subs="verbatim,quotes"]
----
@SpringJUnitConfig(TestConfig::class)
@ActiveProfiles("lang_en")
class GreetingServiceTests {

@Nested
inner class EnglishGreetings {

@Test
fun hello(@Autowired service:GreetingService) {
assertThat(service.greetWorld()).isEqualTo("Hello World")
}
}

@Nested
@NestedTestConfiguration(EnclosingConfiguration.OVERRIDE)
@SpringJUnitConfig(FrenchConfig::class)
@ActiveProfiles("lang_fr")
inner class FrenchGreetings {

@Test
fun hello(@Autowired service:GreetingService) {
assertThat(service.greetWorld()).isEqualTo("Bonjour le monde")
}
}
}
----
======

TIP: If you only need to replace some configuration, `OVERRIDE` mode is often more than
you want. For example, to keep the inherited configuration classes but use different
test properties, declare `@TestPropertySource(properties = ..., inheritProperties = false)`
on the `@Nested` class instead.


[[testcontext-junit4-support]]
== JUnit 4 Support
Expand Down