From 8579ae66fcfc34160795ebbc212fc884eba5b4d0 Mon Sep 17 00:00:00 2001 From: Sebastien Deleuze Date: Fri, 9 Jun 2017 00:47:24 +0300 Subject: [PATCH] Add comprehensive tests of Kotlin extensions This commit also removes extensions hidden by Java API (varargs). --- build.gradle | 1 + .../beans/factory/BeanFactoryExtensions.kt | 10 - .../factory/BeanFactoryExtensionsTests.kt | 58 ++++++ .../ListableBeanFactoryExtensionsTests.kt | 134 +++++++++++++ .../annotation/KotlinAutowiredTests.kt | 7 +- ...ConfigApplicationContextExtensionsTests.kt | 26 +++ ...enericApplicationContextExtensionsTests.kt | 9 +- .../ui/ModelExtensionsTests.kt | 5 + .../ui/ModelMapExtensionsTests.kt | 6 + .../jdbc/core/JdbcOperationsExtensions.kt | 28 --- .../MapSqlParameterSourceExtensions.kt | 2 +- .../core/JdbcOperationsExtensionsTests.kt | 178 +++++++----------- .../MapSqlParameterSourceExtensionsTests.kt | 2 + .../client/RestOperationsExtensionsTests.kt | 154 +++++++++++++++ .../function/BodyExtractorsExtensions.kt | 12 +- .../function/BodyInsertersExtensions.kt | 4 +- .../function/BodyExtractorsExtensionsTests.kt | 38 ++++ .../function/BodyInsertersExtensionsTests.kt | 32 ++++ .../client/ClientResponseExtensionsTests.kt | 47 +++++ .../client/WebClientExtensionsTests.kt | 84 +++++++++ .../server/RouterFunctionExtensionsTests.kt | 5 + .../server/ServerRequestExtensionsTests.kt | 50 +++++ .../server/ServerResponseExtensionsTests.kt | 32 ++++ 23 files changed, 766 insertions(+), 158 deletions(-) create mode 100644 spring-beans/src/test/kotlin/org/springframework/beans/factory/BeanFactoryExtensionsTests.kt create mode 100644 spring-beans/src/test/kotlin/org/springframework/beans/factory/ListableBeanFactoryExtensionsTests.kt create mode 100644 spring-context/src/test/kotlin/org/springframework/context/annotation/AnnotationConfigApplicationContextExtensionsTests.kt create mode 100644 spring-web/src/test/kotlin/org/springframework/web/client/RestOperationsExtensionsTests.kt create mode 100644 spring-webflux/src/test/kotlin/org/springframework/web/reactive/function/BodyExtractorsExtensionsTests.kt create mode 100644 spring-webflux/src/test/kotlin/org/springframework/web/reactive/function/BodyInsertersExtensionsTests.kt create mode 100644 spring-webflux/src/test/kotlin/org/springframework/web/reactive/function/client/ClientResponseExtensionsTests.kt create mode 100644 spring-webflux/src/test/kotlin/org/springframework/web/reactive/function/client/WebClientExtensionsTests.kt create mode 100644 spring-webflux/src/test/kotlin/org/springframework/web/reactive/function/server/ServerRequestExtensionsTests.kt create mode 100644 spring-webflux/src/test/kotlin/org/springframework/web/reactive/function/server/ServerResponseExtensionsTests.kt diff --git a/build.gradle b/build.gradle index 3cf6da0e34b..a533f39efe7 100644 --- a/build.gradle +++ b/build.gradle @@ -184,6 +184,7 @@ configure(allprojects) { project -> testCompile("org.mockito:mockito-core:2.6.1") { exclude group:'org.hamcrest', module:'hamcrest-core' } + testCompile("com.nhaarman:mockito-kotlin:1.5.0") testCompile("org.hamcrest:hamcrest-all:${hamcrestVersion}") testRuntime("org.apache.logging.log4j:log4j-core:${log4jVersion}") } diff --git a/spring-beans/src/main/kotlin/org/springframework/beans/factory/BeanFactoryExtensions.kt b/spring-beans/src/main/kotlin/org/springframework/beans/factory/BeanFactoryExtensions.kt index ed5f8557494..0b3c9b435ca 100644 --- a/spring-beans/src/main/kotlin/org/springframework/beans/factory/BeanFactoryExtensions.kt +++ b/spring-beans/src/main/kotlin/org/springframework/beans/factory/BeanFactoryExtensions.kt @@ -19,16 +19,6 @@ fun BeanFactory.getBean(requiredType: KClass): T = getBean(required */ inline fun BeanFactory.getBean(): T = getBean(T::class.java) -/** - * Extension for [BeanFactory.getBean] providing a [KClass] based variant. - * - * @see BeanFactory.getBean(String, Class) - * @author Sebastien Deleuze - * @since 5.0 - */ -fun BeanFactory.getBean(name: String, requiredType: KClass): T = - getBean(name, requiredType.java) - /** * Extension for [BeanFactory.getBean] providing a `getBean("foo")` variant. * diff --git a/spring-beans/src/test/kotlin/org/springframework/beans/factory/BeanFactoryExtensionsTests.kt b/spring-beans/src/test/kotlin/org/springframework/beans/factory/BeanFactoryExtensionsTests.kt new file mode 100644 index 00000000000..f712383a36e --- /dev/null +++ b/spring-beans/src/test/kotlin/org/springframework/beans/factory/BeanFactoryExtensionsTests.kt @@ -0,0 +1,58 @@ +package org.springframework.beans.factory + +import org.junit.Test +import org.junit.runner.RunWith +import org.mockito.Answers +import org.mockito.Mock +import org.mockito.Mockito +import org.mockito.Mockito.verify +import org.mockito.junit.MockitoJUnitRunner + +/** + * Mock object based tests for BeanFactory Kotlin extensions + * + * @author Sebastien Deleuze + */ +@RunWith(MockitoJUnitRunner::class) +class BeanFactoryExtensionsTests { + + @Mock(answer = Answers.RETURNS_MOCKS) + lateinit var bf: BeanFactory + + @Test + fun `getBean with KClass`() { + bf.getBean(Foo::class) + verify(bf, Mockito.times(1)).getBean(Foo::class.java) + } + + @Test + fun `getBean with reified type parameters`() { + bf.getBean() + verify(bf, Mockito.times(1)).getBean(Foo::class.java) + } + + @Test + fun `getBean with String and reified type parameters`() { + val name = "foo" + bf.getBean(name) + verify(bf, Mockito.times(1)).getBean(name, Foo::class.java) + } + + @Test + fun `getBean with KClass and varargs`() { + val arg1 = "arg1" + val arg2 = "arg2" + bf.getBean(Foo::class, arg1, arg2) + verify(bf, Mockito.times(1)).getBean(Foo::class.java, arg1, arg2) + } + + @Test + fun `getBean with reified type parameters and varargs`() { + val arg1 = "arg1" + val arg2 = "arg2" + bf.getBean(arg1, arg2) + verify(bf, Mockito.times(1)).getBean(Foo::class.java, arg1, arg2) + } + + class Foo +} diff --git a/spring-beans/src/test/kotlin/org/springframework/beans/factory/ListableBeanFactoryExtensionsTests.kt b/spring-beans/src/test/kotlin/org/springframework/beans/factory/ListableBeanFactoryExtensionsTests.kt new file mode 100644 index 00000000000..9bbb3c87ebe --- /dev/null +++ b/spring-beans/src/test/kotlin/org/springframework/beans/factory/ListableBeanFactoryExtensionsTests.kt @@ -0,0 +1,134 @@ +package org.springframework.beans.factory + +import org.junit.Test +import org.junit.runner.RunWith +import org.mockito.Answers +import org.mockito.Mock +import org.mockito.Mockito +import org.mockito.junit.MockitoJUnitRunner + +/** + * Mock object based tests for ListableBeanFactory Kotlin extensions + * + * @author Sebastien Deleuze + */ +@RunWith(MockitoJUnitRunner::class) +class ListenableBeanFactoryExtensionsTests { + + @Mock(answer = Answers.RETURNS_MOCKS) + lateinit var lbf: ListableBeanFactory + + @Test + fun `getBeanNamesForType with KClass`() { + lbf.getBeanNamesForType(Foo::class) + Mockito.verify(lbf, Mockito.times(1)).getBeanNamesForType(Foo::class.java, true , true) + } + + @Test + fun `getBeanNamesForType with KClass and Boolean`() { + lbf.getBeanNamesForType(Foo::class, false) + Mockito.verify(lbf, Mockito.times(1)).getBeanNamesForType(Foo::class.java, false , true) + } + + @Test + fun `getBeanNamesForType with KClass, Boolean and Boolean`() { + lbf.getBeanNamesForType(Foo::class, false, false) + Mockito.verify(lbf, Mockito.times(1)).getBeanNamesForType(Foo::class.java, false , false) + } + + @Test + fun `getBeanNamesForType with reified type parameters`() { + lbf.getBeanNamesForType() + Mockito.verify(lbf, Mockito.times(1)).getBeanNamesForType(Foo::class.java, true , true) + } + + @Test + fun `getBeanNamesForType with reified type parameters and Boolean`() { + lbf.getBeanNamesForType(false) + Mockito.verify(lbf, Mockito.times(1)).getBeanNamesForType(Foo::class.java, false , true) + } + + @Test + fun `getBeanNamesForType with reified type parameters, Boolean and Boolean`() { + lbf.getBeanNamesForType(false, false) + Mockito.verify(lbf, Mockito.times(1)).getBeanNamesForType(Foo::class.java, false , false) + } + + @Test + fun `getBeansOfType with KClass`() { + lbf.getBeansOfType(Foo::class) + Mockito.verify(lbf, Mockito.times(1)).getBeansOfType(Foo::class.java, true , true) + } + + @Test + fun `getBeansOfType with KClass and Boolean`() { + lbf.getBeansOfType(Foo::class, false) + Mockito.verify(lbf, Mockito.times(1)).getBeansOfType(Foo::class.java, false , true) + } + + @Test + fun `getBeansOfType with KClass, Boolean and Boolean`() { + lbf.getBeansOfType(Foo::class, false, false) + Mockito.verify(lbf, Mockito.times(1)).getBeansOfType(Foo::class.java, false , false) + } + + @Test + fun `getBeansOfType with reified type parameters`() { + lbf.getBeansOfType() + Mockito.verify(lbf, Mockito.times(1)).getBeansOfType(Foo::class.java, true , true) + } + + @Test + fun `getBeansOfType with reified type parameters and Boolean`() { + lbf.getBeansOfType(false) + Mockito.verify(lbf, Mockito.times(1)).getBeansOfType(Foo::class.java, false , true) + } + + @Test + fun `getBeansOfType with reified type parameters, Boolean and Boolean`() { + lbf.getBeansOfType(false, false) + Mockito.verify(lbf, Mockito.times(1)).getBeansOfType(Foo::class.java, false , false) + } + + @Test + fun `getBeanNamesForAnnotation with KClass`() { + lbf.getBeanNamesForAnnotation(Bar::class) + Mockito.verify(lbf, Mockito.times(1)).getBeanNamesForAnnotation(Bar::class.java) + } + + @Test + fun `getBeanNamesForAnnotation with reified type parameters`() { + lbf.getBeanNamesForAnnotation() + Mockito.verify(lbf, Mockito.times(1)).getBeanNamesForAnnotation(Bar::class.java) + } + + @Test + fun `getBeansWithAnnotation with KClass`() { + lbf.getBeansWithAnnotation(Bar::class) + Mockito.verify(lbf, Mockito.times(1)).getBeansWithAnnotation(Bar::class.java) + } + + @Test + fun `getBeansWithAnnotation with reified type parameters`() { + lbf.getBeansWithAnnotation() + Mockito.verify(lbf, Mockito.times(1)).getBeansWithAnnotation(Bar::class.java) + } + + @Test + fun `findAnnotationOnBean with String and KClass`() { + val name = "bar" + lbf.findAnnotationOnBean(name, Bar::class) + Mockito.verify(lbf, Mockito.times(1)).findAnnotationOnBean(name, Bar::class.java) + } + + @Test + fun `findAnnotationOnBean with String and reified type parameters`() { + val name = "bar" + lbf.findAnnotationOnBean(name) + Mockito.verify(lbf, Mockito.times(1)).findAnnotationOnBean(name, Bar::class.java) + } + + class Foo + + annotation class Bar +} diff --git a/spring-beans/src/test/kotlin/org/springframework/beans/factory/annotation/KotlinAutowiredTests.kt b/spring-beans/src/test/kotlin/org/springframework/beans/factory/annotation/KotlinAutowiredTests.kt index 02920309111..2fb5725ee43 100644 --- a/spring-beans/src/test/kotlin/org/springframework/beans/factory/annotation/KotlinAutowiredTests.kt +++ b/spring-beans/src/test/kotlin/org/springframework/beans/factory/annotation/KotlinAutowiredTests.kt @@ -16,13 +16,8 @@ package org.springframework.beans.factory.annotation -import java.lang.reflect.Method - -import org.junit.Before import org.junit.Test -import org.springframework.beans.factory.annotation.Autowired -import org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor import org.springframework.beans.factory.support.DefaultListableBeanFactory import org.springframework.beans.factory.support.RootBeanDefinition import org.springframework.tests.sample.beans.TestBean @@ -30,7 +25,7 @@ import org.springframework.tests.sample.beans.TestBean import org.junit.Assert.* /** - * Tests for Kotlin support with [@Autowired]. + * Tests for Kotlin support with [Autowired]. * * @author Juergen Hoeller */ diff --git a/spring-context/src/test/kotlin/org/springframework/context/annotation/AnnotationConfigApplicationContextExtensionsTests.kt b/spring-context/src/test/kotlin/org/springframework/context/annotation/AnnotationConfigApplicationContextExtensionsTests.kt new file mode 100644 index 00000000000..aa193078c2f --- /dev/null +++ b/spring-context/src/test/kotlin/org/springframework/context/annotation/AnnotationConfigApplicationContextExtensionsTests.kt @@ -0,0 +1,26 @@ +package org.springframework.context.annotation + +import org.junit.Assert.assertNotNull +import org.junit.Test +import org.springframework.beans.factory.getBean +import org.springframework.context.support.registerBean + +/** + * Tests for [AnnotationConfigApplicationContext] Kotlin extensions + * + * @author Sebastien Deleuze + */ +class AnnotationConfigApplicationContextExtensionsTests { + + @Test + fun `Instantiate AnnotationConfigApplicationContext`() { + val applicationContext = AnnotationConfigApplicationContext { + registerBean() + } + applicationContext.refresh() + assertNotNull(applicationContext) + assertNotNull(applicationContext.getBean()) + } + + class Foo +} diff --git a/spring-context/src/test/kotlin/org/springframework/context/support/GenericApplicationContextExtensionsTests.kt b/spring-context/src/test/kotlin/org/springframework/context/support/GenericApplicationContextExtensionsTests.kt index ad1660c4ebc..e3d138744ce 100644 --- a/spring-context/src/test/kotlin/org/springframework/context/support/GenericApplicationContextExtensionsTests.kt +++ b/spring-context/src/test/kotlin/org/springframework/context/support/GenericApplicationContextExtensionsTests.kt @@ -4,6 +4,11 @@ import org.junit.Assert.assertNotNull import org.junit.Test import org.springframework.beans.factory.getBean +/** + * Tests for [GenericApplicationContext] Kotlin extensions + * + * @author Sebastien Deleuze + */ class GenericApplicationContextExtensionsTests { @Test @@ -69,8 +74,8 @@ class GenericApplicationContextExtensionsTests { assertNotNull(context.getBean()) } - internal class BeanA + class BeanA - internal class BeanB(val a: BeanA) + class BeanB(val a: BeanA) } diff --git a/spring-context/src/test/kotlin/org/springframework/ui/ModelExtensionsTests.kt b/spring-context/src/test/kotlin/org/springframework/ui/ModelExtensionsTests.kt index bd41714d0f7..45a17c83454 100644 --- a/spring-context/src/test/kotlin/org/springframework/ui/ModelExtensionsTests.kt +++ b/spring-context/src/test/kotlin/org/springframework/ui/ModelExtensionsTests.kt @@ -21,6 +21,11 @@ import org.junit.Assert.assertTrue import org.junit.Test +/** + * Tests for [Model] Kotlin extensions + * + * @author Sebastien Deleuze + */ class ModelExtensionsTests { @Test diff --git a/spring-context/src/test/kotlin/org/springframework/ui/ModelMapExtensionsTests.kt b/spring-context/src/test/kotlin/org/springframework/ui/ModelMapExtensionsTests.kt index 0b5bb99a8ab..1543721f213 100644 --- a/spring-context/src/test/kotlin/org/springframework/ui/ModelMapExtensionsTests.kt +++ b/spring-context/src/test/kotlin/org/springframework/ui/ModelMapExtensionsTests.kt @@ -21,6 +21,11 @@ import org.junit.Assert.assertTrue import org.junit.Test +/** + * Tests for [ModelMap] Kotlin extensions + * + * @author Sebastien Deleuze + */ class ModelMapExtensionsTests { @Test @@ -30,4 +35,5 @@ class ModelMapExtensionsTests { assertTrue(model.containsAttribute("foo")) assertEquals("bing", model["foo"]) } + } diff --git a/spring-jdbc/src/main/kotlin/org/springframework/jdbc/core/JdbcOperationsExtensions.kt b/spring-jdbc/src/main/kotlin/org/springframework/jdbc/core/JdbcOperationsExtensions.kt index d28fcf6a707..3c053797ada 100644 --- a/spring-jdbc/src/main/kotlin/org/springframework/jdbc/core/JdbcOperationsExtensions.kt +++ b/spring-jdbc/src/main/kotlin/org/springframework/jdbc/core/JdbcOperationsExtensions.kt @@ -91,15 +91,6 @@ fun JdbcOperations.queryForObject(sql: String, requiredType: KClass queryForObject(sql, requiredType.java, *args) -/** - * Extension for [JdbcOperations.queryForList] providing a [KClass] based variant. - * - * @author Mario Arias - * @since 5.0 - */ -fun JdbcOperations.queryForList(sql: String, elementType: KClass): List = - queryForList(sql, elementType.java) - /** * Extension for [JdbcOperations.queryForList] providing a `queryForList("...")` variant. * @@ -110,15 +101,6 @@ fun JdbcOperations.queryForList(sql: String, elementType: KClass): inline fun JdbcOperations.queryForList(sql: String): List = queryForList(sql, T::class.java) -/** - * Extension for [JdbcOperations.queryForList] providing a [KClass] based variant. - * - * @author Mario Arias - * @since 5.0 - */ -fun JdbcOperations.queryForList(sql: String, args: Array, argTypes: IntArray, elementType: KClass): List = - queryForList(sql, args, argTypes, elementType.java) - /** * Extension for [JdbcOperations.queryForList] providing a `queryForList("...", arrayOf(arg1, argN), intArray(type1, typeN))` variant * @@ -129,15 +111,6 @@ fun JdbcOperations.queryForList(sql: String, args: Array, arg inline fun JdbcOperations.queryForList(sql: String, args: Array, argTypes: IntArray): List = queryForList(sql, args, argTypes, T::class.java) -/** - * Extension for [JdbcOperations.queryForList] providing a [KClass] based variant. - * - * @author Mario Arias - * @since 5.0 - */ -fun JdbcOperations.queryForList(sql: String, args: Array, elementType: KClass): List = - queryForList(sql, args, elementType.java) - /** * Extension for [JdbcOperations.queryForList] providing a `queryForList("...", arrayOf(arg1, argN))` variant * @@ -174,4 +147,3 @@ fun JdbcOperations.query(sql: String, vararg args: Any, function: (ResultSet) -> */ fun JdbcOperations.query(sql: String, vararg args: Any, function: (ResultSet, Int) -> T): List = query(sql, RowMapper { rs, i -> function(rs, i) }, *args) - diff --git a/spring-jdbc/src/main/kotlin/org/springframework/jdbc/core/namedparam/MapSqlParameterSourceExtensions.kt b/spring-jdbc/src/main/kotlin/org/springframework/jdbc/core/namedparam/MapSqlParameterSourceExtensions.kt index b2e0e452d10..681aba568f5 100644 --- a/spring-jdbc/src/main/kotlin/org/springframework/jdbc/core/namedparam/MapSqlParameterSourceExtensions.kt +++ b/spring-jdbc/src/main/kotlin/org/springframework/jdbc/core/namedparam/MapSqlParameterSourceExtensions.kt @@ -56,4 +56,4 @@ operator fun MapSqlParameterSource.set(paramName: String, sqlType: Int, value: A */ operator fun MapSqlParameterSource.set(paramName: String, sqlType: Int, typeName: String, value: Any) { this.addValue(paramName, value, sqlType, typeName) -} \ No newline at end of file +} diff --git a/spring-jdbc/src/test/kotlin/org/springframework/jdbc/core/JdbcOperationsExtensionsTests.kt b/spring-jdbc/src/test/kotlin/org/springframework/jdbc/core/JdbcOperationsExtensionsTests.kt index 9f7cb441e94..40eaa716215 100644 --- a/spring-jdbc/src/test/kotlin/org/springframework/jdbc/core/JdbcOperationsExtensionsTests.kt +++ b/spring-jdbc/src/test/kotlin/org/springframework/jdbc/core/JdbcOperationsExtensionsTests.kt @@ -18,172 +18,140 @@ package org.springframework.jdbc.core import org.junit.Assert.assertEquals -import org.junit.Before -import org.junit.Rule import org.junit.Test -import org.junit.rules.ExpectedException -import org.mockito.ArgumentMatchers.anyString -import org.mockito.BDDMockito.given -import org.mockito.Mockito.mock +import org.junit.runner.RunWith +import org.mockito.Answers +import org.mockito.Mock +import org.mockito.Mockito.* +import org.mockito.junit.MockitoJUnitRunner import java.sql.* -import javax.sql.DataSource /** - * Mock object based tests for JdbcOperationsExtension + * Mock object based tests for [JdbcOperations] Kotlin extensions + * + * @author Mario Arias + * @author Sebastien Deleuze */ +@RunWith(MockitoJUnitRunner::class) class JdbcOperationsExtensionsTests { - @Rule - @JvmField - val thrown = ExpectedException.none()!! - - lateinit private var connection: Connection - lateinit private var dataSource: DataSource - lateinit private var preparedStatement: PreparedStatement - lateinit private var statement: Statement - lateinit private var resultSet: ResultSet - lateinit private var template: JdbcTemplate - lateinit private var callableStatement: CallableStatement - lateinit private var resultSetMetaData: ResultSetMetaData - - @Before - fun setup() { - connection = mock(Connection::class.java) - dataSource = mock(DataSource::class.java) - preparedStatement = mock(PreparedStatement::class.java) - statement = mock(Statement::class.java) - resultSet = mock(ResultSet::class.java) - callableStatement = mock(CallableStatement::class.java) - resultSetMetaData = mock(ResultSetMetaData::class.java) - template = JdbcTemplate(dataSource) - given(dataSource.connection).willReturn(connection) - given(connection.prepareStatement(anyString())).willReturn(preparedStatement) - given(preparedStatement.executeQuery()).willReturn(resultSet) - given(preparedStatement.executeQuery(anyString())).willReturn(resultSet) - given(preparedStatement.connection).willReturn(connection) - given(statement.connection).willReturn(connection) - given(statement.executeQuery(anyString())).willReturn(resultSet) - given(connection.prepareCall(anyString())).willReturn(callableStatement) - given(connection.createStatement()).willReturn(statement) - given(callableStatement.resultSet).willReturn(resultSet) - given(resultSetMetaData.columnCount).willReturn(1) - given(resultSetMetaData.getColumnName(1)).willReturn("age") - given(resultSet.metaData).willReturn(resultSetMetaData) - given(resultSet.next()).willReturn(true, false) - given(resultSet.getInt(1)).willReturn(22) - } - + @Mock(answer = Answers.RETURNS_MOCKS) + lateinit var template: JdbcTemplate @Test fun `queryForObject with KClass`() { - val i = template.queryForObject("select age from customer where id = 3", Int::class) - assertEquals(22, i) + val sql = "select age from customer where id = 3" + val requiredType = Int::class + template.queryForObject(sql, requiredType) + verify(template, times(1)).queryForObject(sql, requiredType) } @Test - fun `queryForObject with reified type`() { - val i: Int? = template.queryForObject("select age from customer where id = 3") - assertEquals(22, i) + fun `queryForObject with reified type parameters`() { + val sql = "select age from customer where id = 3" + template.queryForObject(sql) + verify(template, times(1)).queryForObject(sql, Integer::class.java) } @Test fun `queryForObject with RowMapper-like function`() { - val i = template.queryForObject("select age from customer where id = ?", 3) { rs, _ -> - rs.getInt(1) - } - assertEquals(22, i) + val sql = "select age from customer where id = ?" + template.queryForObject(sql, 3) { rs: ResultSet, _: Int -> rs.getInt(1) } + verify(template, times(1)).queryForObject(eq(sql), any>(), eq(3)) } @Test fun `queryForObject with argTypes`() { - val i = template.queryForObject("select age from customer where id = ?", arrayOf(3), - intArrayOf(JDBCType.INTEGER.vendorTypeNumber), Int::class) - assertEquals(22, i) + val sql = "select age from customer where id = ?" + val args = arrayOf(3) + val argTypes = intArrayOf(JDBCType.INTEGER.vendorTypeNumber) + val requiredType = Int::class + template.queryForObject(sql, args, argTypes, requiredType) + verify(template, times(1)).queryForObject(sql, args, argTypes, requiredType) } @Test - fun `queryForObject with reified type and argTypes`() { - val i: Int? = template.queryForObject("select age from customer where id = ?", arrayOf(3), - intArrayOf(JDBCType.INTEGER.vendorTypeNumber)) - assertEquals(22, i) + fun `queryForObject with reified type parameters and argTypes`() { + val sql = "select age from customer where id = ?" + val args = arrayOf(3) + val argTypes = intArrayOf(JDBCType.INTEGER.vendorTypeNumber) + template.queryForObject(sql, args, argTypes) + verify(template, times(1)).queryForObject(sql, args, argTypes, Integer::class.java) } @Test fun `queryForObject with args`() { - val i = template.queryForObject("select age from customer where id = ?", arrayOf(3), Int::class) - assertEquals(22, i) + val sql = "select age from customer where id = ?" + val args = arrayOf(3) + template.queryForObject(sql, args, Int::class) + verify(template, times(1)).queryForObject(sql, args, Int::class.java) } @Test - fun `queryForObject with reified type and args`() { - val i: Int? = template.queryForObject("select age from customer where id = ?", arrayOf(3)) - assertEquals(22, i) + fun `queryForObject with reified type parameters and args`() { + val sql = "select age from customer where id = ?" + val args = arrayOf(3) + template.queryForObject(sql, args) + verify(template, times(1)).queryForObject(sql, args, Integer::class.java) } @Test fun `queryForObject with varargs`() { - val i = template.queryForObject("select age from customer where id = ?", Int::class, 3) - assertEquals(22, i) + val sql = "select age from customer where id = ?" + template.queryForObject(sql, Int::class, 3) + verify(template, times(1)).queryForObject(sql, Int::class.java, 3) } @Test - fun `queryForList with KClass`() { - val i = template.queryForList(sql = "select age from customer where id = 3", elementType = Int::class) - assertEquals(22, i.first()) + fun `queryForList with reified type parameters`() { + val sql = "select age from customer where id = 3" + template.queryForList(sql) + verify(template, times(1)).queryForList(sql, Integer::class.java) } @Test - fun `queryForList with reified type`() { - val i = template.queryForList("select age from customer where id = 3") - assertEquals(22, i.first()) + fun `queryForList with reified type parameters and argTypes`() { + val sql = "select age from customer where id = ?" + val args = arrayOf(3) + val argTypes = intArrayOf(JDBCType.INTEGER.vendorTypeNumber) + template.queryForList(sql, args, argTypes) + verify(template, times(1)).queryForList(sql, args, argTypes, Integer::class.java) } @Test - fun `queryForList with argTypes`() { - val i = template.queryForList(sql = "select age from customer where id = ?", args = arrayOf(3), - argTypes = intArrayOf(JDBCType.INTEGER.vendorTypeNumber), elementType = Int::class) - assertEquals(22, i.first()) - } - - @Test - fun `queryForList with reified type and argTypes`() { - val i = template.queryForList("select age from customer where id = ?", arrayOf(3), intArrayOf(JDBCType.INTEGER.vendorTypeNumber)) - assertEquals(22, i.first()) - } - - @Test - fun `queryForList with args`() { - val i = template.queryForList(sql = "select age from customer where id = ?", args = arrayOf(3), elementType = Int::class) - assertEquals(22, i.first()) - } - - @Test - fun `queryForList with reified type and args`() { - val i = template.queryForList("select age from customer where id = ?", arrayOf(3)) - assertEquals(22, i.first()) + fun `queryForList with reified type parameters and args`() { + val sql = "select age from customer where id = ?" + val args = arrayOf(3) + template.queryForList(sql, args) + verify(template, times(1)).queryForList(sql, args, Integer::class.java) } @Test fun `query with ResultSetExtractor-like function`() { - val i = template.query("select age from customer where id = ?", 3) { rs -> + val sql = "select age from customer where id = ?" + template.query(sql, 3) { rs -> rs.next() rs.getInt(1) } - assertEquals(22, i) + verify(template, times(1)).query(eq(sql), any>(), eq(3)) } @Test fun `query with RowCallbackHandler-like function`() { - template.query("select age from customer where id = ?", 3) { rs -> + val sql = "select age from customer where id = ?" + template.query(sql, 3) { rs -> assertEquals(22, rs.getInt(1)) } + verify(template, times(1)).query(eq(sql), any(), eq(3)) } @Test fun `query with RowMapper-like function`() { - val i = template.query("select age from customer where id = ?", 3) { rs, _ -> + val sql = "select age from customer where id = ?" + template.query(sql, 3) { rs, _ -> rs.getInt(1) } - assertEquals(22, i.first()) + verify(template, times(1)).query(eq(sql), any>(), eq(3)) } -} \ No newline at end of file + +} diff --git a/spring-jdbc/src/test/kotlin/org/springframework/jdbc/core/namedparam/MapSqlParameterSourceExtensionsTests.kt b/spring-jdbc/src/test/kotlin/org/springframework/jdbc/core/namedparam/MapSqlParameterSourceExtensionsTests.kt index 5ab31a25f95..dd388e98a78 100644 --- a/spring-jdbc/src/test/kotlin/org/springframework/jdbc/core/namedparam/MapSqlParameterSourceExtensionsTests.kt +++ b/spring-jdbc/src/test/kotlin/org/springframework/jdbc/core/namedparam/MapSqlParameterSourceExtensionsTests.kt @@ -21,6 +21,8 @@ import org.junit.Test import java.sql.JDBCType /** + * Tests for [MapSqlParameterSource] Kotlin extensions + * * @author Mario Arias */ class MapSqlParameterSourceExtensionsTests { diff --git a/spring-web/src/test/kotlin/org/springframework/web/client/RestOperationsExtensionsTests.kt b/spring-web/src/test/kotlin/org/springframework/web/client/RestOperationsExtensionsTests.kt new file mode 100644 index 00000000000..3355852d3e7 --- /dev/null +++ b/spring-web/src/test/kotlin/org/springframework/web/client/RestOperationsExtensionsTests.kt @@ -0,0 +1,154 @@ +package org.springframework.web.client + +import com.nhaarman.mockito_kotlin.mock +import org.junit.Test +import org.junit.runner.RunWith +import org.mockito.Answers +import org.mockito.Mock +import org.mockito.Mockito +import org.mockito.Mockito.verify +import org.mockito.junit.MockitoJUnitRunner +import org.springframework.http.HttpEntity +import org.springframework.http.HttpMethod +import org.springframework.http.RequestEntity +import java.net.URI + +/** + * Mock object based tests for [RestOperations] Kotlin extensions + * + * @author Sebastien Deleuze + */ +@RunWith(MockitoJUnitRunner::class) +class RestOperationsExtensionsTests { + + @Mock(answer = Answers.RETURNS_MOCKS) + lateinit var template: RestOperations + + @Test + fun `getForObject with reified type parameters, String and varargs`() { + val url = "https://spring.io" + val var1 = "var1" + val var2 = "var2" + template.getForObject(url, var1, var2) + verify(template, Mockito.times(1)).getForObject(url, Foo::class.java, var1, var2) + } + + @Test + fun `getForObject with reified type parameters, String and Map`() { + val url = "https://spring.io" + val vars = mapOf(Pair("key1", "value1"), Pair("key2", "value2")) + template.getForObject(url, vars) + verify(template, Mockito.times(1)).getForObject(url, Foo::class.java, vars) + } + + @Test + fun `getForObject with reified type parameters and URI`() { + val url = URI("https://spring.io") + template.getForObject(url) + verify(template, Mockito.times(1)).getForObject(url, Foo::class.java) + } + + @Test + fun `getForEntity with reified type parameters, String and varargs`() { + val url = "https://spring.io" + val var1 = "var1" + val var2 = "var2" + template.getForEntity(url, var1, var2) + verify(template, Mockito.times(1)).getForEntity(url, Foo::class.java, var1, var2) + } + + @Test + fun `postForObject with reified type parameters, String and varargs`() { + val url = "https://spring.io" + val body: Any = "body" + val var1 = "var1" + val var2 = "var2" + template.postForObject(url, body, var1, var2) + verify(template, Mockito.times(1)).postForObject(url, body, Foo::class.java, var1, var2) + } + + @Test + fun `postForObject with reified type parameters, String and Map`() { + val url = "https://spring.io" + val body: Any = "body" + val vars = mapOf(Pair("key1", "value1"), Pair("key2", "value2")) + template.postForObject(url, body, vars) + verify(template, Mockito.times(1)).postForObject(url, body, Foo::class.java, vars) + } + + @Test + fun `postForObject with reified type parameters`() { + val url = "https://spring.io" + val body: Any = "body" + template.postForObject(url, body) + verify(template, Mockito.times(1)).postForObject(url, body, Foo::class.java) + } + + @Test + fun `postForEntity with reified type parameters, String and varargs`() { + val url = "https://spring.io" + val body: Any = "body" + val var1 = "var1" + val var2 = "var2" + template.postForEntity(url, body, var1, var2) + verify(template, Mockito.times(1)).postForEntity(url, body, Foo::class.java, var1, var2) + } + + @Test + fun `postForEntity with reified type parameters, String and Map`() { + val url = "https://spring.io" + val body: Any = "body" + val vars = mapOf(Pair("key1", "value1"), Pair("key2", "value2")) + template.postForEntity(url, body, vars) + verify(template, Mockito.times(1)).postForEntity(url, body, Foo::class.java, vars) + } + + @Test + fun `postForEntity with reified type parameters`() { + val url = "https://spring.io" + val body: Any = "body" + template.postForEntity(url, body) + verify(template, Mockito.times(1)).postForEntity(url, body, Foo::class.java) + } + + @Test + fun `exchange with reified type parameters, String, HttpMethod, HttpEntity and varargs`() { + val url = "https://spring.io" + val method = HttpMethod.GET + val entity = mock>() + val var1 = "var1" + val var2 = "var2" + template.exchange(url, method, entity, var1, var2) + verify(template, Mockito.times(1)).exchange(url, method, entity, Foo::class.java, var1, var2) + } + + @Test + fun `exchange with reified type parameters, String, HttpMethod, HttpEntity and Map`() { + val url = "https://spring.io" + val method = HttpMethod.GET + val entity = mock>() + val vars = mapOf(Pair("key1", "value1"), Pair("key2", "value2")) + template.exchange(url, method, entity, vars) + verify(template, Mockito.times(1)).exchange(url, method, entity, Foo::class.java, vars) + } + + @Test + fun `exchange with reified type parameters, String, HttpMethod, HttpEntity`() { + val url = "https://spring.io" + val method = HttpMethod.GET + val entity = mock>() + template.exchange(url, method, entity) + verify(template, Mockito.times(1)).exchange(url, method, entity, Foo::class.java) + } + + @Test + fun `exchange with reified type parameters, String, HttpEntity`() { + val url = "https://spring.io" + val entity = mock>() + template.exchange(entity) + verify(template, Mockito.times(1)).exchange(entity, Foo::class.java) + } + + class Foo + +} diff --git a/spring-webflux/src/main/kotlin/org/springframework/web/reactive/function/BodyExtractorsExtensions.kt b/spring-webflux/src/main/kotlin/org/springframework/web/reactive/function/BodyExtractorsExtensions.kt index 8dc50e51e1d..287e4608683 100644 --- a/spring-webflux/src/main/kotlin/org/springframework/web/reactive/function/BodyExtractorsExtensions.kt +++ b/spring-webflux/src/main/kotlin/org/springframework/web/reactive/function/BodyExtractorsExtensions.kt @@ -6,37 +6,41 @@ import reactor.core.publisher.Mono import kotlin.reflect.KClass /** - * Function for providing a `toMono()` alternative to `BodyExtractors.toMono(Foo::class.java)`. + * Function for providing a `bodyToMono()` alternative to `BodyExtractors.toMono(Foo::class.java)`. * * @author Sebastien Deleuze * @since 5.0 + * @see [KT-11968](https://youtrack.jetbrains.com/issue/KT-11968) */ inline fun toMono(): BodyExtractor, ReactiveHttpInputMessage> = BodyExtractors.toMono(T::class.java) /** - * Function for providing a `toMono(Foo::class)` alternative to `BodyExtractors.toMono(Foo::class.java)`. + * Function for providing a `bodyToMono(Foo::class)` alternative to `BodyExtractors.toMono(Foo::class.java)`. * * @author Sebastien Deleuze * @since 5.0 + * @see [KT-11968](https://youtrack.jetbrains.com/issue/KT-11968) */ fun toMono(elementClass: KClass): BodyExtractor, ReactiveHttpInputMessage> = BodyExtractors.toMono(elementClass.java) /** - * Function for providing a `toFlux()` alternative to `BodyExtractors.toFlux(Foo::class.java)`. + * Function for providing a `bodyToFlux()` alternative to `BodyExtractors.toFlux(Foo::class.java)`. * * @author Sebastien Deleuze * @since 5.0 + * @see [KT-11968](https://youtrack.jetbrains.com/issue/KT-11968) */ inline fun toFlux(): BodyExtractor, ReactiveHttpInputMessage> = BodyExtractors.toFlux(T::class.java) /** - * Function for providing a `toFlux(Foo::class)` alternative to `BodyExtractors.toFlux(Foo::class.java)`. + * Function for providing a `bodyToFlux(Foo::class)` alternative to `BodyExtractors.toFlux(Foo::class.java)`. * * @author Sebastien Deleuze * @since 5.0 + * @see [KT-11968](https://youtrack.jetbrains.com/issue/KT-11968) */ fun toFlux(elementClass: KClass): BodyExtractor, ReactiveHttpInputMessage> = BodyExtractors.toFlux(elementClass.java) diff --git a/spring-webflux/src/main/kotlin/org/springframework/web/reactive/function/BodyInsertersExtensions.kt b/spring-webflux/src/main/kotlin/org/springframework/web/reactive/function/BodyInsertersExtensions.kt index 849d0938963..14a4e129969 100644 --- a/spring-webflux/src/main/kotlin/org/springframework/web/reactive/function/BodyInsertersExtensions.kt +++ b/spring-webflux/src/main/kotlin/org/springframework/web/reactive/function/BodyInsertersExtensions.kt @@ -5,7 +5,7 @@ import org.springframework.http.ReactiveHttpOutputMessage import org.springframework.http.server.reactive.ServerHttpResponse /** - * Function for providing a `fromPublisher(publisher)` alternative to `BodyInserters.fromPublisher(publisher, Foo::class.java)`. + * Function for providing a `bodyFromPublisher(publisher)` alternative to `BodyInserters.fromPublisher(publisher, Foo::class.java)`. * * @author Sebastien Deleuze * @since 5.0 @@ -14,7 +14,7 @@ inline fun , reified S : Any> fromPublisher(publisher: BodyInserters.fromPublisher(publisher, S::class.java) /** - * Function for providing a `fromServerSentEvents(publisher)` alternative to `BodyInserters.fromServerSentEvents(publisher, Foo::class.java)`. + * Function for providing a `bodyFromServerSentEvents(publisher)` alternative to `BodyInserters.fromServerSentEvents(publisher, Foo::class.java)`. * * @author Sebastien Deleuze * @since 5.0 diff --git a/spring-webflux/src/test/kotlin/org/springframework/web/reactive/function/BodyExtractorsExtensionsTests.kt b/spring-webflux/src/test/kotlin/org/springframework/web/reactive/function/BodyExtractorsExtensionsTests.kt new file mode 100644 index 00000000000..328faf6a8b0 --- /dev/null +++ b/spring-webflux/src/test/kotlin/org/springframework/web/reactive/function/BodyExtractorsExtensionsTests.kt @@ -0,0 +1,38 @@ +package org.springframework.web.reactive.function + +import org.junit.Assert.assertNotNull +import org.junit.Test +import org.junit.runner.RunWith +import org.mockito.junit.MockitoJUnitRunner + + +/** + * Tests for [BodyExtractors] Kotlin extensions + * + * @author Sebastien Deleuze + */ +@RunWith(MockitoJUnitRunner::class) +class BodyExtractorsExtensionsTests { + + @Test + fun `toMono with KClass`() { + assertNotNull(toMono(Foo::class)) + } + + @Test + fun `toMono with reified type parameter`() { + assertNotNull(toMono()) + } + + @Test + fun `toFlux with KClass`() { + assertNotNull(toFlux(Foo::class)) + } + + @Test + fun `toFlux with reified type parameter`() { + assertNotNull(toFlux()) + } + + class Foo +} diff --git a/spring-webflux/src/test/kotlin/org/springframework/web/reactive/function/BodyInsertersExtensionsTests.kt b/spring-webflux/src/test/kotlin/org/springframework/web/reactive/function/BodyInsertersExtensionsTests.kt new file mode 100644 index 00000000000..ea45894da40 --- /dev/null +++ b/spring-webflux/src/test/kotlin/org/springframework/web/reactive/function/BodyInsertersExtensionsTests.kt @@ -0,0 +1,32 @@ +package org.springframework.web.reactive.function + +import com.nhaarman.mockito_kotlin.mock +import org.junit.Assert.assertNotNull +import org.junit.Test +import org.junit.runner.RunWith +import org.mockito.junit.MockitoJUnitRunner +import org.reactivestreams.Publisher + + +/** + * Tests for [BodyExtractors] Kotlin extensions + * + * @author Sebastien Deleuze + */ +@RunWith(MockitoJUnitRunner::class) +class BodyInsertersExtensionsTests { + + @Test + fun `fromPublisher with reified type parameters`() { + val publisher = mock>() + assertNotNull(fromPublisher(publisher)) + } + + @Test + fun `fromServerSentEvents with reified type parameters`() { + val publisher = mock>() + assertNotNull(fromServerSentEvents(publisher)) + } + + class Foo +} diff --git a/spring-webflux/src/test/kotlin/org/springframework/web/reactive/function/client/ClientResponseExtensionsTests.kt b/spring-webflux/src/test/kotlin/org/springframework/web/reactive/function/client/ClientResponseExtensionsTests.kt new file mode 100644 index 00000000000..7540d64db7e --- /dev/null +++ b/spring-webflux/src/test/kotlin/org/springframework/web/reactive/function/client/ClientResponseExtensionsTests.kt @@ -0,0 +1,47 @@ +package org.springframework.web.reactive.function.client + +import org.junit.Test +import org.junit.runner.RunWith +import org.mockito.Answers +import org.mockito.Mock +import org.mockito.Mockito +import org.mockito.Mockito.verify +import org.mockito.junit.MockitoJUnitRunner + +/** + * Mock object based tests for [ClientResponse] Kotlin extensions + * + * @author Sebastien Deleuze + */ +@RunWith(MockitoJUnitRunner::class) +class ClientResponseExtensionsTests { + + @Mock(answer = Answers.RETURNS_MOCKS) + lateinit var response: ClientResponse + + @Test + fun `bodyToMono with KClass`() { + response.bodyToMono(Foo::class) + verify(response, Mockito.times(1)).bodyToMono(Foo::class.java) + } + + @Test + fun `bodyToMono with reified type parameters`() { + response.bodyToMono() + verify(response, Mockito.times(1)).bodyToMono(Foo::class.java) + } + + @Test + fun `bodyToFlux with KClass`() { + response.bodyToFlux(Foo::class) + verify(response, Mockito.times(1)).bodyToFlux(Foo::class.java) + } + + @Test + fun `bodyToFlux with reified type parameters`() { + response.bodyToFlux() + verify(response, Mockito.times(1)).bodyToFlux(Foo::class.java) + } + + class Foo +} diff --git a/spring-webflux/src/test/kotlin/org/springframework/web/reactive/function/client/WebClientExtensionsTests.kt b/spring-webflux/src/test/kotlin/org/springframework/web/reactive/function/client/WebClientExtensionsTests.kt new file mode 100644 index 00000000000..6710a05d793 --- /dev/null +++ b/spring-webflux/src/test/kotlin/org/springframework/web/reactive/function/client/WebClientExtensionsTests.kt @@ -0,0 +1,84 @@ +package org.springframework.web.reactive.function.client + +import com.nhaarman.mockito_kotlin.mock +import org.junit.Test +import org.junit.runner.RunWith +import org.mockito.Answers +import org.mockito.Mock +import org.mockito.Mockito +import org.mockito.Mockito.verify +import org.mockito.junit.MockitoJUnitRunner +import org.reactivestreams.Publisher + +/** + * Mock object based tests for [WebClient] Kotlin extensions + * + * @author Sebastien Deleuze + */ +@RunWith(MockitoJUnitRunner::class) +class WebClientExtensionsTests { + + @Mock(answer = Answers.RETURNS_MOCKS) + lateinit var requestBodySpec: WebClient.RequestBodySpec + + @Mock(answer = Answers.RETURNS_MOCKS) + lateinit var responseSpec: WebClient.ResponseSpec + + + @Test + fun `RequestBodySpec#body with Publisher and reified type parameters`() { + val body = mock>() + requestBodySpec.body(body) + verify(requestBodySpec, Mockito.times(1)).body(body, Foo::class.java) + } + + @Test + fun `ResponseSpec#bodyToMono with KClass`() { + responseSpec.bodyToMono(Foo::class) + verify(responseSpec, Mockito.times(1)).bodyToMono(Foo::class.java) + } + + @Test + fun `ResponseSpec#bodyToMono with reified type parameters`() { + responseSpec.bodyToMono() + verify(responseSpec, Mockito.times(1)).bodyToMono(Foo::class.java) + } + + @Test + fun `ResponseSpec#bodyToFlux with KClass`() { + responseSpec.bodyToFlux(Foo::class) + verify(responseSpec, Mockito.times(1)).bodyToFlux(Foo::class.java) + } + + @Test + fun `ResponseSpec#bodyToFlux with reified type parameters`() { + responseSpec.bodyToFlux() + verify(responseSpec, Mockito.times(1)).bodyToFlux(Foo::class.java) + } + + @Test + fun `ResponseSpec#toEntity with KClass`() { + responseSpec.toEntity(Foo::class) + verify(responseSpec, Mockito.times(1)).toEntity(Foo::class.java) + } + + @Test + fun `ResponseSpec#toEntity with reified type parameters`() { + responseSpec.toEntity() + verify(responseSpec, Mockito.times(1)).toEntity(Foo::class.java) + } + + @Test + fun `ResponseSpec#toEntityList with KClass`() { + responseSpec.toEntityList(Foo::class) + verify(responseSpec, Mockito.times(1)).toEntityList(Foo::class.java) + } + + @Test + fun `ResponseSpec#toEntityList with reified type parameters`() { + responseSpec.toEntityList() + verify(responseSpec, Mockito.times(1)).toEntityList(Foo::class.java) + } + + class Foo +} diff --git a/spring-webflux/src/test/kotlin/org/springframework/web/reactive/function/server/RouterFunctionExtensionsTests.kt b/spring-webflux/src/test/kotlin/org/springframework/web/reactive/function/server/RouterFunctionExtensionsTests.kt index 443ab616d30..2760db9e4fb 100644 --- a/spring-webflux/src/test/kotlin/org/springframework/web/reactive/function/server/RouterFunctionExtensionsTests.kt +++ b/spring-webflux/src/test/kotlin/org/springframework/web/reactive/function/server/RouterFunctionExtensionsTests.kt @@ -27,6 +27,11 @@ import reactor.core.publisher.Mono import reactor.test.StepVerifier import java.net.URI +/** + * Tests for [RouterFunction] Kotlin DSL + * + * @author Sebastien Deleuze + */ class RouterFunctionExtensionsTests { @Test diff --git a/spring-webflux/src/test/kotlin/org/springframework/web/reactive/function/server/ServerRequestExtensionsTests.kt b/spring-webflux/src/test/kotlin/org/springframework/web/reactive/function/server/ServerRequestExtensionsTests.kt new file mode 100644 index 00000000000..1380ba27ea7 --- /dev/null +++ b/spring-webflux/src/test/kotlin/org/springframework/web/reactive/function/server/ServerRequestExtensionsTests.kt @@ -0,0 +1,50 @@ +package org.springframework.web.reactive.function.client + +import org.junit.Test +import org.junit.runner.RunWith +import org.mockito.Answers +import org.mockito.Mock +import org.mockito.Mockito +import org.mockito.Mockito.verify +import org.mockito.junit.MockitoJUnitRunner +import org.springframework.web.reactive.function.server.ServerRequest +import org.springframework.web.reactive.function.server.bodyToFlux +import org.springframework.web.reactive.function.server.bodyToMono + +/** + * Mock object based tests for [ServerRequest] Kotlin extensions + * + * @author Sebastien Deleuze + */ +@RunWith(MockitoJUnitRunner::class) +class ServerRequestExtensionsTests { + + @Mock(answer = Answers.RETURNS_MOCKS) + lateinit var request: ServerRequest + + @Test + fun `bodyToMono with KClass`() { + request.bodyToMono(Foo::class) + verify(request, Mockito.times(1)).bodyToMono(Foo::class.java) + } + + @Test + fun `bodyToMono with reified type parameters`() { + request.bodyToMono() + verify(request, Mockito.times(1)).bodyToMono(Foo::class.java) + } + + @Test + fun `bodyToFlux with KClass`() { + request.bodyToFlux(Foo::class) + verify(request, Mockito.times(1)).bodyToFlux(Foo::class.java) + } + + @Test + fun `bodyToFlux with reified type parameters`() { + request.bodyToFlux() + verify(request, Mockito.times(1)).bodyToFlux(Foo::class.java) + } + + class Foo +} diff --git a/spring-webflux/src/test/kotlin/org/springframework/web/reactive/function/server/ServerResponseExtensionsTests.kt b/spring-webflux/src/test/kotlin/org/springframework/web/reactive/function/server/ServerResponseExtensionsTests.kt new file mode 100644 index 00000000000..16c9037a916 --- /dev/null +++ b/spring-webflux/src/test/kotlin/org/springframework/web/reactive/function/server/ServerResponseExtensionsTests.kt @@ -0,0 +1,32 @@ +package org.springframework.web.reactive.function.server + +import com.nhaarman.mockito_kotlin.mock +import org.junit.Test +import org.junit.runner.RunWith +import org.mockito.Answers +import org.mockito.Mock +import org.mockito.Mockito +import org.mockito.junit.MockitoJUnitRunner +import org.reactivestreams.Publisher + +/** + * Mock object based tests for [ServerResponse] Kotlin extensions + * + * @author Sebastien Deleuze + */ +@RunWith(MockitoJUnitRunner::class) +class ServerResponseExtensionsTests { + + @Mock(answer = Answers.RETURNS_MOCKS) + lateinit var bodyBuilder: ServerResponse.BodyBuilder + + + @Test + fun `BodyBuilder#body with Publisher and reified type parameters`() { + val body = mock>() + bodyBuilder.body(body) + Mockito.verify(bodyBuilder, Mockito.times(1)).body(body, Foo::class.java) + } + + class Foo +}