SpringBootTest使用Mock测试文件上传

匿名 (未验证) 提交于 2019-12-03 00:18:01

@SpringBootTest @RunWith(SpringJUnit4ClassRunner.class) public class OssControllerTest {     @Autowired     private WebApplicationContext wac;      private MockMvc mvc;      @Before     public void setupMockMvc(){         mvc = MockMvcBuilders.webAppContextSetup(wac).build(); //初始化MockMvc对象     }      @Test     public void uploadFilePublic() throws Exception {         File file = new File("C:\\Users\\Administrator\\Desktop\\xxxx.jpg");         //文件之外的参数         String key = OssUtils.createFileKey(file.getName(), null);         MockMultipartFile firstFile = new MockMultipartFile("file", "xxxx.jpg",                 MediaType.TEXT_PLAIN_VALUE, new FileInputStream(file));          mvc.perform(MockMvcRequestBuilders.fileUpload("/ossService/uploadFilePublic")                 .file(firstFile)//文件                 .param("key", key))//参数                 .andExpect(MockMvcResultMatchers.status().isOk());         LogUtils.info("文件的key为:"+key);     }   }

参考博客:http://tobato.iteye.com/blog/2315174

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!