Skip to content

Commit b08c29c

Browse files
committed
convert GeoLocationServiceImpl test to Kluent
1 parent ad13f89 commit b08c29c

File tree

1 file changed

+34
-76
lines changed

1 file changed

+34
-76
lines changed
Lines changed: 34 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
package org.learning.by.example.reactive.kotlin.microservices.KotlinReactiveMS.services
22

3-
import com.natpryce.hamkrest.assertion.assert
4-
import com.natpryce.hamkrest.equalTo
5-
import com.natpryce.hamkrest.isA
6-
import com.nhaarman.mockito_kotlin.*
3+
import com.nhaarman.mockito_kotlin.any
4+
import org.amshove.kluent.`should be instance of`
5+
import org.amshove.kluent.`should be`
6+
import org.amshove.kluent.`should equal`
77
import org.junit.jupiter.api.DisplayName
88
import org.junit.jupiter.api.Test
99
import org.learning.by.example.reactive.kotlin.microservices.KotlinReactiveMS.exceptions.GeoLocationNotFoundException
1010
import org.learning.by.example.reactive.kotlin.microservices.KotlinReactiveMS.exceptions.GetGeoLocationException
1111
import org.learning.by.example.reactive.kotlin.microservices.KotlinReactiveMS.exceptions.InvalidParametersException
1212
import org.learning.by.example.reactive.kotlin.microservices.KotlinReactiveMS.model.GeoLocationResponse
1313
import org.learning.by.example.reactive.kotlin.microservices.KotlinReactiveMS.model.GeographicCoordinates
14-
import org.learning.by.example.reactive.kotlin.microservices.KotlinReactiveMS.test.getMonoFromJsonPath
15-
import org.learning.by.example.reactive.kotlin.microservices.KotlinReactiveMS.test.isNull
16-
import org.learning.by.example.reactive.kotlin.microservices.KotlinReactiveMS.test.mockWebClient
14+
import org.learning.by.example.reactive.kotlin.microservices.KotlinReactiveMS.test.*
1715
import org.learning.by.example.reactive.kotlin.microservices.KotlinReactiveMS.test.tags.UnitTest
1816
import org.springframework.boot.test.mock.mockito.SpyBean
1917
import reactor.core.publisher.Mono
@@ -49,156 +47,116 @@ internal class GeoLocationServiceImplTest {
4947

5048
@Test
5149
fun getMockingWebClientTest() {
52-
53-
geoLocationServiceImpl.webClient = mockWebClient(geoLocationServiceImpl.webClient, LOCATION_OK)
50+
geoLocationServiceImpl.webClient = geoLocationServiceImpl.webClient `with mock response` LOCATION_OK
5451

5552
val geoLocationResponse = GOOGLE_ADDRESS_MONO
5653
.transform(geoLocationServiceImpl::buildUrl)
5754
.transform(geoLocationServiceImpl::get).block()
55+
geoLocationResponse.status `should equal` OK_STATUS
5856

59-
assert.that(geoLocationResponse.status, equalTo(OK_STATUS))
60-
61-
reset(geoLocationServiceImpl.webClient)
57+
geoLocationServiceImpl.webClient reset `mock responses`
6258
}
6359

6460
@Test
6561
fun fromAddressTest() {
66-
67-
doReturn(LOCATION_OK).whenever(geoLocationServiceImpl).get(any())
62+
(geoLocationServiceImpl `will return` LOCATION_OK).get(any())
6863

6964
val geographicCoordinates = geoLocationServiceImpl.fromAddress(GOOGLE_ADDRESS_MONO).block()
65+
geographicCoordinates `should equal` GeographicCoordinates(GOOGLE_LAT, GOOGLE_LNG)
7066

71-
assert.that(geographicCoordinates, equalTo(GeographicCoordinates(GOOGLE_LAT, GOOGLE_LNG)))
72-
73-
reset(geoLocationServiceImpl)
67+
geoLocationServiceImpl reset `mock responses`
7468
}
7569

7670
@Test
7771
fun fromAddressNotFoundTest() {
7872

79-
doReturn(LOCATION_NOT_FOUND).whenever(geoLocationServiceImpl).get(any())
73+
(geoLocationServiceImpl `will return` LOCATION_NOT_FOUND).get(any())
8074

8175
val geographicCoordinates: GeographicCoordinates? = geoLocationServiceImpl.fromAddress(GOOGLE_ADDRESS_MONO)
8276
.onErrorResume {
83-
assert.that(it, isA<GeoLocationNotFoundException>())
77+
it `should be instance of` GeoLocationNotFoundException::class
8478
Mono.empty()
8579
}
8680
.block()
81+
geographicCoordinates `should be` null
8782

88-
assert.that(geographicCoordinates, isNull())
89-
90-
verify(geoLocationServiceImpl, times(1)).fromAddress(any())
91-
verify(geoLocationServiceImpl, times(1)).get(any())
92-
verify(geoLocationServiceImpl, times(1)).buildUrl(any())
93-
verify(geoLocationServiceImpl, times(1)).geometryLocation(any())
94-
95-
reset(geoLocationServiceImpl)
83+
geoLocationServiceImpl reset `mock responses`
9684
}
9785

9886
@Test
9987
fun fromAddressEmptyTest() {
100-
101-
doReturn(LOCATION_EMPTY).whenever(geoLocationServiceImpl).get(any())
88+
(geoLocationServiceImpl `will return` LOCATION_EMPTY).get(any())
10289

10390
val geographicCoordinates: GeographicCoordinates? = geoLocationServiceImpl.fromAddress(GOOGLE_ADDRESS_MONO)
10491
.onErrorResume {
105-
assert.that(it, isA<GetGeoLocationException>())
92+
it `should be instance of` GetGeoLocationException::class
10693
Mono.empty()
10794
}
10895
.block()
96+
geographicCoordinates `should be` null
10997

110-
assert.that(geographicCoordinates, isNull())
111-
112-
verify(geoLocationServiceImpl, times(1)).fromAddress(any())
113-
verify(geoLocationServiceImpl, times(1)).get(any())
114-
verify(geoLocationServiceImpl, times(1)).buildUrl(any())
115-
verify(geoLocationServiceImpl, times(1)).geometryLocation(any())
116-
117-
reset(geoLocationServiceImpl)
98+
geoLocationServiceImpl reset `mock responses`
11899
}
119100

120101

121102
@Test
122103
fun fromAddressWrongStatusTest() {
123-
124-
doReturn(LOCATION_WRONG_STATUS).whenever(geoLocationServiceImpl).get(any())
104+
(geoLocationServiceImpl `will return` LOCATION_WRONG_STATUS).get(any())
125105

126106
val geographicCoordinates: GeographicCoordinates? = geoLocationServiceImpl.fromAddress(GOOGLE_ADDRESS_MONO)
127107
.onErrorResume {
128-
assert.that(it, isA<GetGeoLocationException>())
108+
it `should be instance of` GetGeoLocationException::class
129109
Mono.empty()
130110
}
131111
.block()
112+
geographicCoordinates `should be` null
132113

133-
assert.that(geographicCoordinates, isNull())
134-
135-
verify(geoLocationServiceImpl, times(1)).fromAddress(any())
136-
verify(geoLocationServiceImpl, times(1)).get(any())
137-
verify(geoLocationServiceImpl, times(1)).buildUrl(any())
138-
verify(geoLocationServiceImpl, times(1)).geometryLocation(any())
139-
140-
reset(geoLocationServiceImpl)
114+
geoLocationServiceImpl reset `mock responses`
141115
}
142116

143117
@Test
144118
fun fromAddressExceptionTest() {
145-
146-
doReturn(LOCATION_EXCEPTION).whenever(geoLocationServiceImpl).get(any())
119+
(geoLocationServiceImpl `will return` LOCATION_EXCEPTION).get(any())
147120

148121
val geographicCoordinates: GeographicCoordinates? = geoLocationServiceImpl.fromAddress(GOOGLE_ADDRESS_MONO)
149122
.onErrorResume {
150-
assert.that(it, isA<GetGeoLocationException>())
123+
it `should be instance of` GetGeoLocationException::class
151124
Mono.empty()
152125
}
153126
.block()
127+
geographicCoordinates `should be` null
154128

155-
assert.that(geographicCoordinates, isNull())
156-
157-
verify(geoLocationServiceImpl, times(1)).fromAddress(any())
158-
verify(geoLocationServiceImpl, times(1)).get(any())
159-
verify(geoLocationServiceImpl, times(1)).buildUrl(any())
160-
verify(geoLocationServiceImpl, times(1)).geometryLocation(any())
161-
162-
reset(geoLocationServiceImpl)
129+
geoLocationServiceImpl reset `mock responses`
163130
}
164131

165132
@Test
166133
fun fromAddressBigExceptionTest() {
167-
168-
doReturn(BIG_EXCEPTION).whenever(geoLocationServiceImpl).get(any())
134+
(geoLocationServiceImpl `will return` BIG_EXCEPTION).get(any())
169135

170136
val geographicCoordinates: GeographicCoordinates? = geoLocationServiceImpl.fromAddress(GOOGLE_ADDRESS_MONO)
171137
.onErrorResume {
172-
assert.that(it, isA<GetGeoLocationException>())
138+
it `should be instance of` GetGeoLocationException::class
173139
Mono.empty()
174140
}
175141
.block()
142+
geographicCoordinates `should be` null
176143

177-
assert.that(geographicCoordinates, isNull())
178-
179-
verify(geoLocationServiceImpl, times(1)).fromAddress(any())
180-
verify(geoLocationServiceImpl, times(1)).get(any())
181-
verify(geoLocationServiceImpl, times(1)).buildUrl(any())
182-
verify(geoLocationServiceImpl, times(1)).geometryLocation(any())
183-
184-
reset(geoLocationServiceImpl)
144+
geoLocationServiceImpl reset `mock responses`
185145
}
186146

187147

188148
@Test
189149
fun buildUrlTest() {
190150
val url = GOOGLE_ADDRESS_MONO.transform(geoLocationServiceImpl::buildUrl).block()
191-
192-
assert.that(url, equalTo(geoLocationServiceImpl.endPoint + GOOGLE_ADDRESS_IN_PARAMS))
151+
url `should equal` geoLocationServiceImpl.endPoint + GOOGLE_ADDRESS_IN_PARAMS
193152
}
194153

195154
@Test
196155
fun buildUrlEmptyAddressTest() {
197156
val url = "".toMono().transform(geoLocationServiceImpl::buildUrl).onErrorResume {
198-
assert.that(it, isA<InvalidParametersException>())
157+
it `should be instance of` InvalidParametersException::class
199158
Mono.empty()
200159
}.block()
201-
202-
assert.that(url, isNull())
160+
url `should be` null
203161
}
204162
}

0 commit comments

Comments
 (0)