SpringBootTest使用Mock测试文件上传

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/IsITMan/article/details/80421748

@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

猜你喜欢

转载自blog.csdn.net/IsITMan/article/details/80421748