Ao rodar a classe de teste abaixo, a asserção falha.
@SpringBootTest
@AutoConfigureMockMvc
@AutoConfigureJsonTesters
class MedicoControllerTest {
@Autowired
private MockMvc mvc;
@Autowired
private JacksonTester<DadosCadastroMedico> dadosCadastroMedicoJson;
@Autowired
private JacksonTester<DadosDetalhamentoMedico> dadosDetalhamentoMedicoJson;
@MockBean
private MedicoRepository repository;
@Test
@DisplayName("Deveria devolver codigo http 200 quando informacoes estao validas")
@WithMockUser
void cadastraCenario2() throws Exception {
DadosCadastroMedico dadosCadastro = new DadosCadastroMedico("Medico", "medico@voll.med", "61999999999",
"123456", Especialidade.CARDIOLOGIA, dadosEndereco());
when(repository.save(any())).thenReturn(new Medico(dadosCadastro));
MockHttpServletResponse response = mvc.perform(post("/medicos/cadastro").contentType(MediaType.APPLICATION_JSON)
.content(dadosCadastroMedicoJson.write(dadosCadastro).getJson())).andReturn().getResponse();
DadosDetalhamentoMedico dadosDetalhamento = new DadosDetalhamentoMedico(null, dadosCadastro.nome(),
dadosCadastro.email(), dadosCadastro.crm(), dadosCadastro.telefone(), dadosCadastro.especialidade(),
new Endereco(dadosCadastro.endereco()));
String jsonEsperado = dadosDetalhamentoMedicoJson.write(dadosDetalhamento).getJson();
assertThat(response.getStatus()).isEqualTo(HttpStatus.CREATED.value());
assertThat(response.getContentAsString()).isEqualTo(jsonEsperado);
}
private DadosEndereco dadosEndereco() {
return new DadosEndereco("rua xpto", "bairro", "00000000", "Brasilia", "DF", null, null);
}
}
MockHttpServletRequest:
HTTP Method = POST
Request URI = /medicos/cadastro
Parameters = {}
Headers = [Content-Type:"application/json;charset=UTF-8", Content-Length:"248"]
Body = {"nome":"Medico","email":"medico@voll.med","telefone":"61999999999","crm":"123456","especialidade":"CARDIOLOGIA","endereco":{"logradouro":"rua xpto","bairro":"bairro","cep":"00000000","cidade":"Brasilia","uf":"DF","complemento":null,"numero":null}}
Session Attrs = {}
Handler:
Type = med.voll.api.controller.MedicoController
Method = med.voll.api.controller.MedicoController#cadastra(DadosCadastroMedico, UriComponentsBuilder)
Async:
Async started = false
Async result = null
Resolved Exception:
Type = org.springframework.web.bind.MethodArgumentNotValidException
ModelAndView:
View name = null
View = null
Model = null
FlashMap:
Attributes = null
MockHttpServletResponse:
Status = 400
Error message = null
Headers = [Content-Type:"application/json", X-Content-Type-Options:"nosniff", X-XSS-Protection:"0", Cache-Control:"no-cache, no-store, max-age=0, must-revalidate", Pragma:"no-cache", Expires:"0", X-Frame-Options:"DENY"]
Content type = application/json
Body = [{"campo":"endereco.cep","mensagem":"must match \"\\d{5}-\\d{3}\""}]
Forwarded URL = null
Redirected URL = null
Cookies = []
2023-09-18T13:32:00.022-03:00 INFO 8612 --- [ionShutdownHook] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2023-09-18T13:32:00.037-03:00 INFO 8612 --- [ionShutdownHook] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown initiated...
2023-09-18T13:32:00.053-03:00 INFO 8612 --- [ionShutdownHook] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown completed.