我想使用Spring Boot進行集成測試,但發佈請求不起作用。方法saveClientePessoaFisica永遠不會被調用,並且不會返回任何類型的錯誤!我只是嘗試使用get方法進行其他測試,並且它正常工作。春季啓動測試MockMvc執行後 - 不能正常工作
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
@ActiveProfiles("dev")
public class ClienteControllerIT {
@Autowired
private MockMvc mvc;
@Test
public void nao_deve_permitir_salvar_cliente_pf_com_nome_cpf_duplicado() throws Exception {
this.mvc.perform(post("/api/cliente/pessoafisica/post")
.contentType(MediaType.APPLICATION_JSON)
.content("teste")
.andExpect(status().is2xxSuccessful());
}
}
@RestController
@RequestMapping(path = "/api/cliente")
public class ClienteController {
@Autowired
private PessoaFisicaService pessoaFisicaService;
@PostMapping(path = "/pessoafisica/post", consumes = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Void> saveClientePessoaFisica(@RequestBody PessoaFisica pessoaFisica) throws Exception {
this.pessoaFisicaService.save(pessoaFisica);
return new ResponseEntity<Void>(HttpStatus.CREATED);
}
}
非常感謝!這個日誌提示非常有用! –
自動裝配的mvcMock在init方法中被替換。另外,如果您需要使用簡單的用戶和角色,可以使用@WithMockUser(username =「admin1」,roles =「ADMIN」)更好地標註測試函數。 – EliuX
非常感謝您提供這個調試提示!非常有用和實用 – kidnan1991