Spring MockMvc - Custom Validators are not registered/called in Spring Container

led
@Configuration
@EnableWebMvc
@ComponentScan("com.xyz.web")
class BaseTestConfig extends WebMvcConfigurerAdapter {

    @Bean
    AccountService accountService() {
        return Mockito.mock(AccountService)
    }

}

class Test {
    @Autowired WebApplicationContext wac

    MockMvc mockMvc

    @BeforeClass
    void setup() {
        mockMvc = MockMvcBuilders.webAppContextSetup(wac).build()
    }

    @Test
    void testSubmitPageFails() {
        mockMvc.perform(post("/createAccount")
                .param("account.username", "x")
                .param("confirmEmail", "y")
                .param("account.firstName", "")
                .param("account.lastName", "")
                .param("account.zip", ""))
                .andExpect(model().errorCount(5))
                .andExpect(view().name("account/createAccount"))
                .andExpect(status().isOk())
    }

    @Test
    void testSubmitPageSucceeds() {
        mockMvc.perform(post("/createAccount")
                .param("account.username", "[email protected]")
                .param("confirmEmail", "[email protected]")
                .param("account.firstName", "John")
                .param("account.lastName", "Doe")
                .param("account.zip", "22102"))
                .andExpect(flash().attributeCount(1))
                .andExpect(status().isMovedTemporarily())
                .andExpect(redirectedUrl("/home"))
    }
}

I define my test class and config class as shown above. However, controller methods having @Valid annotations not being invoked. It seems like they are not registered during Spring container initialization.

These validators registered just fine when I run the application.

Any idea why my custom validators are ignored for my controller tests?

led

I enabled MockMvcResultHandlers.print() to see what the issue is. It turned out that a repository on which my validator depends was not defined. Defining that in BaseTestConfig resolved the issue.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Spring MockMVC - How to mock custom validators running outside of controllers

From Dev

Spring MockMVC - How to mock custom validators running outside of controllers

From Dev

Spring MVC custom validators

From Dev

Tests fail with custom jsr validators with @Inject for Spring boot application

From Dev

Spring Security Unit Test - MockMvc perform test with custom user

From Dev

Spring MockMVC, Spring security and Mockito

From Dev

Spring MockMvc redirectedUrl with pattern

From Dev

How to conditionally invoke spring validators

From Java

Spring Boot Aspectj test with MockMvc

From Dev

Testing Spring MVC Router with MockMVC

From Dev

Inject mock into Spring MockMvc WebApplicationContext

From Dev

Mockmvc put method is not working spring

From Dev

MockMvc and Spring Security - Null FilterChainProxy

From Dev

Spring JUnit testing using mockMvc

From Dev

Spring Boot Aspectj test with MockMvc

From Dev

Spring JUnit testing using mockMvc

From Java

Testing Spring's @RequestBody using Spring MockMVC

From Dev

Spring Security Test and MockMvc supply null custom UserDetails parameter to REST Controller

From Dev

Configuring Spring MockMvc to use custom argument resolver before built-in ones

From Java

Spring Boot MVC Test - MockMvc is always null

From Dev

Spring Test MockMvc perform request on external URL

From Dev

Testing Spring 4.0.3 Controller (MVC) with MockMVC

From Dev

Upload file using Spring mvc and MockMVC

From Dev

Empty response headers from Spring MockMvc with springSecurityFilterChain

From Dev

Which Spring Maven dependency provides class MockMvc?

From Dev

Spring Boot - MockMVC Error creating bean

From Dev

Using Spring mockMvc to test optional path variables

From Dev

Testing Spring 4.0.3 Controller (MVC) with MockMVC

From Dev

Spring mockMvc throws error when using ExceptionHandler

Related Related

  1. 1

    Spring MockMVC - How to mock custom validators running outside of controllers

  2. 2

    Spring MockMVC - How to mock custom validators running outside of controllers

  3. 3

    Spring MVC custom validators

  4. 4

    Tests fail with custom jsr validators with @Inject for Spring boot application

  5. 5

    Spring Security Unit Test - MockMvc perform test with custom user

  6. 6

    Spring MockMVC, Spring security and Mockito

  7. 7

    Spring MockMvc redirectedUrl with pattern

  8. 8

    How to conditionally invoke spring validators

  9. 9

    Spring Boot Aspectj test with MockMvc

  10. 10

    Testing Spring MVC Router with MockMVC

  11. 11

    Inject mock into Spring MockMvc WebApplicationContext

  12. 12

    Mockmvc put method is not working spring

  13. 13

    MockMvc and Spring Security - Null FilterChainProxy

  14. 14

    Spring JUnit testing using mockMvc

  15. 15

    Spring Boot Aspectj test with MockMvc

  16. 16

    Spring JUnit testing using mockMvc

  17. 17

    Testing Spring's @RequestBody using Spring MockMVC

  18. 18

    Spring Security Test and MockMvc supply null custom UserDetails parameter to REST Controller

  19. 19

    Configuring Spring MockMvc to use custom argument resolver before built-in ones

  20. 20

    Spring Boot MVC Test - MockMvc is always null

  21. 21

    Spring Test MockMvc perform request on external URL

  22. 22

    Testing Spring 4.0.3 Controller (MVC) with MockMVC

  23. 23

    Upload file using Spring mvc and MockMVC

  24. 24

    Empty response headers from Spring MockMvc with springSecurityFilterChain

  25. 25

    Which Spring Maven dependency provides class MockMvc?

  26. 26

    Spring Boot - MockMVC Error creating bean

  27. 27

    Using Spring mockMvc to test optional path variables

  28. 28

    Testing Spring 4.0.3 Controller (MVC) with MockMVC

  29. 29

    Spring mockMvc throws error when using ExceptionHandler

HotTag

Archive