Migrate to AssertJ in Kotlin tests
Closes gh-23475
This commit is contained in:
@@ -16,7 +16,7 @@
|
||||
|
||||
package org.springframework.core
|
||||
|
||||
import org.junit.jupiter.api.Assertions.assertEquals
|
||||
import org.assertj.core.api.Assertions.assertThat
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
class KotlinDefaultParameterNameDiscovererTests {
|
||||
@@ -31,6 +31,6 @@ class KotlinDefaultParameterNameDiscovererTests {
|
||||
fun getParameterNamesOnEnum() {
|
||||
val constructor = MyEnum::class.java.declaredConstructors[0]
|
||||
val actualParams = parameterNameDiscoverer.getParameterNames(constructor)
|
||||
assertEquals(2, actualParams!!.size)
|
||||
assertThat(actualParams!!.size).isEqualTo(2)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
|
||||
package org.springframework.core
|
||||
|
||||
import org.junit.jupiter.api.Assertions.assertEquals
|
||||
import org.assertj.core.api.Assertions.assertThat
|
||||
import org.junit.jupiter.api.Test
|
||||
import org.springframework.core.GenericTypeResolver.resolveReturnTypeArgument
|
||||
import java.lang.reflect.Method
|
||||
@@ -31,14 +31,14 @@ class KotlinGenericTypeResolverTests {
|
||||
|
||||
@Test
|
||||
fun methodReturnTypes() {
|
||||
assertEquals(Integer::class.java, resolveReturnTypeArgument(findMethod(MyTypeWithMethods::class.java, "integer")!!,
|
||||
MyInterfaceType::class.java))
|
||||
assertEquals(String::class.java, resolveReturnTypeArgument(findMethod(MyTypeWithMethods::class.java, "string")!!,
|
||||
MyInterfaceType::class.java))
|
||||
assertEquals(null, resolveReturnTypeArgument(findMethod(MyTypeWithMethods::class.java, "raw")!!,
|
||||
MyInterfaceType::class.java))
|
||||
assertEquals(null, resolveReturnTypeArgument(findMethod(MyTypeWithMethods::class.java, "object")!!,
|
||||
MyInterfaceType::class.java))
|
||||
assertThat(resolveReturnTypeArgument(findMethod(MyTypeWithMethods::class.java, "integer")!!,
|
||||
MyInterfaceType::class.java)).isEqualTo(Integer::class.java)
|
||||
assertThat(resolveReturnTypeArgument(findMethod(MyTypeWithMethods::class.java, "string")!!,
|
||||
MyInterfaceType::class.java)).isEqualTo(String::class.java)
|
||||
assertThat(resolveReturnTypeArgument(findMethod(MyTypeWithMethods::class.java, "raw")!!,
|
||||
MyInterfaceType::class.java)).isNull()
|
||||
assertThat(resolveReturnTypeArgument(findMethod(MyTypeWithMethods::class.java, "object")!!,
|
||||
MyInterfaceType::class.java)).isNull()
|
||||
}
|
||||
|
||||
private fun findMethod(clazz: Class<*>, name: String): Method? =
|
||||
|
||||
@@ -16,9 +16,7 @@
|
||||
|
||||
package org.springframework.core
|
||||
|
||||
import org.junit.jupiter.api.Assertions.assertEquals
|
||||
import org.junit.jupiter.api.Assertions.assertFalse
|
||||
import org.junit.jupiter.api.Assertions.assertTrue
|
||||
import org.assertj.core.api.Assertions.assertThat
|
||||
import org.junit.jupiter.api.Test
|
||||
import java.lang.reflect.Method
|
||||
import java.lang.reflect.TypeVariable
|
||||
@@ -49,59 +47,58 @@ class KotlinMethodParameterTests {
|
||||
|
||||
@Test
|
||||
fun `Method parameter nullability`() {
|
||||
assertTrue(MethodParameter(nullableMethod, 0).isOptional)
|
||||
assertFalse(MethodParameter(nonNullableMethod, 0).isOptional)
|
||||
assertThat(MethodParameter(nullableMethod, 0).isOptional).isTrue()
|
||||
assertThat(MethodParameter(nonNullableMethod, 0).isOptional).isFalse()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Method return type nullability`() {
|
||||
assertTrue(MethodParameter(nullableMethod, -1).isOptional)
|
||||
assertFalse(MethodParameter(nonNullableMethod, -1).isOptional)
|
||||
assertThat(MethodParameter(nullableMethod, -1).isOptional).isTrue()
|
||||
assertThat(MethodParameter(nonNullableMethod, -1).isOptional).isFalse()
|
||||
}
|
||||
|
||||
@Test // SPR-17222
|
||||
fun `Inner class constructor`() {
|
||||
assertFalse(MethodParameter(innerClassConstructor, 0).isOptional)
|
||||
|
||||
assertFalse(MethodParameter(innerClassWithParametersConstructor, 0).isOptional)
|
||||
assertFalse(MethodParameter(innerClassWithParametersConstructor, 1).isOptional)
|
||||
assertTrue(MethodParameter(innerClassWithParametersConstructor, 2).isOptional)
|
||||
assertThat(MethodParameter(innerClassConstructor, 0).isOptional).isFalse()
|
||||
assertThat(MethodParameter(innerClassWithParametersConstructor, 0).isOptional).isFalse()
|
||||
assertThat(MethodParameter(innerClassWithParametersConstructor, 1).isOptional).isFalse()
|
||||
assertThat(MethodParameter(innerClassWithParametersConstructor, 2).isOptional).isTrue()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Regular class constructor`() {
|
||||
assertFalse(MethodParameter(regularClassConstructor, 0).isOptional)
|
||||
assertTrue(MethodParameter(regularClassConstructor, 1).isOptional)
|
||||
assertThat(MethodParameter(regularClassConstructor, 0).isOptional).isFalse()
|
||||
assertThat(MethodParameter(regularClassConstructor, 1).isOptional).isTrue()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Suspending function return type`() {
|
||||
assertEquals(Number::class.java, returnParameterType("suspendFun"))
|
||||
assertEquals(Number::class.java, returnGenericParameterType("suspendFun"))
|
||||
assertThat(returnParameterType("suspendFun")).isEqualTo(Number::class.java)
|
||||
assertThat(returnGenericParameterType("suspendFun")).isEqualTo(Number::class.java)
|
||||
|
||||
assertEquals(Producer::class.java, returnParameterType("suspendFun2"))
|
||||
assertEquals("org.springframework.core.Producer<? extends java.lang.Number>", returnGenericParameterTypeName("suspendFun2"))
|
||||
assertThat(returnParameterType("suspendFun2")).isEqualTo(Producer::class.java)
|
||||
assertThat(returnGenericParameterTypeName("suspendFun2")).isEqualTo("org.springframework.core.Producer<? extends java.lang.Number>")
|
||||
|
||||
assertEquals(Wrapper::class.java, returnParameterType("suspendFun3"))
|
||||
assertEquals("org.springframework.core.Wrapper<java.lang.Number>", returnGenericParameterTypeName("suspendFun3"))
|
||||
assertThat(returnParameterType("suspendFun3")).isEqualTo(Wrapper::class.java)
|
||||
assertThat(returnGenericParameterTypeName("suspendFun3")).isEqualTo("org.springframework.core.Wrapper<java.lang.Number>")
|
||||
|
||||
assertEquals(Consumer::class.java, returnParameterType("suspendFun4"))
|
||||
assertEquals("org.springframework.core.Consumer<? super java.lang.Number>", returnGenericParameterTypeName("suspendFun4"))
|
||||
assertThat(returnParameterType("suspendFun4")).isEqualTo(Consumer::class.java)
|
||||
assertThat(returnGenericParameterTypeName("suspendFun4")).isEqualTo("org.springframework.core.Consumer<? super java.lang.Number>")
|
||||
|
||||
assertEquals(Producer::class.java, returnParameterType("suspendFun5"))
|
||||
assertTrue(returnGenericParameterType("suspendFun5") is TypeVariable<*>)
|
||||
assertEquals("org.springframework.core.Producer<? extends java.lang.Number>", returnGenericParameterTypeBoundName("suspendFun5"))
|
||||
assertThat(returnParameterType("suspendFun5")).isEqualTo(Producer::class.java)
|
||||
assertThat(returnGenericParameterType("suspendFun5")).isInstanceOf(TypeVariable::class.java)
|
||||
assertThat(returnGenericParameterTypeBoundName("suspendFun5")).isEqualTo("org.springframework.core.Producer<? extends java.lang.Number>")
|
||||
|
||||
assertEquals(Wrapper::class.java, returnParameterType("suspendFun6"))
|
||||
assertTrue(returnGenericParameterType("suspendFun6") is TypeVariable<*>)
|
||||
assertEquals("org.springframework.core.Wrapper<java.lang.Number>", returnGenericParameterTypeBoundName("suspendFun6"))
|
||||
assertThat(returnParameterType("suspendFun6")).isEqualTo(Wrapper::class.java)
|
||||
assertThat(returnGenericParameterType("suspendFun6")).isInstanceOf(TypeVariable::class.java)
|
||||
assertThat(returnGenericParameterTypeBoundName("suspendFun6")).isEqualTo("org.springframework.core.Wrapper<java.lang.Number>")
|
||||
|
||||
assertEquals(Consumer::class.java, returnParameterType("suspendFun7"))
|
||||
assertTrue(returnGenericParameterType("suspendFun7") is TypeVariable<*>)
|
||||
assertEquals("org.springframework.core.Consumer<? super java.lang.Number>", returnGenericParameterTypeBoundName("suspendFun7"))
|
||||
assertThat(returnParameterType("suspendFun7")).isEqualTo(Consumer::class.java)
|
||||
assertThat(returnGenericParameterType("suspendFun7")).isInstanceOf(TypeVariable::class.java)
|
||||
assertThat(returnGenericParameterTypeBoundName("suspendFun7")).isEqualTo("org.springframework.core.Consumer<? super java.lang.Number>")
|
||||
|
||||
assertEquals(Object::class.java, returnParameterType("suspendFun8"))
|
||||
assertEquals(Object::class.java, returnGenericParameterType("suspendFun8"))
|
||||
assertThat(returnParameterType("suspendFun8")).isEqualTo(Object::class.java)
|
||||
assertThat(returnGenericParameterType("suspendFun8")).isEqualTo(Object::class.java)
|
||||
}
|
||||
|
||||
private fun returnParameterType(funName: String) = returnMethodParameter(funName).parameterType
|
||||
|
||||
@@ -17,16 +17,13 @@
|
||||
package org.springframework.core
|
||||
|
||||
import kotlinx.coroutines.Deferred
|
||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||
import kotlinx.coroutines.GlobalScope
|
||||
import kotlinx.coroutines.async
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.flow
|
||||
import kotlinx.coroutines.flow.toList
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import org.junit.jupiter.api.Assertions.assertEquals
|
||||
import org.junit.jupiter.api.Assertions.assertTrue
|
||||
import org.junit.jupiter.api.Assertions.fail
|
||||
import org.assertj.core.api.Assertions.assertThat
|
||||
import org.junit.jupiter.api.Test
|
||||
import org.reactivestreams.Publisher
|
||||
import reactor.core.publisher.Flux
|
||||
@@ -35,7 +32,6 @@ import reactor.test.StepVerifier
|
||||
import java.time.Duration
|
||||
import kotlin.reflect.KClass
|
||||
|
||||
@ExperimentalCoroutinesApi
|
||||
class KotlinReactiveAdapterRegistryTests {
|
||||
|
||||
private val registry = ReactiveAdapterRegistry.getSharedInstance()
|
||||
@@ -44,16 +40,16 @@ class KotlinReactiveAdapterRegistryTests {
|
||||
fun deferredToPublisher() {
|
||||
val source = GlobalScope.async { 1 }
|
||||
val target: Publisher<Int> = getAdapter(Deferred::class).toPublisher(source)
|
||||
assertTrue(target is Mono<*>, "Expected Mono Publisher: " + target.javaClass.name)
|
||||
assertEquals(1, (target as Mono<Int>).block(Duration.ofMillis(1000)))
|
||||
assertThat(target).isInstanceOf(Mono::class.java)
|
||||
assertThat((target as Mono<Int>).block(Duration.ofMillis(1000))).isEqualTo(1)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun publisherToDeferred() {
|
||||
val source = Mono.just(1)
|
||||
val target = getAdapter(Deferred::class).fromPublisher(source)
|
||||
assertTrue(target is Deferred<*>)
|
||||
assertEquals(1, runBlocking { (target as Deferred<*>).await() })
|
||||
assertThat(target).isInstanceOf(Deferred::class.java)
|
||||
assertThat(runBlocking { (target as Deferred<*>).await() }).isEqualTo(1)
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -64,7 +60,7 @@ class KotlinReactiveAdapterRegistryTests {
|
||||
emit(3)
|
||||
}
|
||||
val target: Publisher<Int> = getAdapter(Flow::class).toPublisher(source)
|
||||
assertTrue(target is Flux<*>, "Expected Flux Publisher: " + target.javaClass.name)
|
||||
assertThat(target).isInstanceOf(Flux::class.java)
|
||||
StepVerifier.create(target)
|
||||
.expectNext(1)
|
||||
.expectNext(2)
|
||||
@@ -76,12 +72,8 @@ class KotlinReactiveAdapterRegistryTests {
|
||||
fun publisherToFlow() {
|
||||
val source = Flux.just(1, 2, 3)
|
||||
val target = getAdapter(Flow::class).fromPublisher(source)
|
||||
if (target is Flow<*>) {
|
||||
assertEquals(listOf(1, 2, 3), runBlocking { target.toList() })
|
||||
}
|
||||
else {
|
||||
fail()
|
||||
}
|
||||
assertThat(target).isInstanceOf(Flow::class.java)
|
||||
assertThat(runBlocking { (target as Flow<*>).toList() }).contains(1, 2, 3)
|
||||
}
|
||||
|
||||
private fun getAdapter(reactiveType: KClass<*>): ReactiveAdapter {
|
||||
|
||||
@@ -16,10 +16,9 @@
|
||||
|
||||
package org.springframework.core
|
||||
|
||||
import org.assertj.core.api.Assertions.assertThat
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
import org.hamcrest.CoreMatchers.`is`
|
||||
import org.hamcrest.MatcherAssert.assertThat
|
||||
import org.springframework.util.ReflectionUtils
|
||||
|
||||
/**
|
||||
@@ -33,21 +32,21 @@ class KotlinReflectionParameterNameDiscovererTests {
|
||||
fun getParameterNamesOnInterface() {
|
||||
val method = ReflectionUtils.findMethod(MessageService::class.java,"sendMessage", String::class.java)!!
|
||||
val actualParams = parameterNameDiscoverer.getParameterNames(method)
|
||||
assertThat(actualParams, `is`(arrayOf("message")))
|
||||
assertThat(actualParams).contains("message")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun getParameterNamesOnClass() {
|
||||
val method = ReflectionUtils.findMethod(MessageServiceImpl::class.java,"sendMessage", String::class.java)!!
|
||||
val actualParams = parameterNameDiscoverer.getParameterNames(method)
|
||||
assertThat(actualParams, `is`(arrayOf("message")))
|
||||
assertThat(actualParams).contains("message")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun getParameterNamesOnExtensionMethod() {
|
||||
val method = ReflectionUtils.findMethod(UtilityClass::class.java, "identity", String::class.java)!!
|
||||
val actualParams = parameterNameDiscoverer.getParameterNames(method)!!
|
||||
assertThat(actualParams, `is`(arrayOf("\$receiver")))
|
||||
assertThat(actualParams).contains("\$receiver")
|
||||
}
|
||||
|
||||
interface MessageService {
|
||||
|
||||
Reference in New Issue
Block a user