mirror of
https://github.com/Dannecron/spring-boot-demo.git
synced 2025-12-26 00:32:34 +03:00
add global exception handler for controllers
add new not found exception, add new test for ShopController
This commit is contained in:
@@ -11,6 +11,8 @@ 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 java.time.OffsetDateTime
|
||||
import java.util.*
|
||||
import kotlin.test.Test
|
||||
|
||||
@WebMvcTest(ShopController::class)
|
||||
@@ -20,30 +22,35 @@ class ShopControllerTest(@Autowired val mockMvc: MockMvc) {
|
||||
|
||||
@Test
|
||||
fun commonInfo_shouldSeeSuccessResponseJson() {
|
||||
val productOne = makeProduct(id = 1, name = "one", price = 11.2)
|
||||
val productTwo = makeProduct(id = 2, name = "two", price = 13.2)
|
||||
val productThree = makeProduct(id = 3, name = "three", price = 15.2)
|
||||
val productFour = makeProduct(id = 4, name = "four", price = 14.2)
|
||||
|
||||
val shopMock = Shop(name="shop", customers= listOf(
|
||||
Customer(
|
||||
name = "cus-one",
|
||||
city = City(name= "city-one"),
|
||||
orders = listOf(
|
||||
Order(products = listOf(Product(name = "one", price = 11.2)), isDelivered = false),
|
||||
Order(products = listOf(Product(name = "two", price = 13.2)), isDelivered = false),
|
||||
Order(products = listOf(Product(name = "three", price = 15.2)), isDelivered = true),
|
||||
Order(products = listOf(productOne), isDelivered = false),
|
||||
Order(products = listOf(productTwo), isDelivered = false),
|
||||
Order(products = listOf(productThree), isDelivered = true),
|
||||
)
|
||||
),
|
||||
Customer(
|
||||
name = "cus-two",
|
||||
city = City(name= "city-two"),
|
||||
orders = listOf(
|
||||
Order(products = listOf(Product(name = "one", price = 12.2)), isDelivered = false),
|
||||
Order(products = listOf(Product(name = "two", price = 13.2)), isDelivered = true),
|
||||
Order(products = listOf(Product(name = "four", price = 14.2)), isDelivered = true),
|
||||
Order(products = listOf(productOne), isDelivered = false),
|
||||
Order(products = listOf(productTwo), isDelivered = true),
|
||||
Order(products = listOf(productFour), isDelivered = true),
|
||||
)
|
||||
),
|
||||
))
|
||||
|
||||
val expectedJson: String = """{
|
||||
|"customers": {"withMoreUndeliveredOrdersThanDelivered": ["cus-one"]},
|
||||
|"products": {"orderedByAllCustomers": ["two"]}
|
||||
|"products": {"orderedByAllCustomers": ["one", "two"]}
|
||||
|}""".trimMargin()
|
||||
|
||||
whenever(
|
||||
@@ -55,4 +62,28 @@ class ShopControllerTest(@Autowired val mockMvc: MockMvc) {
|
||||
.andExpect(content().contentType("application/json"))
|
||||
.andExpect(content().json(expectedJson))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun commonInfo_shouldSeeNotFoundResponse() {
|
||||
whenever(
|
||||
shopProvider.getRandomShop()
|
||||
) doReturn (null)
|
||||
|
||||
mockMvc.perform(get("/shop/common-info"))
|
||||
.andExpect(status().isNotFound)
|
||||
.andExpect(content().contentType("application/json"))
|
||||
.andExpect(content().json("""{"status":"not found"}"""))
|
||||
}
|
||||
|
||||
private fun makeProduct(id: Long, name: String, price: Double): Product {
|
||||
return Product(
|
||||
id = id,
|
||||
guid = UUID.randomUUID(),
|
||||
name = name,
|
||||
description = null,
|
||||
price = (price * 100).toInt(),
|
||||
createdAt = OffsetDateTime.now(),
|
||||
updatedAt = null,
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user