Polishing
This commit is contained in:
parent
dfea3d05aa
commit
60d05d4204
|
|
@ -251,11 +251,14 @@ https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-featu
|
||||||
=== Constructor injection
|
=== Constructor injection
|
||||||
|
|
||||||
As described in the xref:testing/testcontext-framework/support-classes.adoc#testcontext-junit-jupiter-di[dedicated section],
|
As described in the xref:testing/testcontext-framework/support-classes.adoc#testcontext-junit-jupiter-di[dedicated section],
|
||||||
JUnit 5 allows constructor injection of beans which is pretty useful with Kotlin
|
JUnit Jupiter (JUnit 5) allows constructor injection of beans which is pretty useful with Kotlin
|
||||||
in order to use `val` instead of `lateinit var`. You can use
|
in order to use `val` instead of `lateinit var`. You can use
|
||||||
{api-spring-framework}/test/context/TestConstructor.html[`@TestConstructor(autowireMode = AutowireMode.ALL)`]
|
{api-spring-framework}/test/context/TestConstructor.html[`@TestConstructor(autowireMode = AutowireMode.ALL)`]
|
||||||
to enable autowiring for all parameters.
|
to enable autowiring for all parameters.
|
||||||
|
|
||||||
|
NOTE: You can also change the default behavior to `ALL` in a `junit-platform.properties`
|
||||||
|
file with a `spring.test.constructor.autowire.mode = all` property.
|
||||||
|
|
||||||
[source,kotlin,indent=0]
|
[source,kotlin,indent=0]
|
||||||
----
|
----
|
||||||
@SpringJUnitConfig(TestConfig::class)
|
@SpringJUnitConfig(TestConfig::class)
|
||||||
|
|
@ -272,11 +275,11 @@ class OrderServiceIntegrationTests(val orderService: OrderService,
|
||||||
=== `PER_CLASS` Lifecycle
|
=== `PER_CLASS` Lifecycle
|
||||||
|
|
||||||
Kotlin lets you specify meaningful test function names between backticks (```).
|
Kotlin lets you specify meaningful test function names between backticks (```).
|
||||||
As of JUnit 5, Kotlin test classes can use the `@TestInstance(TestInstance.Lifecycle.PER_CLASS)`
|
With JUnit Jupiter (JUnit 5), Kotlin test classes can use the `@TestInstance(TestInstance.Lifecycle.PER_CLASS)`
|
||||||
annotation to enable single instantiation of test classes, which allows the use of `@BeforeAll`
|
annotation to enable single instantiation of test classes, which allows the use of `@BeforeAll`
|
||||||
and `@AfterAll` annotations on non-static methods, which is a good fit for Kotlin.
|
and `@AfterAll` annotations on non-static methods, which is a good fit for Kotlin.
|
||||||
|
|
||||||
You can also change the default behavior to `PER_CLASS` thanks to a `junit-platform.properties`
|
NOTE: You can also change the default behavior to `PER_CLASS` in a `junit-platform.properties`
|
||||||
file with a `junit.jupiter.testinstance.lifecycle.default = per_class` property.
|
file with a `junit.jupiter.testinstance.lifecycle.default = per_class` property.
|
||||||
|
|
||||||
The following example demonstrates `@BeforeAll` and `@AfterAll` annotations on non-static methods:
|
The following example demonstrates `@BeforeAll` and `@AfterAll` annotations on non-static methods:
|
||||||
|
|
|
||||||
|
|
@ -193,9 +193,10 @@ public class AutowiredAnnotationBeanPostProcessor implements SmartInstantiationA
|
||||||
this.autowiredAnnotationTypes.add(Autowired.class);
|
this.autowiredAnnotationTypes.add(Autowired.class);
|
||||||
this.autowiredAnnotationTypes.add(Value.class);
|
this.autowiredAnnotationTypes.add(Value.class);
|
||||||
|
|
||||||
|
ClassLoader classLoader = AutowiredAnnotationBeanPostProcessor.class.getClassLoader();
|
||||||
try {
|
try {
|
||||||
this.autowiredAnnotationTypes.add((Class<? extends Annotation>)
|
this.autowiredAnnotationTypes.add((Class<? extends Annotation>)
|
||||||
ClassUtils.forName("jakarta.inject.Inject", AutowiredAnnotationBeanPostProcessor.class.getClassLoader()));
|
ClassUtils.forName("jakarta.inject.Inject", classLoader));
|
||||||
logger.trace("'jakarta.inject.Inject' annotation found and supported for autowiring");
|
logger.trace("'jakarta.inject.Inject' annotation found and supported for autowiring");
|
||||||
}
|
}
|
||||||
catch (ClassNotFoundException ex) {
|
catch (ClassNotFoundException ex) {
|
||||||
|
|
@ -204,7 +205,7 @@ public class AutowiredAnnotationBeanPostProcessor implements SmartInstantiationA
|
||||||
|
|
||||||
try {
|
try {
|
||||||
this.autowiredAnnotationTypes.add((Class<? extends Annotation>)
|
this.autowiredAnnotationTypes.add((Class<? extends Annotation>)
|
||||||
ClassUtils.forName("javax.inject.Inject", AutowiredAnnotationBeanPostProcessor.class.getClassLoader()));
|
ClassUtils.forName("javax.inject.Inject", classLoader));
|
||||||
logger.trace("'javax.inject.Inject' annotation found and supported for autowiring");
|
logger.trace("'javax.inject.Inject' annotation found and supported for autowiring");
|
||||||
}
|
}
|
||||||
catch (ClassNotFoundException ex) {
|
catch (ClassNotFoundException ex) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue