Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring Boot Test MockMvc执行post-不工作_Spring_Spring Mvc_Testing_Post_Spring Boot - Fatal编程技术网

Spring Boot Test MockMvc执行post-不工作

Spring Boot Test MockMvc执行post-不工作,spring,spring-mvc,testing,post,spring-boot,Spring,Spring Mvc,Testing,Post,Spring Boot,我试图使用Spring引导进行集成测试,但post请求不起作用。永远不会调用saveClientePessoaFisica方法,也不会返回任何类型的错误!我只是尝试使用get方法进行其他测试,它工作正常 @RunWith(SpringRunner.class) @SpringBootTest @AutoConfigureMockMvc @ActiveProfiles("dev") public class ClienteControllerIT { @Autowired pri

我试图使用Spring引导进行集成测试,但post请求不起作用。永远不会调用saveClientePessoaFisica方法,也不会返回任何类型的错误!我只是尝试使用get方法进行其他测试,它工作正常

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

}
@RunWith(SpringRunner.class)
@春靴测试
@AutoConfigureMockMvc
@动态配置文件(“开发”)
公共类客户控制器{
@自动连线
私有MockMvc;
@试验
public void nao_deve_permitir_salvar_client_pf_com_nome_cpf_duplicado()引发异常{
this.mvc.perform(post(“/api/cliente/pessoafisica/post”)
.contentType(MediaType.APPLICATION_JSON)
.内容(“测试”)
.andExpect(status().is2xxSuccessful());
}
}
@RestController
@请求映射(path=“/api/cliente”)
公共类客户控制器{
@自动连线
私人PessoaFisicaService PessoaFisicaService;
@PostMapping(path=“/pessoafisica/post”,consumes=MediaType.APPLICATION\u JSON\u VALUE)
public ResponseEntity saveClientePessoaFisica(@RequestBody PessoaFisica PessoaFisica)引发异常{
这个.pessoaFisicaService.save(pessoaFisica);
返回新的ResponseEntity(HttpStatus.CREATED);
}
}

一些需要寻找的东西:

  • 在mockmvc中启用日志记录
  • 正确启用mockmvc
  • 使用spring security时,请在mockmvc中初始化它
  • 当使用spring-security/CSRF/httppost时,在mockmvc中传递一个CSRF
  • 启用调试日志记录
像这样:

logging:
  level:
    org.springframework.web: DEBUG
    org.springframework.security: DEBUG
工作测试:

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

    }

}
您的内容“teste”不是有效的JSON。当我使用您的代码时,我收到一个JsonParseException抱怨(顺便说一句,内容(“teste”)后面缺少一个括号)。使用andDo(print())也很有帮助,它将为您提供更详细的请求和响应:

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

非常感谢!此日志提示非常有用!自动连线mvcMock在init方法中被替换。此外,如果您需要使用简单的用户和角色,请使用
@WithMockUser(username=“admin1”,roles=“ADMIN”)
。非常感谢您提供此调试提示!非常有用和实用