Closed
Description
@validated with MockMVC is not working.
@RunWith(MockitoJUnitRunner.class)
@Slf4j
public class TestController {
private MockMvc mvc;
@Mock
private TestService service;
@InjectMocks
private Controller controller;
private ObjectMapper mapper = new ObjectMapper();
@Before
public void setup() { // MockMvc standalone approach mvc =
mvc = MockMvcBuilders.standaloneSetup(controller).setValidator(validator()).setControllerAdvice(new InvoiceServiceExceptionHandler())
.build();
}
@Test
public void whenNullValue_thenReturns400() throws JsonProcessingException, Exception {
TestDTO testDTO = new TestDTO();
testDTO.setId(null);
ArrayList<TestDTO> testList = new ArrayList<TestDTO>(Arrays.asList(testDTO));
String jsonTestList = mapper.writeValueAsString(testList);
MvcResult responseMVC = mvc.perform(post("/v1/test").content(jsonTestList).header(HttpHeaders.CONTENT_TYPE,
MediaType.APPLICATION_JSON)).andExpect(status().isBadRequest()).andReturn();
}
Originally posted by @SumithraPrasad in #16917 (comment)