mirror of
https://github.com/Dannecron/spring-boot-demo.git
synced 2025-12-26 00:32:34 +03:00
add tests for ShopController
This commit is contained in:
@@ -14,11 +14,9 @@ import org.springframework.web.bind.annotation.RestController
|
||||
class ShopController (
|
||||
@field:Autowired private val shopProvider: ShopProvider,
|
||||
) {
|
||||
@GetMapping(value = ["/shop/common-info"], produces = ["application/json"] )
|
||||
@GetMapping(value = ["/shop/common-info"], produces = ["application/json"])
|
||||
@ResponseBody
|
||||
fun commonInfo(response: HttpServletResponse): String {
|
||||
response.contentType = "application/json"
|
||||
|
||||
val shop = shopProvider.getRandomShop()
|
||||
|
||||
if (shop == null) {
|
||||
|
||||
@@ -19,7 +19,7 @@ fun renderProductTable(): String {
|
||||
}
|
||||
}
|
||||
|
||||
val products = getProducts()
|
||||
val products = getInnerProducts()
|
||||
for ((i, product) in products.withIndex()) {
|
||||
tr {
|
||||
td (color = getCellColor(0, i+1)) {
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.example.demo.provider.html
|
||||
|
||||
data class InnerProduct(val description: String, val price: Double, val popularity: Int)
|
||||
|
||||
fun getInnerProducts(): Set<InnerProduct> {
|
||||
return setOf(
|
||||
InnerProduct("one", 12.0, 12),
|
||||
InnerProduct("two", 13.0, 20),
|
||||
InnerProduct("three", 14.0, 50)
|
||||
)
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
package com.example.demo.provider.html
|
||||
|
||||
data class Product(val description: String, val price: Double, val popularity: Int)
|
||||
|
||||
fun getProducts(): Set<Product> {
|
||||
return setOf(
|
||||
Product("one", 12.0, 12),
|
||||
Product("two", 13.0, 20),
|
||||
Product("three", 14.0, 50)
|
||||
)
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
package com.example.demo
|
||||
|
||||
import org.junit.jupiter.api.Test
|
||||
import org.springframework.boot.test.context.SpringBootTest
|
||||
|
||||
@SpringBootTest
|
||||
class DemoApplicationTests {
|
||||
|
||||
@Test
|
||||
fun contextLoads() {
|
||||
}
|
||||
|
||||
}
|
||||
@@ -11,7 +11,6 @@ import kotlin.test.Test
|
||||
|
||||
@WebMvcTest(GreetingController::class)
|
||||
class GreetingControllerTest(@Autowired val mockMvc: MockMvc) {
|
||||
|
||||
@Test
|
||||
fun greetings_shouldSeeGreetingMessage() {
|
||||
mockMvc.perform(get("/greeting"))
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
package com.example.demo.controllers
|
||||
|
||||
import com.example.demo.models.*
|
||||
import com.example.demo.provider.ShopProvider
|
||||
import org.mockito.kotlin.doReturn
|
||||
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.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 kotlin.test.Test
|
||||
|
||||
@WebMvcTest(ShopController::class)
|
||||
class ShopControllerTest(@Autowired val mockMvc: MockMvc) {
|
||||
@MockBean
|
||||
private lateinit var shopProvider: ShopProvider
|
||||
|
||||
@Test
|
||||
fun commonInfo_shouldSeeSuccessResponseJson() {
|
||||
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),
|
||||
)
|
||||
),
|
||||
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),
|
||||
)
|
||||
),
|
||||
))
|
||||
|
||||
val expectedJson: String = """{
|
||||
|"customers": {"withMoreUndeliveredOrdersThanDelivered": ["cus-one"]},
|
||||
|"products": {"orderedByAllCustomers": ["two"]}
|
||||
|}""".trimMargin()
|
||||
|
||||
whenever(
|
||||
shopProvider.getRandomShop()
|
||||
) doReturn (shopMock)
|
||||
|
||||
mockMvc.perform(get("/shop/common-info"))
|
||||
.andExpect(status().isOk)
|
||||
.andExpect(content().contentType("application/json"))
|
||||
.andExpect(content().json(expectedJson))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user