Skip to content

Commit bc01eeb

Browse files
committed
Leverage JUnit 6 suspending function support
Closes spring-projectsgh-36215
1 parent 77c24b2 commit bc01eeb

23 files changed

Lines changed: 288 additions & 540 deletions

File tree

integration-tests/src/test/kotlin/org/springframework/aop/framework/autoproxy/AspectJAutoProxyInterceptorKotlinIntegrationTests.kt

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package org.springframework.aop.framework.autoproxy
1818

1919
import kotlinx.coroutines.delay
20-
import kotlinx.coroutines.runBlocking
2120
import org.aopalliance.intercept.MethodInterceptor
2221
import org.aopalliance.intercept.MethodInvocation
2322
import org.aspectj.lang.ProceedingJoinPoint
@@ -73,43 +72,37 @@ class AspectJAutoProxyInterceptorKotlinIntegrationTests(
7372
}
7473

7574
@Test
76-
fun `Multiple interceptors with suspending function`() {
75+
suspend fun `Multiple interceptors with suspending function`() {
7776
assertThat(firstAdvisor.interceptor.invocations).isEmpty()
7877
assertThat(secondAdvisor.interceptor.invocations).isEmpty()
7978
val value = "Hello!"
80-
runBlocking {
81-
assertThat(echo.suspendingEcho(value)).isEqualTo(value)
82-
}
79+
assertThat(echo.suspendingEcho(value)).isEqualTo(value)
8380
assertThat(firstAdvisor.interceptor.invocations).singleElement().matches { Mono::class.java.isAssignableFrom(it) }
8481
assertThat(secondAdvisor.interceptor.invocations).singleElement().matches { Mono::class.java.isAssignableFrom(it) }
8582
}
8683

8784
@Test // gh-33095
88-
fun `Aspect and reactive transactional with suspending function`() {
85+
suspend fun `Aspect and reactive transactional with suspending function`() {
8986
assertThat(countingAspect.counter).isZero()
9087
assertThat(reactiveTransactionManager.commits).isZero()
9188
val value = "Hello!"
92-
runBlocking {
93-
assertThat(echo.suspendingTransactionalEcho(value)).isEqualTo(value)
94-
}
89+
assertThat(echo.suspendingTransactionalEcho(value)).isEqualTo(value)
9590
assertThat(countingAspect.counter).`as`("aspect applied").isOne()
9691
assertThat(reactiveTransactionManager.begun).isOne()
9792
assertThat(reactiveTransactionManager.commits).`as`("transactional applied").isOne()
9893
}
9994

10095
@Test // gh-33210
101-
fun `Aspect and cacheable with suspending function`() {
96+
suspend fun `Aspect and cacheable with suspending function`() {
10297
assertThat(countingAspect.counter).isZero()
10398
val value = "Hello!"
104-
runBlocking {
105-
assertThat(echo.suspendingCacheableEcho(value)).isEqualTo("$value 0")
106-
assertThat(echo.suspendingCacheableEcho(value)).isEqualTo("$value 0")
107-
assertThat(echo.suspendingCacheableEcho(value)).isEqualTo("$value 0")
108-
assertThat(countingAspect.counter).`as`("aspect applied once").isOne()
109-
110-
assertThat(echo.suspendingCacheableEcho("$value bis")).isEqualTo("$value bis 1")
111-
assertThat(echo.suspendingCacheableEcho("$value bis")).isEqualTo("$value bis 1")
112-
}
99+
assertThat(echo.suspendingCacheableEcho(value)).isEqualTo("$value 0")
100+
assertThat(echo.suspendingCacheableEcho(value)).isEqualTo("$value 0")
101+
assertThat(echo.suspendingCacheableEcho(value)).isEqualTo("$value 0")
102+
assertThat(countingAspect.counter).`as`("aspect applied once").isOne()
103+
104+
assertThat(echo.suspendingCacheableEcho("$value bis")).isEqualTo("$value bis 1")
105+
assertThat(echo.suspendingCacheableEcho("$value bis")).isEqualTo("$value bis 1")
113106
assertThat(countingAspect.counter).`as`("aspect applied once per key").isEqualTo(2)
114107
}
115108

spring-aop/src/test/kotlin/org/springframework/aop/framework/CoroutinesUtilsTests.kt

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package org.springframework.aop.framework
1919
import kotlinx.coroutines.CoroutineName
2020
import kotlinx.coroutines.flow.Flow
2121
import kotlinx.coroutines.flow.toList
22-
import kotlinx.coroutines.runBlocking
2322
import org.assertj.core.api.Assertions.assertThat
2423
import org.junit.jupiter.api.Test
2524
import reactor.core.publisher.Flux
@@ -37,39 +36,31 @@ class CoroutinesUtilsTests {
3736
fun awaitSingleNonNullValue() {
3837
val value = "foo"
3938
val continuation = Continuation<Any>(CoroutineName("test")) { }
40-
runBlocking {
41-
assertThat(CoroutinesUtils.awaitSingleOrNull(value, continuation)).isEqualTo(value)
42-
}
39+
assertThat(CoroutinesUtils.awaitSingleOrNull(value, continuation)).isEqualTo(value)
4340
}
4441

4542
@Test
4643
fun awaitSingleNullValue() {
4744
val value = null
4845
val continuation = Continuation<Any>(CoroutineName("test")) { }
49-
runBlocking {
50-
assertThat(CoroutinesUtils.awaitSingleOrNull(value, continuation)).isNull()
51-
}
46+
assertThat(CoroutinesUtils.awaitSingleOrNull(value, continuation)).isNull()
5247
}
5348

5449
@Test
5550
fun awaitSingleMonoValue() {
5651
val value = "foo"
5752
val continuation = Continuation<Any>(CoroutineName("test")) { }
58-
runBlocking {
59-
assertThat(CoroutinesUtils.awaitSingleOrNull(Mono.just(value), continuation)).isEqualTo(value)
60-
}
53+
assertThat(CoroutinesUtils.awaitSingleOrNull(Mono.just(value), continuation)).isEqualTo(value)
6154
}
6255

6356
@Test
6457
@Suppress("UNCHECKED_CAST")
65-
fun flow() {
58+
suspend fun flow() {
6659
val value1 = "foo"
6760
val value2 = "bar"
6861
val values = Flux.just(value1, value2)
6962
val flow = CoroutinesUtils.asFlow(values) as Flow<String>
70-
runBlocking {
71-
assertThat(flow.toList()).containsExactly(value1, value2)
72-
}
63+
assertThat(flow.toList()).containsExactly(value1, value2)
7364
}
7465

7566
}

spring-context/src/test/kotlin/org/springframework/cache/KotlinCacheReproTests.kt

Lines changed: 38 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package org.springframework.cache
1818

19-
import kotlinx.coroutines.runBlocking
2019
import org.assertj.core.api.Assertions.assertThat
2120
import org.junit.jupiter.api.Test
2221
import org.springframework.beans.testfixture.beans.TestBean
@@ -33,51 +32,47 @@ import org.springframework.context.annotation.Configuration
3332
class KotlinCacheReproTests {
3433

3534
@Test
36-
fun spr14235AdaptsToSuspendingFunction() {
37-
runBlocking {
38-
val context = AnnotationConfigApplicationContext(
39-
Spr14235Config::class.java,
40-
Spr14235SuspendingService::class.java
41-
)
42-
val bean = context.getBean(Spr14235SuspendingService::class.java)
43-
val cache = context.getBean(CacheManager::class.java).getCache("itemCache")!!
44-
val tb: TestBean = bean.findById("tb1")
45-
assertThat(bean.findById("tb1")).isSameAs(tb)
46-
assertThat(cache["tb1"]!!.get()).isSameAs(tb)
47-
bean.clear()
48-
val tb2: TestBean = bean.findById("tb1")
49-
assertThat(tb2).isNotSameAs(tb)
50-
assertThat(cache["tb1"]!!.get()).isSameAs(tb2)
51-
bean.clear()
52-
bean.insertItem(tb)
53-
assertThat(bean.findById("tb1")).isSameAs(tb)
54-
assertThat(cache["tb1"]!!.get()).isSameAs(tb)
55-
context.close()
56-
}
35+
suspend fun spr14235AdaptsToSuspendingFunction() {
36+
val context = AnnotationConfigApplicationContext(
37+
Spr14235Config::class.java,
38+
Spr14235SuspendingService::class.java
39+
)
40+
val bean = context.getBean(Spr14235SuspendingService::class.java)
41+
val cache = context.getBean(CacheManager::class.java).getCache("itemCache")!!
42+
val tb: TestBean = bean.findById("tb1")
43+
assertThat(bean.findById("tb1")).isSameAs(tb)
44+
assertThat(cache["tb1"]!!.get()).isSameAs(tb)
45+
bean.clear()
46+
val tb2: TestBean = bean.findById("tb1")
47+
assertThat(tb2).isNotSameAs(tb)
48+
assertThat(cache["tb1"]!!.get()).isSameAs(tb2)
49+
bean.clear()
50+
bean.insertItem(tb)
51+
assertThat(bean.findById("tb1")).isSameAs(tb)
52+
assertThat(cache["tb1"]!!.get()).isSameAs(tb)
53+
context.close()
5754
}
5855

5956
@Test
60-
fun spr14235AdaptsToSuspendingFunctionWithSync() {
61-
runBlocking {
62-
val context = AnnotationConfigApplicationContext(
63-
Spr14235Config::class.java,
64-
Spr14235SuspendingServiceSync::class.java
65-
)
66-
val bean = context.getBean(Spr14235SuspendingServiceSync::class.java)
67-
val cache = context.getBean(CacheManager::class.java).getCache("itemCache")!!
68-
val tb = bean.findById("tb1")
69-
assertThat(bean.findById("tb1")).isSameAs(tb)
70-
assertThat(cache["tb1"]!!.get()).isSameAs(tb)
71-
cache.clear()
72-
val tb2 = bean.findById("tb1")
73-
assertThat(tb2).isNotSameAs(tb)
74-
assertThat(cache["tb1"]!!.get()).isSameAs(tb2)
75-
cache.clear()
76-
bean.insertItem(tb)
77-
assertThat(bean.findById("tb1")).isSameAs(tb)
78-
assertThat(cache["tb1"]!!.get()).isSameAs(tb)
79-
context.close()
80-
}
57+
suspend fun spr14235AdaptsToSuspendingFunctionWithSync() {
58+
val context = AnnotationConfigApplicationContext(
59+
Spr14235Config::class.java,
60+
Spr14235SuspendingServiceSync::class.java
61+
)
62+
val bean = context.getBean(Spr14235SuspendingServiceSync::class.java)
63+
val cache = context.getBean(CacheManager::class.java).getCache("itemCache")!!
64+
val tb = bean.findById("tb1")
65+
assertThat(bean.findById("tb1")).isSameAs(tb)
66+
assertThat(cache["tb1"]!!.get()).isSameAs(tb)
67+
cache.clear()
68+
val tb2 = bean.findById("tb1")
69+
assertThat(tb2).isNotSameAs(tb)
70+
assertThat(cache["tb1"]!!.get()).isSameAs(tb2)
71+
cache.clear()
72+
bean.insertItem(tb)
73+
assertThat(bean.findById("tb1")).isSameAs(tb)
74+
assertThat(cache["tb1"]!!.get()).isSameAs(tb)
75+
context.close()
8176
}
8277

8378
@Test

spring-context/src/test/kotlin/org/springframework/validation/beanvalidation/MethodValidationKotlinTests.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class MethodValidationKotlinTests {
4949
}
5050

5151
@Test
52-
fun coroutinesParameterValidation() = runBlocking<Unit> {
52+
suspend fun coroutinesParameterValidation() {
5353
val bean = MyValidCoroutinesBean()
5454
val proxyFactory = ProxyFactory(bean)
5555
val validator = LocalValidatorFactoryBean()

0 commit comments

Comments
 (0)