Mock MVC javax.validation

I would like to poke fun at my check in order to check.

The size check fails in my test. The maximum ID size is 40 characters and I am waiting for an exception when I test it with 42 long IDs.

MyObject.java

@NotNull
@Size(min = 1, max = 40)
private String id;

      

MyTest.java:

@Autowired
private Validator validator; // javax.validation.Validator

@Before
public void setUp() {
    mockMvc = MockMvcBuilders.standaloneSetup(MyController).build();
}

@Test
public void Test() throws Exception {
    myObject.setId(StringUtils.repeat("k", 42);
    mockMvc.perform(post(MY_URL)
                .contentType(MediaType.APPLICATION_JSON)
                .content(mapper.writeValueAsString(myObject)))
                .andExpect(status().isBadRequest())
                .andExpect(content().contentType(APPLICATION_JSON_WITH_UTF8));
}

      

I've already mocked every service needed. They are found in XML files like this:

<bean id="mockValidator" class="org.mockito.Mockito" factory-method="mock">
        <constructor-arg value="javax.validation.Validator"/>
</bean>

      

But I got this message:

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NumberFormatException: For input string: "kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk"

      

From the UI everything works fine.

+3


source to share





All Articles