Migrate Kotlin tests to Mockk

See gh-15993
This commit is contained in:
Sebastien Deleuze 2019-02-19 12:39:44 +01:00 committed by Stephane Nicoll
parent 967eecfbf8
commit cca79b8d4d
3 changed files with 40 additions and 61 deletions

View File

@ -61,23 +61,9 @@
<version>1.3.3</version> <version>1.3.3</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.nhaarman</groupId> <groupId>io.mockk</groupId>
<artifactId>mockito-kotlin</artifactId> <artifactId>mockk</artifactId>
<version>1.6.0</version> <version>1.9.1.kotlin12</version>
<exclusions>
<exclusion>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
</exclusion>
<exclusion>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-reflect</artifactId>
</exclusion>
<exclusion>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
</exclusion>
</exclusions>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.sonatype.plexus</groupId> <groupId>org.sonatype.plexus</groupId>

View File

@ -188,8 +188,8 @@
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.nhaarman</groupId> <groupId>io.mockk</groupId>
<artifactId>mockito-kotlin</artifactId> <artifactId>mockk</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>

View File

@ -16,15 +16,10 @@
package org.springframework.boot.test.web.client package org.springframework.boot.test.web.client
import com.nhaarman.mockito_kotlin.mock import io.mockk.mockk
import io.mockk.verify
import org.junit.Assert import org.junit.Assert
import org.junit.Test import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Answers
import org.mockito.Mock
import org.mockito.Mockito.times
import org.mockito.Mockito.verify
import org.mockito.junit.MockitoJUnitRunner
import org.springframework.core.ParameterizedTypeReference import org.springframework.core.ParameterizedTypeReference
import org.springframework.http.HttpEntity import org.springframework.http.HttpEntity
import org.springframework.http.HttpMethod import org.springframework.http.HttpMethod
@ -40,11 +35,9 @@ import kotlin.reflect.jvm.kotlinFunction
* *
* @author Sebastien Deleuze * @author Sebastien Deleuze
*/ */
@RunWith(MockitoJUnitRunner::class)
class TestRestTemplateExtensionsTests { class TestRestTemplateExtensionsTests {
@Mock(answer = Answers.RETURNS_MOCKS) val template = mockk<TestRestTemplate>(relaxed = true)
lateinit var template: TestRestTemplate
@Test @Test
fun `getForObject with reified type parameters, String and varargs`() { fun `getForObject with reified type parameters, String and varargs`() {
@ -53,7 +46,7 @@ class TestRestTemplateExtensionsTests {
val var2 = "var2" val var2 = "var2"
template.getForObject<Foo>(url, var1, var2) template.getForObject<Foo>(url, var1, var2)
template.restTemplate template.restTemplate
verify(template, times(1)).getForObject(url, Foo::class.java, var1, var2) verify(exactly = 1) { template.getForObject(url, Foo::class.java, var1, var2) }
} }
@Test @Test
@ -61,21 +54,21 @@ class TestRestTemplateExtensionsTests {
val url = "https://spring.io" val url = "https://spring.io"
val vars = mapOf(Pair("key1", "value1"), Pair("key2", "value2")) val vars = mapOf(Pair("key1", "value1"), Pair("key2", "value2"))
template.getForObject<Foo>(url, vars) template.getForObject<Foo>(url, vars)
verify(template, times(1)).getForObject(url, Foo::class.java, vars) verify(exactly = 1) { template.getForObject(url, Foo::class.java, vars) }
} }
@Test @Test
fun `getForObject with reified type parameters and URI`() { fun `getForObject with reified type parameters and URI`() {
val url = URI("https://spring.io") val url = URI("https://spring.io")
template.getForObject<Foo>(url) template.getForObject<Foo>(url)
verify(template, times(1)).getForObject(url, Foo::class.java) verify(exactly = 1) { template.getForObject(url, Foo::class.java) }
} }
@Test @Test
fun `getForEntity with reified type parameters and URI`() { fun `getForEntity with reified type parameters and URI`() {
val url = URI("https://spring.io") val url = URI("https://spring.io")
template.getForEntity<Foo>(url) template.getForEntity<Foo>(url)
verify(template, times(1)).getForEntity(url, Foo::class.java) verify(exactly = 1) { template.getForEntity(url, Foo::class.java) }
} }
@Test @Test
@ -84,7 +77,7 @@ class TestRestTemplateExtensionsTests {
val var1 = "var1" val var1 = "var1"
val var2 = "var2" val var2 = "var2"
template.getForEntity<Foo>(url, var1, var2) template.getForEntity<Foo>(url, var1, var2)
verify(template, times(1)).getForEntity(url, Foo::class.java, var1, var2) verify(exactly = 1) { template.getForEntity(url, Foo::class.java, var1, var2) }
} }
@Test @Test
@ -92,7 +85,7 @@ class TestRestTemplateExtensionsTests {
val url = "https://spring.io" val url = "https://spring.io"
val vars = mapOf(Pair("key1", "value1"), Pair("key2", "value2")) val vars = mapOf(Pair("key1", "value1"), Pair("key2", "value2"))
template.getForEntity<Foo>(url, vars) template.getForEntity<Foo>(url, vars)
verify(template, times(1)).getForEntity(url, Foo::class.java, vars) verify(exactly = 1) { template.getForEntity(url, Foo::class.java, vars) }
} }
@Test @Test
@ -102,7 +95,7 @@ class TestRestTemplateExtensionsTests {
val var1 = "var1" val var1 = "var1"
val var2 = "var2" val var2 = "var2"
template.patchForObject<Foo>(url, body, var1, var2) template.patchForObject<Foo>(url, body, var1, var2)
verify(template, times(1)).patchForObject(url, body, Foo::class.java, var1, var2) verify(exactly = 1) { template.patchForObject(url, body, Foo::class.java, var1, var2) }
} }
@Test @Test
@ -111,7 +104,7 @@ class TestRestTemplateExtensionsTests {
val body: Any = "body" val body: Any = "body"
val vars = mapOf(Pair("key1", "value1"), Pair("key2", "value2")) val vars = mapOf(Pair("key1", "value1"), Pair("key2", "value2"))
template.patchForObject<Foo>(url, body, vars) template.patchForObject<Foo>(url, body, vars)
verify(template, times(1)).patchForObject(url, body, Foo::class.java, vars) verify(exactly = 1) { template.patchForObject(url, body, Foo::class.java, vars) }
} }
@Test @Test
@ -119,14 +112,14 @@ class TestRestTemplateExtensionsTests {
val url = "https://spring.io" val url = "https://spring.io"
val body: Any = "body" val body: Any = "body"
template.patchForObject<Foo>(url, body) template.patchForObject<Foo>(url, body)
verify(template, times(1)).patchForObject(url, body, Foo::class.java) verify(exactly = 1) { template.patchForObject(url, body, Foo::class.java) }
} }
@Test @Test
fun `patchForObject with reified type parameters`() { fun `patchForObject with reified type parameters`() {
val url = "https://spring.io" val url = "https://spring.io"
template.patchForObject<Foo>(url) template.patchForObject<Foo>(url)
verify(template, times(1)).patchForObject(url, null, Foo::class.java) verify(exactly = 1) { template.patchForObject(url, null, Foo::class.java) }
} }
@Test @Test
@ -136,7 +129,7 @@ class TestRestTemplateExtensionsTests {
val var1 = "var1" val var1 = "var1"
val var2 = "var2" val var2 = "var2"
template.postForObject<Foo>(url, body, var1, var2) template.postForObject<Foo>(url, body, var1, var2)
verify(template, times(1)).postForObject(url, body, Foo::class.java, var1, var2) verify(exactly = 1) { template.postForObject(url, body, Foo::class.java, var1, var2) }
} }
@Test @Test
@ -145,7 +138,7 @@ class TestRestTemplateExtensionsTests {
val body: Any = "body" val body: Any = "body"
val vars = mapOf(Pair("key1", "value1"), Pair("key2", "value2")) val vars = mapOf(Pair("key1", "value1"), Pair("key2", "value2"))
template.postForObject<Foo>(url, body, vars) template.postForObject<Foo>(url, body, vars)
verify(template, times(1)).postForObject(url, body, Foo::class.java, vars) verify(exactly = 1) { template.postForObject(url, body, Foo::class.java, vars) }
} }
@Test @Test
@ -153,14 +146,14 @@ class TestRestTemplateExtensionsTests {
val url = "https://spring.io" val url = "https://spring.io"
val body: Any = "body" val body: Any = "body"
template.postForObject<Foo>(url, body) template.postForObject<Foo>(url, body)
verify(template, times(1)).postForObject(url, body, Foo::class.java) verify(exactly = 1) { template.postForObject(url, body, Foo::class.java) }
} }
@Test @Test
fun `postForObject with reified type parameters`() { fun `postForObject with reified type parameters`() {
val url = "https://spring.io" val url = "https://spring.io"
template.postForObject<Foo>(url) template.postForObject<Foo>(url)
verify(template, times(1)).postForObject(url, null, Foo::class.java) verify(exactly = 1) { template.postForObject(url, null, Foo::class.java) }
} }
@Test @Test
@ -170,7 +163,7 @@ class TestRestTemplateExtensionsTests {
val var1 = "var1" val var1 = "var1"
val var2 = "var2" val var2 = "var2"
template.postForEntity<Foo>(url, body, var1, var2) template.postForEntity<Foo>(url, body, var1, var2)
verify(template, times(1)).postForEntity(url, body, Foo::class.java, var1, var2) verify(exactly = 1) { template.postForEntity(url, body, Foo::class.java, var1, var2) }
} }
@Test @Test
@ -179,7 +172,7 @@ class TestRestTemplateExtensionsTests {
val body: Any = "body" val body: Any = "body"
val vars = mapOf(Pair("key1", "value1"), Pair("key2", "value2")) val vars = mapOf(Pair("key1", "value1"), Pair("key2", "value2"))
template.postForEntity<Foo>(url, body, vars) template.postForEntity<Foo>(url, body, vars)
verify(template, times(1)).postForEntity(url, body, Foo::class.java, vars) verify(exactly = 1) { template.postForEntity(url, body, Foo::class.java, vars) }
} }
@Test @Test
@ -187,55 +180,55 @@ class TestRestTemplateExtensionsTests {
val url = "https://spring.io" val url = "https://spring.io"
val body: Any = "body" val body: Any = "body"
template.postForEntity<Foo>(url, body) template.postForEntity<Foo>(url, body)
verify(template, times(1)).postForEntity(url, body, Foo::class.java) verify(exactly = 1) { template.postForEntity(url, body, Foo::class.java) }
} }
@Test @Test
fun `postForEntity with reified type parameters`() { fun `postForEntity with reified type parameters`() {
val url = "https://spring.io" val url = "https://spring.io"
template.postForEntity<Foo>(url) template.postForEntity<Foo>(url)
verify(template, times(1)).postForEntity(url, null, Foo::class.java) verify(exactly = 1) { template.postForEntity(url, null, Foo::class.java) }
} }
@Test @Test
fun `exchange with reified type parameters, String, HttpMethod, HttpEntity and varargs`() { fun `exchange with reified type parameters, String, HttpMethod, HttpEntity and varargs`() {
val url = "https://spring.io" val url = "https://spring.io"
val method = HttpMethod.GET val method = HttpMethod.GET
val entity = mock<HttpEntity<Foo>>() val entity = mockk<HttpEntity<Foo>>()
val var1 = "var1" val var1 = "var1"
val var2 = "var2" val var2 = "var2"
template.exchange<List<Foo>>(url, method, entity, var1, var2) template.exchange<List<Foo>>(url, method, entity, var1, var2)
verify(template, times(1)).exchange(url, method, entity, verify(exactly = 1) { template.exchange(url, method, entity,
object : ParameterizedTypeReference<List<Foo>>() {}, var1, var2) object : ParameterizedTypeReference<List<Foo>>() {}, var1, var2) }
} }
@Test @Test
fun `exchange with reified type parameters, String, HttpMethod, HttpEntity and Map`() { fun `exchange with reified type parameters, String, HttpMethod, HttpEntity and Map`() {
val url = "https://spring.io" val url = "https://spring.io"
val method = HttpMethod.GET val method = HttpMethod.GET
val entity = mock<HttpEntity<Foo>>() val entity = mockk<HttpEntity<Foo>>()
val vars = mapOf(Pair("key1", "value1"), Pair("key2", "value2")) val vars = mapOf(Pair("key1", "value1"), Pair("key2", "value2"))
template.exchange<List<Foo>>(url, method, entity, vars) template.exchange<List<Foo>>(url, method, entity, vars)
verify(template, times(1)).exchange(url, method, entity, verify(exactly = 1) { template.exchange(url, method, entity,
object : ParameterizedTypeReference<List<Foo>>() {}, vars) object : ParameterizedTypeReference<List<Foo>>() {}, vars) }
} }
@Test @Test
fun `exchange with reified type parameters, String, HttpMethod, HttpEntity`() { fun `exchange with reified type parameters, String, HttpMethod, HttpEntity`() {
val url = "https://spring.io" val url = "https://spring.io"
val method = HttpMethod.GET val method = HttpMethod.GET
val entity = mock<HttpEntity<Foo>>() val entity = mockk<HttpEntity<Foo>>()
template.exchange<List<Foo>>(url, method, entity) template.exchange<List<Foo>>(url, method, entity)
verify(template, times(1)).exchange(url, method, entity, verify(exactly = 1) { template.exchange(url, method, entity,
object : ParameterizedTypeReference<List<Foo>>() {}) object : ParameterizedTypeReference<List<Foo>>() {}) }
} }
@Test @Test
fun `exchange with reified type parameters and HttpEntity`() { fun `exchange with reified type parameters and HttpEntity`() {
val entity = mock<RequestEntity<Foo>>() val entity = mockk<RequestEntity<Foo>>()
template.exchange<List<Foo>>(entity) template.exchange<List<Foo>>(entity)
verify(template, times(1)).exchange(entity, verify(exactly = 1) { template.exchange(entity,
object : ParameterizedTypeReference<List<Foo>>() {}) object : ParameterizedTypeReference<List<Foo>>() {}) }
} }
@Test @Test
@ -243,8 +236,8 @@ class TestRestTemplateExtensionsTests {
val url = "https://spring.io" val url = "https://spring.io"
val method = HttpMethod.GET val method = HttpMethod.GET
template.exchange<List<Foo>>(url, method) template.exchange<List<Foo>>(url, method)
verify(template, times(1)).exchange(url, method, null, verify(exactly = 1) { template.exchange(url, method, null,
object : ParameterizedTypeReference<List<Foo>>() {}) object : ParameterizedTypeReference<List<Foo>>() {}) }
} }
@Test @Test