mirror of
https://github.com/Dannecron/spring-boot-demo.git
synced 2025-12-25 16:22:35 +03:00
some refactoring
This commit is contained in:
@@ -49,8 +49,7 @@ class ProductControllerTest(@Autowired val mockMvc: MockMvc): BaseUnitTest() {
|
|||||||
|
|
||||||
whenever(productService.findByGuid(
|
whenever(productService.findByGuid(
|
||||||
eq(guid),
|
eq(guid),
|
||||||
))
|
)) doReturn product
|
||||||
.thenReturn(product)
|
|
||||||
|
|
||||||
mockMvc.get("/api/product/$guid")
|
mockMvc.get("/api/product/$guid")
|
||||||
.andExpect { status { status { isOk() } } }
|
.andExpect { status { status { isOk() } } }
|
||||||
@@ -68,8 +67,7 @@ class ProductControllerTest(@Autowired val mockMvc: MockMvc): BaseUnitTest() {
|
|||||||
|
|
||||||
whenever(productService.findByGuid(
|
whenever(productService.findByGuid(
|
||||||
eq(guid),
|
eq(guid),
|
||||||
))
|
)) doReturn null
|
||||||
.thenReturn(null)
|
|
||||||
|
|
||||||
mockMvc.get("/api/product/$guid")
|
mockMvc.get("/api/product/$guid")
|
||||||
.andExpect { status { status { isNotFound() } } }
|
.andExpect { status { status { isNotFound() } } }
|
||||||
@@ -122,8 +120,7 @@ class ProductControllerTest(@Autowired val mockMvc: MockMvc): BaseUnitTest() {
|
|||||||
eq(name),
|
eq(name),
|
||||||
eq(price),
|
eq(price),
|
||||||
eq(description)
|
eq(description)
|
||||||
))
|
)) doReturn Product(
|
||||||
.thenReturn(Product(
|
|
||||||
id = productId,
|
id = productId,
|
||||||
guid = UUID.randomUUID(),
|
guid = UUID.randomUUID(),
|
||||||
name = name,
|
name = name,
|
||||||
@@ -132,7 +129,7 @@ class ProductControllerTest(@Autowired val mockMvc: MockMvc): BaseUnitTest() {
|
|||||||
createdAt = OffsetDateTime.now(),
|
createdAt = OffsetDateTime.now(),
|
||||||
updatedAt = null,
|
updatedAt = null,
|
||||||
deletedAt = null,
|
deletedAt = null,
|
||||||
))
|
)
|
||||||
|
|
||||||
mockMvc.post("/api/product") {
|
mockMvc.post("/api/product") {
|
||||||
contentType = MediaType.APPLICATION_JSON
|
contentType = MediaType.APPLICATION_JSON
|
||||||
@@ -185,8 +182,7 @@ class ProductControllerTest(@Autowired val mockMvc: MockMvc): BaseUnitTest() {
|
|||||||
|
|
||||||
whenever(productService.delete(
|
whenever(productService.delete(
|
||||||
eq(guid),
|
eq(guid),
|
||||||
))
|
)) doReturn Product(
|
||||||
.thenReturn(Product(
|
|
||||||
id = 2133,
|
id = 2133,
|
||||||
guid = guid,
|
guid = guid,
|
||||||
name = "name",
|
name = "name",
|
||||||
@@ -195,7 +191,7 @@ class ProductControllerTest(@Autowired val mockMvc: MockMvc): BaseUnitTest() {
|
|||||||
createdAt = OffsetDateTime.now(),
|
createdAt = OffsetDateTime.now(),
|
||||||
updatedAt = null,
|
updatedAt = null,
|
||||||
deletedAt = OffsetDateTime.now(),
|
deletedAt = OffsetDateTime.now(),
|
||||||
))
|
)
|
||||||
|
|
||||||
mockMvc.delete("/api/product/${guid}")
|
mockMvc.delete("/api/product/${guid}")
|
||||||
.andExpect { status { status { isOk() } } }
|
.andExpect { status { status { isOk() } } }
|
||||||
|
|||||||
@@ -8,10 +8,9 @@ import org.mockito.kotlin.whenever
|
|||||||
import org.springframework.beans.factory.annotation.Autowired
|
import org.springframework.beans.factory.annotation.Autowired
|
||||||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
|
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
|
||||||
import org.springframework.boot.test.mock.mockito.MockBean
|
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.MockMvc
|
||||||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
|
import org.springframework.test.web.servlet.get
|
||||||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.content
|
|
||||||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status
|
|
||||||
import java.time.OffsetDateTime
|
import java.time.OffsetDateTime
|
||||||
import java.util.*
|
import java.util.*
|
||||||
import kotlin.test.Test
|
import kotlin.test.Test
|
||||||
@@ -56,24 +55,24 @@ class ShopControllerTest(@Autowired val mockMvc: MockMvc): BaseUnitTest() {
|
|||||||
|
|
||||||
whenever(
|
whenever(
|
||||||
shopProvider.getRandomShop()
|
shopProvider.getRandomShop()
|
||||||
) doReturn (shopMock)
|
) doReturn shopMock
|
||||||
|
|
||||||
mockMvc.perform(get("/shop/common-info"))
|
mockMvc.get("/shop/common-info")
|
||||||
.andExpect(status().isOk)
|
.andExpect{ status { status { isOk() } } }
|
||||||
.andExpect(content().contentType("application/json"))
|
.andExpect { content { contentType(MediaType.APPLICATION_JSON) } }
|
||||||
.andExpect(content().json(expectedJson))
|
.andExpect { content { json(expectedJson) } }
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun commonInfo_shouldSeeNotFoundResponse() {
|
fun commonInfo_shouldSeeNotFoundResponse() {
|
||||||
whenever(
|
whenever(
|
||||||
shopProvider.getRandomShop()
|
shopProvider.getRandomShop()
|
||||||
) doReturn (null)
|
) doReturn null
|
||||||
|
|
||||||
mockMvc.perform(get("/shop/common-info"))
|
mockMvc.get("/shop/common-info")
|
||||||
.andExpect(status().isNotFound)
|
.andExpect { status { status { isNotFound() } } }
|
||||||
.andExpect(content().contentType("application/json"))
|
.andExpect { content { contentType(MediaType.APPLICATION_JSON) } }
|
||||||
.andExpect(content().json("""{"status":"not found"}"""))
|
.andExpect { content { json("""{"status":"not found"}""") } }
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun makeProduct(id: Long, name: String, price: Double): Product = Product(
|
private fun makeProduct(id: Long, name: String, price: Double): Product = Product(
|
||||||
|
|||||||
@@ -53,9 +53,8 @@ class ProductServiceImplTest: BaseUnitTest() {
|
|||||||
deletedAt = OffsetDateTime.now(),
|
deletedAt = OffsetDateTime.now(),
|
||||||
)
|
)
|
||||||
|
|
||||||
whenever(productRepository.findByGuid(eq(guid)))
|
whenever(productRepository.findByGuid(eq(guid))) doReturn product
|
||||||
.thenReturn(product)
|
whenever(producer.produceProductInfo(defaultTopic, product)) doAnswer {}
|
||||||
whenever(producer.produceProductInfo(defaultTopic, product)).doAnswer{}
|
|
||||||
|
|
||||||
productService.syncToKafka(guid, null)
|
productService.syncToKafka(guid, null)
|
||||||
}
|
}
|
||||||
@@ -65,8 +64,7 @@ class ProductServiceImplTest: BaseUnitTest() {
|
|||||||
val specificTopic = "specificNotice"
|
val specificTopic = "specificNotice"
|
||||||
val guid = UUID.randomUUID()
|
val guid = UUID.randomUUID()
|
||||||
|
|
||||||
whenever(productRepository.findByGuid(eq(guid)))
|
whenever(productRepository.findByGuid(eq(guid))) doReturn null
|
||||||
.thenReturn(null)
|
|
||||||
|
|
||||||
assertThrows<ProductNotFoundException> {
|
assertThrows<ProductNotFoundException> {
|
||||||
productService.syncToKafka(guid, specificTopic)
|
productService.syncToKafka(guid, specificTopic)
|
||||||
@@ -91,10 +89,8 @@ class ProductServiceImplTest: BaseUnitTest() {
|
|||||||
deletedAt = OffsetDateTime.now(),
|
deletedAt = OffsetDateTime.now(),
|
||||||
)
|
)
|
||||||
|
|
||||||
whenever(productRepository.findByGuid(eq(guid)))
|
whenever(productRepository.findByGuid(eq(guid))) doReturn product
|
||||||
.thenReturn(product)
|
whenever(producer.produceProductInfo(specificTopic, product)) doThrow InvalidArgumentException("some error")
|
||||||
whenever(producer.produceProductInfo(specificTopic, product))
|
|
||||||
.doThrow(InvalidArgumentException("some error"))
|
|
||||||
|
|
||||||
assertThrows< InvalidArgumentException> {
|
assertThrows< InvalidArgumentException> {
|
||||||
productService.syncToKafka(guid, specificTopic)
|
productService.syncToKafka(guid, specificTopic)
|
||||||
|
|||||||
@@ -50,14 +50,12 @@ class ProducerImplTest: BaseUnitTest() {
|
|||||||
|
|
||||||
val captor = argumentCaptor<Message<String>>()
|
val captor = argumentCaptor<Message<String>>()
|
||||||
|
|
||||||
whenever(kafkaTemplate.send(captor.capture()))
|
whenever(kafkaTemplate.send(captor.capture())) doReturn CompletableFuture<SendResult<String, Any>>()
|
||||||
.doReturn(CompletableFuture<SendResult<String, Any>>())
|
|
||||||
|
|
||||||
whenever(schemaValidator.validate(
|
whenever(schemaValidator.validate(
|
||||||
eq("product-sync"),
|
eq("product-sync"),
|
||||||
eq(Json.encodeToJsonElement(product))
|
eq(Json.encodeToJsonElement(product))
|
||||||
))
|
)) doAnswer { }
|
||||||
.doAnswer { }
|
|
||||||
|
|
||||||
producerImpl.produceProductInfo(topic, product)
|
producerImpl.produceProductInfo(topic, product)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user