some refactoring

This commit is contained in:
Denis Savosin
2024-10-11 12:11:02 +07:00
parent d8b068917f
commit 80423338f5
4 changed files with 41 additions and 52 deletions

View File

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

View File

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

View File

@@ -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<ProductNotFoundException> {
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)

View File

@@ -50,14 +50,12 @@ class ProducerImplTest: BaseUnitTest() {
val captor = argumentCaptor<Message<String>>()
whenever(kafkaTemplate.send(captor.capture()))
.doReturn(CompletableFuture<SendResult<String, Any>>())
whenever(kafkaTemplate.send(captor.capture())) doReturn CompletableFuture<SendResult<String, Any>>()
whenever(schemaValidator.validate(
eq("product-sync"),
eq(Json.encodeToJsonElement(product))
))
.doAnswer { }
)) doAnswer { }
producerImpl.produceProductInfo(topic, product)