2017-04-18 3 views
0

Ich versuche, einen Integrationstest mit dem Spring-Boot machen, aber die Post-Anfrage funktioniert nicht. Die Methode saveClientePessoaFisica wird nie aufgerufen und gibt keine Art von Fehler zurück! Ich habe gerade versucht, andere Tests mit einer Get-Methode zu machen, und es funktioniert einwandfrei.Spring Boot Test MockMvc führen Post - nicht funktioniert

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

} 

Antwort

1

Ihr Inhalt "teste" ist keine gültige JSON. Wenn ich Ihren Code verwende, bekomme ich eine JsonParseException, die sich darüber beschwert (Übrigens fehlt nach dem Inhalt eine Klammer ("teste")). Auch hilfreich ist die Verwendung von undDo (print()), die Ihnen die Anfrage und Antwort im Detail geben wird:

@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")) 
      .andDo(print()) 
      .andExpect(status().is2xxSuccessful()); 
} 
1

Einige Dinge suchen:

  • Protokollierung aktivieren in Ihrem mockmvc
  • Aktivieren Sie Ihre mockmvc richtig
  • Wenn der Frühling Sicherheit verwenden, initialisieren es in mockmvc
  • Wenn der Frühling Sicherheit mit/CSRF/HTTP POST, übergeben Sie ein csrf in Ihrem mypmvc
  • Aktivieren Sie Debugprotokollierung

So:

logging: 
    level: 
    org.springframework.web: DEBUG 
    org.springframework.security: DEBUG 

Arbeitstest:

@RunWith(SpringRunner.class) 
@SpringBootTest 
@ActiveProfiles("test") 
@AutoConfigureMockMvc 
public class MockMvcTest { 

    @Autowired 
    protected ObjectMapper objectMapper; 

    @Autowired 
    private MockMvc mockMvc; 

    @Autowired 
    private WebApplicationContext webApplicationContext; 

    @Before 
    public void init() throws Exception { 
     mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); 

    } 

    @Test 
    public void adminCanCreateOrganization() throws Exception { 

     this.mockMvc.perform(post("/organizations") 
       .with(user("admin1").roles("ADMIN")) 
       .with(csrf()) 
       .contentType(APPLICATION_JSON) 
       .content(organizationPayload("org1")) 
       .accept(APPLICATION_JSON)) 
       .andDo(print()) 
       .andExpect(status().isCreated()); 

    } 

} 
+0

Vielen Dank! Dieser Log-Tipp war sehr nützlich! –

+0

Der autoverzierte mvcMock wird in der init-Methode ersetzt. Wenn Sie einen einfachen Benutzer und eine Rolle benötigen, können Sie die Testfunktion mit '@WithMockUser (username =" admin1 ", roles =" ADMIN ") annotieren.'. – EliuX

+0

Vielen Dank für diesen Debug-Tipp! Sehr nützlich und praktisch – kidnan1991

Verwandte Themen