Skip to content

Commit ca0e894

Browse files
committed
Merge pull request #35784 from sdeleuze
* pr/35784: Refine SpringApplication.Augmented.with Kotlin extension Closes gh-35784
2 parents 1652c27 + db8dee5 commit ca0e894

File tree

2 files changed

+29
-10
lines changed

2 files changed

+29
-10
lines changed

spring-boot-project/spring-boot/src/main/kotlin/org/springframework/boot/SpringApplicationExtensions.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -68,11 +68,12 @@ inline fun <reified T : Any> fromApplication(): SpringApplication.Augmented {
6868
}
6969

7070
/**
71-
* Extension function that allows `SpringApplication.Augmented.with` to work with Kotlin classes.
71+
* Extension function that allows [SpringApplication.Augmented.with] to work with Kotlin classes.
7272
*
7373
* @author Phillip Webb
74+
* @author Sebastien Deleuze
7475
* @since 3.1.1
7576
*/
76-
fun SpringApplication.Augmented.with(type: KClass<*>): SpringApplication.Augmented {
77-
return this.with(type.java)!!
77+
fun SpringApplication.Augmented.with(vararg types: KClass<*>): SpringApplication.Augmented {
78+
return this.with(*types.map(KClass<*>::java).toTypedArray())!!
7879
}

spring-boot-project/spring-boot/src/test/kotlin/org/springframework/boot/SpringApplicationExtensionsTests.kt

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,6 +21,7 @@ import org.junit.jupiter.api.Test
2121

2222
import org.springframework.beans.factory.getBean
2323
import org.springframework.boot.kotlinsample.TestKotlinApplication
24+
import org.springframework.boot.web.servlet.mock.MockFilter
2425
import org.springframework.boot.web.servlet.server.MockServletWebServerFactory
2526
import org.springframework.context.annotation.Bean
2627
import org.springframework.context.annotation.Configuration
@@ -36,7 +37,7 @@ class SpringApplicationExtensionsTests {
3637
@Test
3738
fun `Kotlin runApplication() top level function`() {
3839
val context = runApplication<ExampleWebConfig>()
39-
assertThat(context).isNotNull
40+
assertThat(context).isNotNull()
4041
}
4142

4243
@Test
@@ -45,7 +46,7 @@ class SpringApplicationExtensionsTests {
4546
val context = runApplication<ExampleWebConfig> {
4647
setEnvironment(environment)
4748
}
48-
assertThat(context).isNotNull
49+
assertThat(context).isNotNull()
4950
assertThat(environment).isEqualTo(context.environment)
5051
}
5152

@@ -54,7 +55,7 @@ class SpringApplicationExtensionsTests {
5455
val context = runApplication<ExampleWebConfig>("--debug", "spring", "boot")
5556
val args = context.getBean<ApplicationArguments>()
5657
assertThat(args.nonOptionArgs.toTypedArray()).containsExactly("spring", "boot")
57-
assertThat(args.containsOption("debug")).isEqualTo(true)
58+
assertThat(args.containsOption("debug")).isTrue()
5859
}
5960

6061
@Test
@@ -65,14 +66,21 @@ class SpringApplicationExtensionsTests {
6566
}
6667
val args = context.getBean<ApplicationArguments>()
6768
assertThat(args.nonOptionArgs.toTypedArray()).containsExactly("spring", "boot")
68-
assertThat(args.containsOption("debug")).isEqualTo(true)
69+
assertThat(args.containsOption("debug")).isTrue()
6970
assertThat(environment).isEqualTo(context.environment)
7071
}
7172

7273
@Test
7374
fun `Kotlin fromApplication() top level function`() {
7475
val context = fromApplication<TestKotlinApplication>().with(ExampleWebConfig::class).run().applicationContext
75-
assertThat(context.getBean<MockServletWebServerFactory>()).isNotNull
76+
assertThat(context.getBean<MockServletWebServerFactory>()).isNotNull()
77+
}
78+
79+
@Test
80+
fun `Kotlin fromApplication() top level function with multiple sources`() {
81+
val context = fromApplication<TestKotlinApplication>().with(ExampleWebConfig::class, ExampleFilterConfig::class).run().applicationContext
82+
assertThat(context.getBean<MockServletWebServerFactory>()).isNotNull()
83+
assertThat(context.getBean<MockFilter>()).isNotNull()
7684
}
7785

7886
@Test
@@ -91,4 +99,14 @@ class SpringApplicationExtensionsTests {
9199

92100
}
93101

102+
@Configuration(proxyBeanMethods = false)
103+
internal open class ExampleFilterConfig {
104+
105+
@Bean
106+
open fun filter(): MockFilter {
107+
return MockFilter()
108+
}
109+
110+
}
111+
94112
}

0 commit comments

Comments
 (0)