do not use ObjectMapper in tests

This commit is contained in:
Denis Savosin
2024-10-04 15:21:11 +07:00
parent 97b673d45e
commit f58a99a68d

View File

@@ -4,7 +4,6 @@ import com.example.demo.BaseUnitTest
import com.example.demo.http.responses.ResponseStatus import com.example.demo.http.responses.ResponseStatus
import com.example.demo.models.Product import com.example.demo.models.Product
import com.example.demo.services.database.product.ProductService import com.example.demo.services.database.product.ProductService
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import org.hamcrest.Matchers.contains import org.hamcrest.Matchers.contains
import org.hamcrest.Matchers.nullValue import org.hamcrest.Matchers.nullValue
import org.junit.jupiter.api.Test import org.junit.jupiter.api.Test
@@ -32,8 +31,6 @@ import java.util.*
class ProductControllerTest(@Autowired val mockMvc: MockMvc): BaseUnitTest() { class ProductControllerTest(@Autowired val mockMvc: MockMvc): BaseUnitTest() {
@MockBean @MockBean
private lateinit var productService: ProductService private lateinit var productService: ProductService
private val mapper = jacksonObjectMapper()
@Test @Test
fun getProduct_success() { fun getProduct_success() {
@@ -119,9 +116,7 @@ class ProductControllerTest(@Autowired val mockMvc: MockMvc): BaseUnitTest() {
val description = null val description = null
val price = 20000.toLong() val price = 20000.toLong()
val reqBody = mapper.writeValueAsString( val reqBody = """{"name":"$name","description":null,"price":$price}"""
mapOf("name" to name, "description" to description, "price" to price)
)
whenever(productService.create( whenever(productService.create(
eq(name), eq(name),
@@ -150,12 +145,9 @@ class ProductControllerTest(@Autowired val mockMvc: MockMvc): BaseUnitTest() {
@Test @Test
fun createProduct_badRequest_noNameParam() { fun createProduct_badRequest_noNameParam() {
val description = null
val price = 20000.toLong() val price = 20000.toLong()
val reqBody = mapper.writeValueAsString( val reqBody = """{"description":null,"price":$price}"""
mapOf("description" to description, "price" to price)
)
mockMvc.post("/api/product") { mockMvc.post("/api/product") {
contentType = MediaType.APPLICATION_JSON contentType = MediaType.APPLICATION_JSON
@@ -171,12 +163,9 @@ class ProductControllerTest(@Autowired val mockMvc: MockMvc): BaseUnitTest() {
@Test @Test
fun createProduct_badRequest_emptyName() { fun createProduct_badRequest_emptyName() {
val description = null
val price = 20000.toLong() val price = 20000.toLong()
val reqBody = mapper.writeValueAsString( val reqBody = """{"name":"","description":null,"price":$price}"""
mapOf("name" to "", "description" to description, "price" to price)
)
mockMvc.post("/api/product") { mockMvc.post("/api/product") {
contentType = MediaType.APPLICATION_JSON contentType = MediaType.APPLICATION_JSON