2013-09-23 65 views
1

我有這樣的入口點到uploadfiles,並能正常工作:Spring測試+ servlet的3.0 +多部分的配置標籤

@RequestMapping(value = "/importarPedidosRepresentante", method = RequestMethod.POST) 
@ResponseBody 
public List<ImportacaoPVEResultado> importarPedidoRepresentanteZip(@RequestParam MultipartFile pedidosZip) throws JsonParseException, JsonMappingException, IOException { 
    .... 
} 

現在,我想使用Spring的測試框架來創建一個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. 

回答

0

你期待

@RequestParam MultipartFile pedidosZip 

但是你應該期望

@RequestPart MultipartFile pedidosZip 
在你的控制器方法