|
1 | 1 | package org.learning.by.example.reactive.kotlin.microservices.KotlinReactiveMS.services
|
2 | 2 |
|
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` |
7 | 7 | import org.junit.jupiter.api.DisplayName
|
8 | 8 | import org.junit.jupiter.api.Test
|
9 | 9 | import org.learning.by.example.reactive.kotlin.microservices.KotlinReactiveMS.exceptions.GeoLocationNotFoundException
|
10 | 10 | import org.learning.by.example.reactive.kotlin.microservices.KotlinReactiveMS.exceptions.GetGeoLocationException
|
11 | 11 | import org.learning.by.example.reactive.kotlin.microservices.KotlinReactiveMS.exceptions.InvalidParametersException
|
12 | 12 | import org.learning.by.example.reactive.kotlin.microservices.KotlinReactiveMS.model.GeoLocationResponse
|
13 | 13 | 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.* |
17 | 15 | import org.learning.by.example.reactive.kotlin.microservices.KotlinReactiveMS.test.tags.UnitTest
|
18 | 16 | import org.springframework.boot.test.mock.mockito.SpyBean
|
19 | 17 | import reactor.core.publisher.Mono
|
@@ -49,156 +47,116 @@ internal class GeoLocationServiceImplTest {
|
49 | 47 |
|
50 | 48 | @Test
|
51 | 49 | fun getMockingWebClientTest() {
|
52 |
| - |
53 |
| - geoLocationServiceImpl.webClient = mockWebClient(geoLocationServiceImpl.webClient, LOCATION_OK) |
| 50 | + geoLocationServiceImpl.webClient = geoLocationServiceImpl.webClient `with mock response` LOCATION_OK |
54 | 51 |
|
55 | 52 | val geoLocationResponse = GOOGLE_ADDRESS_MONO
|
56 | 53 | .transform(geoLocationServiceImpl::buildUrl)
|
57 | 54 | .transform(geoLocationServiceImpl::get).block()
|
| 55 | + geoLocationResponse.status `should equal` OK_STATUS |
58 | 56 |
|
59 |
| - assert.that(geoLocationResponse.status, equalTo(OK_STATUS)) |
60 |
| - |
61 |
| - reset(geoLocationServiceImpl.webClient) |
| 57 | + geoLocationServiceImpl.webClient reset `mock responses` |
62 | 58 | }
|
63 | 59 |
|
64 | 60 | @Test
|
65 | 61 | fun fromAddressTest() {
|
66 |
| - |
67 |
| - doReturn(LOCATION_OK).whenever(geoLocationServiceImpl).get(any()) |
| 62 | + (geoLocationServiceImpl `will return` LOCATION_OK).get(any()) |
68 | 63 |
|
69 | 64 | val geographicCoordinates = geoLocationServiceImpl.fromAddress(GOOGLE_ADDRESS_MONO).block()
|
| 65 | + geographicCoordinates `should equal` GeographicCoordinates(GOOGLE_LAT, GOOGLE_LNG) |
70 | 66 |
|
71 |
| - assert.that(geographicCoordinates, equalTo(GeographicCoordinates(GOOGLE_LAT, GOOGLE_LNG))) |
72 |
| - |
73 |
| - reset(geoLocationServiceImpl) |
| 67 | + geoLocationServiceImpl reset `mock responses` |
74 | 68 | }
|
75 | 69 |
|
76 | 70 | @Test
|
77 | 71 | fun fromAddressNotFoundTest() {
|
78 | 72 |
|
79 |
| - doReturn(LOCATION_NOT_FOUND).whenever(geoLocationServiceImpl).get(any()) |
| 73 | + (geoLocationServiceImpl `will return` LOCATION_NOT_FOUND).get(any()) |
80 | 74 |
|
81 | 75 | val geographicCoordinates: GeographicCoordinates? = geoLocationServiceImpl.fromAddress(GOOGLE_ADDRESS_MONO)
|
82 | 76 | .onErrorResume {
|
83 |
| - assert.that(it, isA<GeoLocationNotFoundException>()) |
| 77 | + it `should be instance of` GeoLocationNotFoundException::class |
84 | 78 | Mono.empty()
|
85 | 79 | }
|
86 | 80 | .block()
|
| 81 | + geographicCoordinates `should be` null |
87 | 82 |
|
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` |
96 | 84 | }
|
97 | 85 |
|
98 | 86 | @Test
|
99 | 87 | fun fromAddressEmptyTest() {
|
100 |
| - |
101 |
| - doReturn(LOCATION_EMPTY).whenever(geoLocationServiceImpl).get(any()) |
| 88 | + (geoLocationServiceImpl `will return` LOCATION_EMPTY).get(any()) |
102 | 89 |
|
103 | 90 | val geographicCoordinates: GeographicCoordinates? = geoLocationServiceImpl.fromAddress(GOOGLE_ADDRESS_MONO)
|
104 | 91 | .onErrorResume {
|
105 |
| - assert.that(it, isA<GetGeoLocationException>()) |
| 92 | + it `should be instance of` GetGeoLocationException::class |
106 | 93 | Mono.empty()
|
107 | 94 | }
|
108 | 95 | .block()
|
| 96 | + geographicCoordinates `should be` null |
109 | 97 |
|
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` |
118 | 99 | }
|
119 | 100 |
|
120 | 101 |
|
121 | 102 | @Test
|
122 | 103 | fun fromAddressWrongStatusTest() {
|
123 |
| - |
124 |
| - doReturn(LOCATION_WRONG_STATUS).whenever(geoLocationServiceImpl).get(any()) |
| 104 | + (geoLocationServiceImpl `will return` LOCATION_WRONG_STATUS).get(any()) |
125 | 105 |
|
126 | 106 | val geographicCoordinates: GeographicCoordinates? = geoLocationServiceImpl.fromAddress(GOOGLE_ADDRESS_MONO)
|
127 | 107 | .onErrorResume {
|
128 |
| - assert.that(it, isA<GetGeoLocationException>()) |
| 108 | + it `should be instance of` GetGeoLocationException::class |
129 | 109 | Mono.empty()
|
130 | 110 | }
|
131 | 111 | .block()
|
| 112 | + geographicCoordinates `should be` null |
132 | 113 |
|
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` |
141 | 115 | }
|
142 | 116 |
|
143 | 117 | @Test
|
144 | 118 | fun fromAddressExceptionTest() {
|
145 |
| - |
146 |
| - doReturn(LOCATION_EXCEPTION).whenever(geoLocationServiceImpl).get(any()) |
| 119 | + (geoLocationServiceImpl `will return` LOCATION_EXCEPTION).get(any()) |
147 | 120 |
|
148 | 121 | val geographicCoordinates: GeographicCoordinates? = geoLocationServiceImpl.fromAddress(GOOGLE_ADDRESS_MONO)
|
149 | 122 | .onErrorResume {
|
150 |
| - assert.that(it, isA<GetGeoLocationException>()) |
| 123 | + it `should be instance of` GetGeoLocationException::class |
151 | 124 | Mono.empty()
|
152 | 125 | }
|
153 | 126 | .block()
|
| 127 | + geographicCoordinates `should be` null |
154 | 128 |
|
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` |
163 | 130 | }
|
164 | 131 |
|
165 | 132 | @Test
|
166 | 133 | fun fromAddressBigExceptionTest() {
|
167 |
| - |
168 |
| - doReturn(BIG_EXCEPTION).whenever(geoLocationServiceImpl).get(any()) |
| 134 | + (geoLocationServiceImpl `will return` BIG_EXCEPTION).get(any()) |
169 | 135 |
|
170 | 136 | val geographicCoordinates: GeographicCoordinates? = geoLocationServiceImpl.fromAddress(GOOGLE_ADDRESS_MONO)
|
171 | 137 | .onErrorResume {
|
172 |
| - assert.that(it, isA<GetGeoLocationException>()) |
| 138 | + it `should be instance of` GetGeoLocationException::class |
173 | 139 | Mono.empty()
|
174 | 140 | }
|
175 | 141 | .block()
|
| 142 | + geographicCoordinates `should be` null |
176 | 143 |
|
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` |
185 | 145 | }
|
186 | 146 |
|
187 | 147 |
|
188 | 148 | @Test
|
189 | 149 | fun buildUrlTest() {
|
190 | 150 | 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 |
193 | 152 | }
|
194 | 153 |
|
195 | 154 | @Test
|
196 | 155 | fun buildUrlEmptyAddressTest() {
|
197 | 156 | val url = "".toMono().transform(geoLocationServiceImpl::buildUrl).onErrorResume {
|
198 |
| - assert.that(it, isA<InvalidParametersException>()) |
| 157 | + it `should be instance of` InvalidParametersException::class |
199 | 158 | Mono.empty()
|
200 | 159 | }.block()
|
201 |
| - |
202 |
| - assert.that(url, isNull()) |
| 160 | + url `should be` null |
203 | 161 | }
|
204 | 162 | }
|
0 commit comments