-
Notifications
You must be signed in to change notification settings - Fork 38.6k
Description
Original Description
I find this test interesting:
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = RestTemplate.class)
class MyTest {
@Autowired
RestTemplate restTemplate;
@Test
void check() {
assertThat(restTemplate).isNotNull();
}
}
This works because while loading a context, AnnotationConfigContextLoader
registers @ContextConfiguration
provided classes(RestTemplate
in this case) to bean definitions, so that they are available for injections.
So, this is also possible:
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = { MyTest2.Foo.class, MyTest2.Bar.class })
class MyTest2 {
static class Foo {
public Foo(Bar bar) { // dependency to Bar
}
}
static class Bar {
public Bar() {
}
}
@Autowired
Foo foo;
@Autowired
Bar bar;
@Test
void check() {
assertThat(this.foo).isNotNull();
assertThat(this.bar).isNotNull();
}
}
I think this is not an intended usage of @ContextConfiguration#classes
.
Probably, by default, filter-out or validate those classes to be @Configuration
classes.
For the case of allowing non @Configuration
classes (for example, lite-mode), probably provide an explicit option(new attribute) on @ContextConfiguration
. e.g.: @ContextConfiguration(classes=MyBean.class, liteMode=true)
This is what I found in real world code base:
@ExtendWith({MockitoExtension.class, SpringExtension.class})
@ContextConfiguration(classes = {ResourceBundleMessageSource.class, MyService.class,
MyExceptionHandler.class, MyPropertyConfiguration.class, RestTemplate.class})
public class MyExceptionHandlerTest {
// ...
}
So, would be nice not seeing such test class :)
Deliverables
Improve documentation for the following regarding "annotated classes".
-
AnnotationConfigRegistry
-
AnnotationConfigApplicationContext
-
AnnotationConfigWebApplicationContext
-
AnnotatedBeanDefinitionReader
-
@Configuration
-
@Import
-
AnnotationConfigContextLoader
-
AnnotationConfigContextLoaderUtils
-
@ContextConfiguration
- Reference Manual:
testing.adoc