我有这个上传文件的入口点,它工作正常:
@RequestMapping(value = "/importarPedidosRepresentante", method = RequestMethod.POST)
@ResponseBody
public List<ImportacaoPVEResultado> importarPedidoRepresentanteZip(@RequestParam MultipartFile pedidosZip) throws JsonParseException, JsonMappingException, IOException {
....
}
现在,我正在尝试使用 Spring-Test 框架创建一个 JUnit 测试:
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(classes = MvcConfiguration.class, loader = AnnotationConfigWebContextLoader.class)
public class TestRepresentantesServices {
private MockMvc mvc;
....
@Test
public void testZip() throws Exception {
MockMultipartFile file = new MockMultipartFile("pedidosZip", "pedidos.zip", MediaType.APPLICATION_OCTET_STREAM_VALUE, jsonZip);
MockHttpServletRequestBuilder request = MockMvcRequestBuilders.fileUpload("/representantes/importarPedidosRepresentante").file(file).contentType(MediaType.MULTIPART_FORM_DATA);
ResultActions resp = mvc.perform(request); /// <-- java.lang.AssertionError: Status expected:<200> but was:<400>
}
}
我收到此断言错误:
java.lang.AssertionError: Status expected:<200> but was:<400>
at org.springframework.test.util.AssertionErrors.fail(AssertionErrors.java:60)
at org.springframework.test.util.AssertionErrors.assertEquals(AssertionErrors.java:89)
at org.springframework.test.web.servlet.result.StatusResultMatchers$5.match(StatusResultMatchers.java:549)
at org.springframework.test.web.servlet.MockMvc.applyDefaultResultActions(MockMvc.java:159)
at org.springframework.test.web.servlet.MockMvc.perform(MockMvc.java:136)
at br.com.jjw.jjwxp.web.test.representantes.controllers.TestRepresentantesServices.testZip(TestRepresentantesServices.java:425)
我做错了什么?
HTTP 400 means: Bad Request
The request could not be understood by the server due to malformed syntax. The client SHOULD NOT repeat the request without modifications.