From 60d05d4204f5ed711c4681666d03235023654b73 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Fri, 8 Sep 2023 19:26:24 +0200 Subject: [PATCH] Polishing --- .../ROOT/pages/languages/kotlin/spring-projects-in.adoc | 9 ++++++--- .../annotation/AutowiredAnnotationBeanPostProcessor.java | 5 +++-- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/framework-docs/modules/ROOT/pages/languages/kotlin/spring-projects-in.adoc b/framework-docs/modules/ROOT/pages/languages/kotlin/spring-projects-in.adoc index 021d436a37..ae7fee0e6b 100644 --- a/framework-docs/modules/ROOT/pages/languages/kotlin/spring-projects-in.adoc +++ b/framework-docs/modules/ROOT/pages/languages/kotlin/spring-projects-in.adoc @@ -251,11 +251,14 @@ https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-featu === Constructor injection 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 {api-spring-framework}/test/context/TestConstructor.html[`@TestConstructor(autowireMode = AutowireMode.ALL)`] 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] ---- @SpringJUnitConfig(TestConfig::class) @@ -272,11 +275,11 @@ class OrderServiceIntegrationTests(val orderService: OrderService, === `PER_CLASS` Lifecycle 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` 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. The following example demonstrates `@BeforeAll` and `@AfterAll` annotations on non-static methods: diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java b/spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java index 75f932757f..d409841a2d 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java @@ -193,9 +193,10 @@ public class AutowiredAnnotationBeanPostProcessor implements SmartInstantiationA this.autowiredAnnotationTypes.add(Autowired.class); this.autowiredAnnotationTypes.add(Value.class); + ClassLoader classLoader = AutowiredAnnotationBeanPostProcessor.class.getClassLoader(); try { this.autowiredAnnotationTypes.add((Class) - 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"); } catch (ClassNotFoundException ex) { @@ -204,7 +205,7 @@ public class AutowiredAnnotationBeanPostProcessor implements SmartInstantiationA try { this.autowiredAnnotationTypes.add((Class) - 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"); } catch (ClassNotFoundException ex) {