Skip to content

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

New issue

Have a question about this project? No Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “No Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? No Sign in to your account

Closed
SumithraPrasad opened this issue Dec 30, 2019 · 1 comment
Closed

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

SumithraPrasad opened this issue Dec 30, 2019 · 1 comment
Labels
status: duplicate A duplicate of another issue

Comments

@SumithraPrasad
Copy link

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

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Dec 30, 2019
@snicoll
Copy link
Member

snicoll commented Dec 30, 2019

@SumithraPrasad there is no need to create a duplicate. #16917 is still open.

@snicoll snicoll closed this as completed Dec 30, 2019
@snicoll snicoll added status: duplicate A duplicate of another issue and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Dec 30, 2019
No Sign up for free to join this conversation on GitHub. Already have an account? No Sign in to comment
Labels
status: duplicate A duplicate of another issue
Projects
None yet
Development

No branches or pull requests

3 participants