Spring Boot Test MockMvc 执行后 - 不工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43474457/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring Boot Test MockMvc perform post - Not working
提问by Pablo Souza
I'm trying to make a Integration test using the Spring Boot but the post request is not working. The method saveClientePessoaFisicais never called and do not return any kind of error! I just tried to make other tests using a get method and it works properly.
我正在尝试使用 Spring Boot 进行集成测试,但发布请求不起作用。该方法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);
}
}
采纳答案by Ralf Stuckert
Your content "teste" is no valid JSON. When I am using your code, I'm getting a JsonParseException complaining about that (By the way, there is a parenthese missing after content("teste") ). Also helpful is using andDo(print()) which will give you the request and response in more detail:
您的内容“teste”不是有效的 JSON。当我使用你的代码时,我收到一个 JsonParseException 抱怨(顺便说一下,在 content("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());
}
回答by ddewaele
Some things to look for :
要寻找的一些东西:
- Enable logging in your mockmvc
- Enable your mockmvc properly
- When using spring security, initialise it in mockmvc
- When using spring security / CSRF / HTTP POST , pass a csrf in your mockmvc
- Enable debug logging
- 在您的 mockmvc 中启用日志记录
- 正确启用您的 mockmvc
- 使用spring security时,在mockmvc中初始化
- 使用 spring security / CSRF / HTTP POST 时,在您的 mockmvc 中传递 csrf
- 启用调试日志
Like this :
像这样 :
logging:
level:
org.springframework.web: DEBUG
org.springframework.security: DEBUG
Working test :
工作测试:
@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());
}
}

