Add comprehensive tests of Kotlin extensions

This commit also removes extensions hidden by Java API (varargs).
This commit is contained in:
Sebastien Deleuze 2017-06-09 00:47:24 +03:00
parent fd53d2a51a
commit 8579ae66fc
23 changed files with 766 additions and 158 deletions

View File

@ -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}")
}

View File

@ -19,16 +19,6 @@ fun <T : Any> BeanFactory.getBean(requiredType: KClass<T>): T = getBean(required
*/
inline fun <reified T : Any> BeanFactory.getBean(): T = getBean(T::class.java)
/**
* Extension for [BeanFactory.getBean] providing a [KClass] based variant.
*
* @see BeanFactory.getBean(String, Class<T>)
* @author Sebastien Deleuze
* @since 5.0
*/
fun <T : Any> BeanFactory.getBean(name: String, requiredType: KClass<T>): T =
getBean(name, requiredType.java)
/**
* Extension for [BeanFactory.getBean] providing a `getBean<Foo>("foo")` variant.
*

View File

@ -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<Foo>()
verify(bf, Mockito.times(1)).getBean(Foo::class.java)
}
@Test
fun `getBean with String and reified type parameters`() {
val name = "foo"
bf.getBean<Foo>(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<Foo>(arg1, arg2)
verify(bf, Mockito.times(1)).getBean(Foo::class.java, arg1, arg2)
}
class Foo
}

View File

@ -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<Foo>()
Mockito.verify(lbf, Mockito.times(1)).getBeanNamesForType(Foo::class.java, true , true)
}
@Test
fun `getBeanNamesForType with reified type parameters and Boolean`() {
lbf.getBeanNamesForType<Foo>(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<Foo>(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<Foo>()
Mockito.verify(lbf, Mockito.times(1)).getBeansOfType(Foo::class.java, true , true)
}
@Test
fun `getBeansOfType with reified type parameters and Boolean`() {
lbf.getBeansOfType<Foo>(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<Foo>(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<Bar>()
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<Bar>()
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<Bar>(name)
Mockito.verify(lbf, Mockito.times(1)).findAnnotationOnBean(name, Bar::class.java)
}
class Foo
annotation class Bar
}

View File

@ -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
*/

View File

@ -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<Foo>()
}
applicationContext.refresh()
assertNotNull(applicationContext)
assertNotNull(applicationContext.getBean<Foo>())
}
class Foo
}

View File

@ -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<BeanB>())
}
internal class BeanA
class BeanA
internal class BeanB(val a: BeanA)
class BeanB(val a: BeanA)
}

View File

@ -21,6 +21,11 @@ import org.junit.Assert.assertTrue
import org.junit.Test
/**
* Tests for [Model] Kotlin extensions
*
* @author Sebastien Deleuze
*/
class ModelExtensionsTests {
@Test

View File

@ -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"])
}
}

View File

@ -91,15 +91,6 @@ fun <T : Any> JdbcOperations.queryForObject(sql: String, requiredType: KClass<T>
queryForObject(sql, requiredType.java, *args)
/**
* Extension for [JdbcOperations.queryForList] providing a [KClass] based variant.
*
* @author Mario Arias
* @since 5.0
*/
fun <T : Any> JdbcOperations.queryForList(sql: String, elementType: KClass<T>): List<T> =
queryForList(sql, elementType.java)
/**
* Extension for [JdbcOperations.queryForList] providing a `queryForList<Foo>("...")` variant.
*
@ -110,15 +101,6 @@ fun <T : Any> JdbcOperations.queryForList(sql: String, elementType: KClass<T>):
inline fun <reified T : Any> JdbcOperations.queryForList(sql: String): List<T> =
queryForList(sql, T::class.java)
/**
* Extension for [JdbcOperations.queryForList] providing a [KClass] based variant.
*
* @author Mario Arias
* @since 5.0
*/
fun <T : Any> JdbcOperations.queryForList(sql: String, args: Array<out Any>, argTypes: IntArray, elementType: KClass<T>): List<T> =
queryForList(sql, args, argTypes, elementType.java)
/**
* Extension for [JdbcOperations.queryForList] providing a `queryForList<Foo>("...", arrayOf(arg1, argN), intArray(type1, typeN))` variant
*
@ -129,15 +111,6 @@ fun <T : Any> JdbcOperations.queryForList(sql: String, args: Array<out Any>, arg
inline fun <reified T : Any> JdbcOperations.queryForList(sql: String, args: Array<out Any>, argTypes: IntArray): List<T> =
queryForList(sql, args, argTypes, T::class.java)
/**
* Extension for [JdbcOperations.queryForList] providing a [KClass] based variant.
*
* @author Mario Arias
* @since 5.0
*/
fun <T : Any> JdbcOperations.queryForList(sql: String, args: Array<out Any>, elementType: KClass<T>): List<T> =
queryForList(sql, args, elementType.java)
/**
* Extension for [JdbcOperations.queryForList] providing a `queryForList<Foo>("...", arrayOf(arg1, argN))` variant
*
@ -174,4 +147,3 @@ fun JdbcOperations.query(sql: String, vararg args: Any, function: (ResultSet) ->
*/
fun <T : Any> JdbcOperations.query(sql: String, vararg args: Any, function: (ResultSet, Int) -> T): List<T> =
query(sql, RowMapper { rs, i -> function(rs, i) }, *args)

View File

@ -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)
}
}

View File

@ -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<Int>(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<RowMapper<Int>>(), 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<Int>(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<Int>(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<Int>(sql)
verify(template, times(1)).queryForList(sql, Integer::class.java)
}
@Test
fun `queryForList with reified type`() {
val i = template.queryForList<Int>("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<Int>(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<Int>("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<Int>("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<Int>(sql, args)
verify(template, times(1)).queryForList(sql, args, Integer::class.java)
}
@Test
fun `query with ResultSetExtractor-like function`() {
val i = template.query<Int>("select age from customer where id = ?", 3) { rs ->
val sql = "select age from customer where id = ?"
template.query<Int>(sql, 3) { rs ->
rs.next()
rs.getInt(1)
}
assertEquals(22, i)
verify(template, times(1)).query(eq(sql), any<ResultSetExtractor<Int>>(), 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<RowCallbackHandler>(), 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<RowMapper<Int>>(), eq(3))
}
}
}

View File

@ -21,6 +21,8 @@ import org.junit.Test
import java.sql.JDBCType
/**
* Tests for [MapSqlParameterSource] Kotlin extensions
*
* @author Mario Arias
*/
class MapSqlParameterSourceExtensionsTests {

View File

@ -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<Foo>(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<Foo>(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<Foo>(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<Foo>(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<Foo>(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<Foo>(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<Foo>(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<Foo>(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<Foo>(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<Foo>(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<HttpEntity<Foo>>()
val var1 = "var1"
val var2 = "var2"
template.exchange<Foo>(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<HttpEntity<Foo>>()
val vars = mapOf(Pair("key1", "value1"), Pair("key2", "value2"))
template.exchange<Foo>(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<HttpEntity<Foo>>()
template.exchange<Foo>(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<RequestEntity<Foo>>()
template.exchange<Foo>(entity)
verify(template, Mockito.times(1)).exchange(entity, Foo::class.java)
}
class Foo
}

View File

@ -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 <reified T : Any> toMono(): BodyExtractor<Mono<T>, 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 <T : Any> toMono(elementClass: KClass<T>): BodyExtractor<Mono<T>, 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 <reified T : Any> toFlux(): BodyExtractor<Flux<T>, 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 <T : Any> toFlux(elementClass: KClass<T>): BodyExtractor<Flux<T>, ReactiveHttpInputMessage> =
BodyExtractors.toFlux(elementClass.java)

View File

@ -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 T : Publisher<S>, 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

View File

@ -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<Foo>())
}
@Test
fun `toFlux with KClass`() {
assertNotNull(toFlux(Foo::class))
}
@Test
fun `toFlux with reified type parameter`() {
assertNotNull(toFlux<Foo>())
}
class Foo
}

View File

@ -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<Publisher<Foo>>()
assertNotNull(fromPublisher(publisher))
}
@Test
fun `fromServerSentEvents with reified type parameters`() {
val publisher = mock<Publisher<Foo>>()
assertNotNull(fromServerSentEvents(publisher))
}
class Foo
}

View File

@ -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<Foo>()
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<Foo>()
verify(response, Mockito.times(1)).bodyToFlux(Foo::class.java)
}
class Foo
}

View File

@ -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<Publisher<Foo>>()
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<Foo>()
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<Foo>()
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<Foo>()
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<Foo>()
verify(responseSpec, Mockito.times(1)).toEntityList(Foo::class.java)
}
class Foo
}

View File

@ -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

View File

@ -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<Foo>()
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<Foo>()
verify(request, Mockito.times(1)).bodyToFlux(Foo::class.java)
}
class Foo
}

View File

@ -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<Publisher<Foo>>()
bodyBuilder.body(body)
Mockito.verify(bodyBuilder, Mockito.times(1)).body(body, Foo::class.java)
}
class Foo
}