【问题标题】:Spring test + servlet 3.0 + multipart-config tagSpring test + servlet 3.0 + multipart-config 标签
【发布时间】:2013-09-23 12:56:55
【问题描述】:

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

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

【问题讨论】:

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


    【解决方案1】:

    你期待

    @RequestParam MultipartFile pedidosZip
    

    你应该期待什么

    @RequestPart MultipartFile pedidosZip
    

    在你的控制器方法中。

    【讨论】:

      猜你喜欢
      • 2015-01-30
      • 2013-01-15
      • 1970-01-01
      • 2016-10-22
      • 2014-04-19
      • 2014-02-15
      • 2013-11-26
      • 2021-12-09
      • 1970-01-01
      相关资源
      最近更新 更多