estou tentando testar meu controller Aluno mas não com exito, a principio ele da forbiiden pelo spring security, não sei como seria para mockar ele, ao tirar o metodo do spring secutiry deixando publico me retorna com outro erro
@AutoConfigureMockMvc @SpringBootTest @ActiveProfiles("test") @ExtendWith(MockitoExtension.class) class AlunoControllerTest {
@Autowired
private MockMvc mockMvc;
@Mock
AlunoForm alunoForm;
@Mock
private UriComponentsBuilder uriComponentsBuilder;
@Mock
private ResponseEntity<AlunoDto> response;
@Mock
private AlunoController alunoController; @Mock private Aluno aluno; @Mock private Responsavel responsavel;
@MockBean
private AlunoRepository alunoRepository;
@MockBean
private ResponsavelRepository responsavelRepository;
@Test
void DeveriacadastrarAluno() throws Exception{
URI uri = new URI("/responsavel/aluno");
String json = "{\"nome\":\"invalido\",\"cpf\":\"123456123\"" +
",\"cnh\":\"31432423424\",\"cpf\":\"1245453456\"" +
",\"telefone\":\"262565655614\",\"dataDeNascimento\":\"2222/05/2000\"}";
responsavel.setId(2L);
aluno.setNome("saci");
aluno.setTelefone("13123123213");
aluno.setCpf("123123123123123");
aluno.setDataNascimento(LocalDate.now());
aluno.setId(2L);
when(alunoRepository.save(aluno)).thenReturn(aluno);
when(alunoController.cadastrarAluno(responsavel.getId(),alunoForm, uriComponentsBuilder)).thenReturn(response);
assertNotNull(aluno);
mockMvc.perform(MockMvcRequestBuilders.post(uri)
.content(json)
.contentType(MediaType.APPLICATION_JSON)).andDo(print()) //simula Postman
.andExpect(MockMvcResultMatchers.status().isCreated());
}
Erro :
MockHttpServletResponse:
Status = 405
Error message = Request method 'POST' not supported
Headers = [Allow:"DELETE, GET, PUT", X-Content-Type-Options:"nosniff", X-XSS-Protection:"1; mode=block", Cache-Control:"no-cache, no-store, max-age=0, must-revalidate", Pragma:"no-cache", Expires:"0", X-Frame-Options:"DENY"]
Content type = null
Body =
Forwarded URL = null
Redirected URL = null Cookies = []
MockHttpServletRequest: HTTP Method = POST Request URI = /responsavel/aluno Parameters = {} Headers = [Content-Type:"application/json;charset=UTF-8", Content-Length:"136"] Body = {"nome":"invalido","cpf":"123456123","cnh":"31432423424","cpf":"1245453456","telefone":"262565655614","dataDeNascimento":"2222/05/2000"} Session Attrs = {}
Handler: Type = null
Async: Async started = false Async result = null
Resolved Exception: Type = org.springframework.web.HttpRequestMethodNotSupportedException
ModelAndView: View name = null View = null Model = null
FlashMap: Attributes = null
MockHttpServletResponse: Status = 405 Error message = Request method 'POST' not supported Headers = [Allow:"DELETE, GET, PUT", X-Content-Type-Options:"nosniff", X-XSS-Protection:"1; mode=block", Cache-Control:"no-cache, no-store, max-age=0, must-revalidate", Pragma:"no-cache", Expires:"0", X-Frame-Options:"DENY"] Content type = null Body = Forwarded URL = null Redirected URL = null Cookies = []
java.lang.AssertionError: Status expected:<201> but was:<405> Expected :201 Actual :405