Skip to content

@Validated with MockMVC is not working. Postman request works #24274

Closed
@SumithraPrasad

Description

@SumithraPrasad

@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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions