Spring test + servlet 3.0 + multipart-config标签

时间:2013-09-23 12:56:56

标签: spring-mvc upload servlet-3.0 spring-test spring-test-mvc

我有这个上传文件的入口点,它工作正常:

@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.

1 个答案:

答案 0 :(得分:0)

你期待

@RequestParam MultipartFile pedidosZip

但是你应该期待

@RequestPart MultipartFile pedidosZip

在控制器方法中。