Весенний тест + сервлет 3.0 + тег multipart-config
У меня есть эта точка входа для загрузки файлов, и она отлично работает:
@RequestMapping(value = "/importarPedidosRepresentante", method = RequestMethod.POST)
@ResponseBody
public List<ImportacaoPVEResultado> importarPedidoRepresentanteZip(@RequestParam MultipartFile pedidosZip) throws JsonParseException, JsonMappingException, IOException {
....
}
Теперь я пытаюсь создать тест JUnit с использованием среды Spring-Test:
@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.
1 ответ
Вы ожидаете
@RequestParam MultipartFile pedidosZip
Однако вы должны ожидать
@RequestPart MultipartFile pedidosZip
в вашем методе контроллера.