From 80423338f57477b560c94007cd3cb9ec4dc9595b Mon Sep 17 00:00:00 2001 From: Denis Savosin Date: Fri, 11 Oct 2024 12:11:02 +0700 Subject: [PATCH] some refactoring --- .../http/controllers/ProductControllerTest.kt | 48 +++++++++---------- .../http/controllers/ShopControllerTest.kt | 25 +++++----- .../product/ProductServiceImplTest.kt | 14 ++---- .../demo/services/kafka/ProducerImplTest.kt | 6 +-- 4 files changed, 41 insertions(+), 52 deletions(-) diff --git a/src/test/kotlin/com/example/demo/http/controllers/ProductControllerTest.kt b/src/test/kotlin/com/example/demo/http/controllers/ProductControllerTest.kt index 7dd2b2b..4c76b2f 100644 --- a/src/test/kotlin/com/example/demo/http/controllers/ProductControllerTest.kt +++ b/src/test/kotlin/com/example/demo/http/controllers/ProductControllerTest.kt @@ -49,8 +49,7 @@ class ProductControllerTest(@Autowired val mockMvc: MockMvc): BaseUnitTest() { whenever(productService.findByGuid( eq(guid), - )) - .thenReturn(product) + )) doReturn product mockMvc.get("/api/product/$guid") .andExpect { status { status { isOk() } } } @@ -68,8 +67,7 @@ class ProductControllerTest(@Autowired val mockMvc: MockMvc): BaseUnitTest() { whenever(productService.findByGuid( eq(guid), - )) - .thenReturn(null) + )) doReturn null mockMvc.get("/api/product/$guid") .andExpect { status { status { isNotFound() } } } @@ -122,17 +120,16 @@ class ProductControllerTest(@Autowired val mockMvc: MockMvc): BaseUnitTest() { eq(name), eq(price), eq(description) - )) - .thenReturn(Product( - id = productId, - guid = UUID.randomUUID(), - name = name, - description = description, - price = price, - createdAt = OffsetDateTime.now(), - updatedAt = null, - deletedAt = null, - )) + )) doReturn Product( + id = productId, + guid = UUID.randomUUID(), + name = name, + description = description, + price = price, + createdAt = OffsetDateTime.now(), + updatedAt = null, + deletedAt = null, + ) mockMvc.post("/api/product") { contentType = MediaType.APPLICATION_JSON @@ -185,17 +182,16 @@ class ProductControllerTest(@Autowired val mockMvc: MockMvc): BaseUnitTest() { whenever(productService.delete( eq(guid), - )) - .thenReturn(Product( - id = 2133, - guid = guid, - name = "name", - description = "description", - price = 210202, - createdAt = OffsetDateTime.now(), - updatedAt = null, - deletedAt = OffsetDateTime.now(), - )) + )) doReturn Product( + id = 2133, + guid = guid, + name = "name", + description = "description", + price = 210202, + createdAt = OffsetDateTime.now(), + updatedAt = null, + deletedAt = OffsetDateTime.now(), + ) mockMvc.delete("/api/product/${guid}") .andExpect { status { status { isOk() } } } diff --git a/src/test/kotlin/com/example/demo/http/controllers/ShopControllerTest.kt b/src/test/kotlin/com/example/demo/http/controllers/ShopControllerTest.kt index 4adbb76..fbfbeb4 100644 --- a/src/test/kotlin/com/example/demo/http/controllers/ShopControllerTest.kt +++ b/src/test/kotlin/com/example/demo/http/controllers/ShopControllerTest.kt @@ -8,10 +8,9 @@ import org.mockito.kotlin.whenever import org.springframework.beans.factory.annotation.Autowired import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest import org.springframework.boot.test.mock.mockito.MockBean +import org.springframework.http.MediaType import org.springframework.test.web.servlet.MockMvc -import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get -import org.springframework.test.web.servlet.result.MockMvcResultMatchers.content -import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status +import org.springframework.test.web.servlet.get import java.time.OffsetDateTime import java.util.* import kotlin.test.Test @@ -56,24 +55,24 @@ class ShopControllerTest(@Autowired val mockMvc: MockMvc): BaseUnitTest() { whenever( shopProvider.getRandomShop() - ) doReturn (shopMock) + ) doReturn shopMock - mockMvc.perform(get("/shop/common-info")) - .andExpect(status().isOk) - .andExpect(content().contentType("application/json")) - .andExpect(content().json(expectedJson)) + mockMvc.get("/shop/common-info") + .andExpect{ status { status { isOk() } } } + .andExpect { content { contentType(MediaType.APPLICATION_JSON) } } + .andExpect { content { json(expectedJson) } } } @Test fun commonInfo_shouldSeeNotFoundResponse() { whenever( shopProvider.getRandomShop() - ) doReturn (null) + ) doReturn null - mockMvc.perform(get("/shop/common-info")) - .andExpect(status().isNotFound) - .andExpect(content().contentType("application/json")) - .andExpect(content().json("""{"status":"not found"}""")) + mockMvc.get("/shop/common-info") + .andExpect { status { status { isNotFound() } } } + .andExpect { content { contentType(MediaType.APPLICATION_JSON) } } + .andExpect { content { json("""{"status":"not found"}""") } } } private fun makeProduct(id: Long, name: String, price: Double): Product = Product( diff --git a/src/test/kotlin/com/example/demo/services/database/product/ProductServiceImplTest.kt b/src/test/kotlin/com/example/demo/services/database/product/ProductServiceImplTest.kt index b4edb08..8bf56f2 100644 --- a/src/test/kotlin/com/example/demo/services/database/product/ProductServiceImplTest.kt +++ b/src/test/kotlin/com/example/demo/services/database/product/ProductServiceImplTest.kt @@ -53,9 +53,8 @@ class ProductServiceImplTest: BaseUnitTest() { deletedAt = OffsetDateTime.now(), ) - whenever(productRepository.findByGuid(eq(guid))) - .thenReturn(product) - whenever(producer.produceProductInfo(defaultTopic, product)).doAnswer{} + whenever(productRepository.findByGuid(eq(guid))) doReturn product + whenever(producer.produceProductInfo(defaultTopic, product)) doAnswer {} productService.syncToKafka(guid, null) } @@ -65,8 +64,7 @@ class ProductServiceImplTest: BaseUnitTest() { val specificTopic = "specificNotice" val guid = UUID.randomUUID() - whenever(productRepository.findByGuid(eq(guid))) - .thenReturn(null) + whenever(productRepository.findByGuid(eq(guid))) doReturn null assertThrows { productService.syncToKafka(guid, specificTopic) @@ -91,10 +89,8 @@ class ProductServiceImplTest: BaseUnitTest() { deletedAt = OffsetDateTime.now(), ) - whenever(productRepository.findByGuid(eq(guid))) - .thenReturn(product) - whenever(producer.produceProductInfo(specificTopic, product)) - .doThrow(InvalidArgumentException("some error")) + whenever(productRepository.findByGuid(eq(guid))) doReturn product + whenever(producer.produceProductInfo(specificTopic, product)) doThrow InvalidArgumentException("some error") assertThrows< InvalidArgumentException> { productService.syncToKafka(guid, specificTopic) diff --git a/src/test/kotlin/com/example/demo/services/kafka/ProducerImplTest.kt b/src/test/kotlin/com/example/demo/services/kafka/ProducerImplTest.kt index 60c67b7..1258823 100644 --- a/src/test/kotlin/com/example/demo/services/kafka/ProducerImplTest.kt +++ b/src/test/kotlin/com/example/demo/services/kafka/ProducerImplTest.kt @@ -50,14 +50,12 @@ class ProducerImplTest: BaseUnitTest() { val captor = argumentCaptor>() - whenever(kafkaTemplate.send(captor.capture())) - .doReturn(CompletableFuture>()) + whenever(kafkaTemplate.send(captor.capture())) doReturn CompletableFuture>() whenever(schemaValidator.validate( eq("product-sync"), eq(Json.encodeToJsonElement(product)) - )) - .doAnswer { } + )) doAnswer { } producerImpl.produceProductInfo(topic, product)