Spring-boot: Validation not working in Spring Boot 2 MockMvc integration tests.

Created on 26 Aug 2018  路  2Comments  路  Source: spring-projects/spring-boot

Recently while working on a REST service (with Spring Boot Starter Web and Data JPA in version 2.0.4.RELEASE) I have discovered that while testing controller with MockMvc validation is not fired. I have found similar issue regarding Spring Boot 1.5.0 which is already resolved #7582 but here I cannot make it work even adding additional dependencies suggested in mentioned issue. I was looking for a ways to make it work but I couldn't find any informations regarding this problem.

Example in Kotlin below:

package com.onwelo.jpk.admin

import com.google.gson.Gson
import com.onwelo.jpk.admin.configuration.AppConfig
import com.onwelo.jpk.admin.configuration.PersistenceConfig
import org.hibernate.validator.constraints.Length
import org.hibernate.validator.constraints.pl.NIP
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.http.MediaType
import org.springframework.test.context.junit.jupiter.web.SpringJUnitWebConfig
import org.springframework.test.web.servlet.MockMvc
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import org.springframework.web.servlet.config.annotation.EnableWebMvc
import javax.validation.Valid
import javax.validation.constraints.NotBlank

data class TestDTO(
        @NotBlank
        @Length(max = 3)
        private val name : String,

        @NotBlank
        @NIP
        private val nip : String
)

@RestController
@RequestMapping("/test")
class TestController {

    @PostMapping("/")
    fun test(@Valid @RequestBody dto : TestDTO) {
        println("I'm here non the less")
    }
}

@Configuration
@EnableWebMvc
class TestConf {

    @Bean
    fun testController() : TestController {
        return TestController()
    }
}

@SpringJUnitWebConfig(classes = [TestConf::class])
@AutoConfigureMockMvc
class JUnitTest(
        @Autowired
        private val mvc : MockMvc
) {

    @Test
    fun test() {
        val dto = TestDTO(name = "423432", nip = "2113")

        mvc.perform(MockMvcRequestBuilders.
                post("/test/")
                .contentType(MediaType.APPLICATION_JSON_UTF8)
                .content(Gson().toJson(dto)))
                .andExpect(status().isBadRequest)
    }
}
duplicate

Most helpful comment

I have no idea how did I check that earlier, but you are right. It's not working in both cases as you predicted. I guess it's duplicate then. If anyone wonders I have solved it as suggested in mentioned issue, by changing annotations to form:

data class TestDTO(
        @get:NotBlank
        @get:Length(max = 3)
        private val name : String,

        @get:NotBlank
        @get:NIP
        private val nip : String
)

Thanks for help and sorry for taking your time.

All 2 comments

That looks like a duplicate of #13897 again. Your report is mentioning specifically MockMvc but it doesn't really say if that's working when the application is running.

I have no idea how did I check that earlier, but you are right. It's not working in both cases as you predicted. I guess it's duplicate then. If anyone wonders I have solved it as suggested in mentioned issue, by changing annotations to form:

data class TestDTO(
        @get:NotBlank
        @get:Length(max = 3)
        private val name : String,

        @get:NotBlank
        @get:NIP
        private val nip : String
)

Thanks for help and sorry for taking your time.

Was this page helpful?
0 / 5 - 0 ratings