Refine SpringApplication.Augmented.with Kotlin extension

This commit allows to specify multiple Kotlin classes.

See gh-35784
pull/36038/head
Sébastien Deleuze 1 year ago committed by Stephane Nicoll
parent 1652c27b3c
commit db8dee5aac

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2017 the original author or authors. * Copyright 2012-2023 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -68,11 +68,12 @@ inline fun <reified T : Any> fromApplication(): SpringApplication.Augmented {
} }
/** /**
* Extension function that allows `SpringApplication.Augmented.with` to work with Kotlin classes. * Extension function that allows [SpringApplication.Augmented.with] to work with Kotlin classes.
* *
* @author Phillip Webb * @author Phillip Webb
* @author Sebastien Deleuze
* @since 3.1.1 * @since 3.1.1
*/ */
fun SpringApplication.Augmented.with(type: KClass<*>): SpringApplication.Augmented { fun SpringApplication.Augmented.with(vararg types: KClass<*>): SpringApplication.Augmented {
return this.with(type.java)!! return this.with(*types.map(KClass<*>::java).toTypedArray())!!
} }

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2022 the original author or authors. * Copyright 2002-2023 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -21,6 +21,7 @@ import org.junit.jupiter.api.Test
import org.springframework.beans.factory.getBean import org.springframework.beans.factory.getBean
import org.springframework.boot.kotlinsample.TestKotlinApplication import org.springframework.boot.kotlinsample.TestKotlinApplication
import org.springframework.boot.web.servlet.mock.MockFilter
import org.springframework.boot.web.servlet.server.MockServletWebServerFactory import org.springframework.boot.web.servlet.server.MockServletWebServerFactory
import org.springframework.context.annotation.Bean import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration import org.springframework.context.annotation.Configuration
@ -36,7 +37,7 @@ class SpringApplicationExtensionsTests {
@Test @Test
fun `Kotlin runApplication() top level function`() { fun `Kotlin runApplication() top level function`() {
val context = runApplication<ExampleWebConfig>() val context = runApplication<ExampleWebConfig>()
assertThat(context).isNotNull assertThat(context).isNotNull()
} }
@Test @Test
@ -45,7 +46,7 @@ class SpringApplicationExtensionsTests {
val context = runApplication<ExampleWebConfig> { val context = runApplication<ExampleWebConfig> {
setEnvironment(environment) setEnvironment(environment)
} }
assertThat(context).isNotNull assertThat(context).isNotNull()
assertThat(environment).isEqualTo(context.environment) assertThat(environment).isEqualTo(context.environment)
} }
@ -54,7 +55,7 @@ class SpringApplicationExtensionsTests {
val context = runApplication<ExampleWebConfig>("--debug", "spring", "boot") val context = runApplication<ExampleWebConfig>("--debug", "spring", "boot")
val args = context.getBean<ApplicationArguments>() val args = context.getBean<ApplicationArguments>()
assertThat(args.nonOptionArgs.toTypedArray()).containsExactly("spring", "boot") assertThat(args.nonOptionArgs.toTypedArray()).containsExactly("spring", "boot")
assertThat(args.containsOption("debug")).isEqualTo(true) assertThat(args.containsOption("debug")).isTrue()
} }
@Test @Test
@ -65,14 +66,21 @@ class SpringApplicationExtensionsTests {
} }
val args = context.getBean<ApplicationArguments>() val args = context.getBean<ApplicationArguments>()
assertThat(args.nonOptionArgs.toTypedArray()).containsExactly("spring", "boot") assertThat(args.nonOptionArgs.toTypedArray()).containsExactly("spring", "boot")
assertThat(args.containsOption("debug")).isEqualTo(true) assertThat(args.containsOption("debug")).isTrue()
assertThat(environment).isEqualTo(context.environment) assertThat(environment).isEqualTo(context.environment)
} }
@Test @Test
fun `Kotlin fromApplication() top level function`() { fun `Kotlin fromApplication() top level function`() {
val context = fromApplication<TestKotlinApplication>().with(ExampleWebConfig::class).run().applicationContext val context = fromApplication<TestKotlinApplication>().with(ExampleWebConfig::class).run().applicationContext
assertThat(context.getBean<MockServletWebServerFactory>()).isNotNull assertThat(context.getBean<MockServletWebServerFactory>()).isNotNull()
}
@Test
fun `Kotlin fromApplication() top level function with multiple sources`() {
val context = fromApplication<TestKotlinApplication>().with(ExampleWebConfig::class, ExampleFilterConfig::class).run().applicationContext
assertThat(context.getBean<MockServletWebServerFactory>()).isNotNull()
assertThat(context.getBean<MockFilter>()).isNotNull()
} }
@Test @Test
@ -91,4 +99,14 @@ class SpringApplicationExtensionsTests {
} }
@Configuration(proxyBeanMethods = false)
internal open class ExampleFilterConfig {
@Bean
open fun filter(): MockFilter {
return MockFilter()
}
}
} }

Loading…
Cancel
Save