繁体   English   中英

Spring Boot分段文件上传集成测试-空文件

[英]Spring boot multipart file upload integration test - null file

我无法编写将文件上传到控制器的测试。

在所有出现此问题的事件中,我都没有发现对我有用的方法。

我可以从Web应用程序上载和存储文件,但是运行测试时,控制器中的文件始终为空

应用

import com.mangofactory.swagger.configuration.SpringSwaggerConfig;
import com.mangofactory.swagger.plugin.EnableSwagger;
import com.mangofactory.swagger.plugin.SwaggerSpringMvcPlugin;
import com.wordnik.swagger.model.ApiInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
@EnableSwagger
public class Application {

private SpringSwaggerConfig springSwaggerConfig;

@Autowired
public void setSpringSwaggerConfig(SpringSwaggerConfig springSwaggerConfig) {
    this.springSwaggerConfig = springSwaggerConfig;
}

@Bean
        // Don't forget the @Bean annotation
public SwaggerSpringMvcPlugin customImplementation() {
    return new SwaggerSpringMvcPlugin(this.springSwaggerConfig).apiInfo(
            apiInfo()).includePatterns("/api/.*");
}

private ApiInfo apiInfo() {
    return new ApiInfo("Application", "Upload files", "Play nice", "reece@reececo.com", "Go for your life", "DoesNotExist");
}

public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

调节器

@RequestMapping(method = POST)
@ApiOperation(value = "Multipart file upload", notes = "Upload a file to an ID")
@ApiResponses(value = { @ApiResponse(code = 200, message = "") })
public @ResponseBody String uploadFile(MultipartFile file, @PathVariable String key) {

// Store File

}

测试

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = EasyshareApplication.class)
@WebAppConfiguration
@IntegrationTest
public class FileUploadControllerTest {

@Autowired
private UploadRepository uploadRepository;

private MockMvc restFileMockMvc;

@Before
public void setup() throws Exception {
    FileController fileController= new FileController();
    ReflectionTestUtils.setField(fileController, "uploadRepository", uploadRepository);
    this.restFileMockMvc = MockMvcBuilders.standaloneSetup(fileController).build();
}

@Test
public void testFileUpload() throws Exception{
    MockMultipartFile mockFile = new MockMultipartFile("data", "DATADATADATDATADATA".getBytes());

    Upload upload = uploadRepository.save(new Upload("Description"));
    String key = upload.getKey();

    restFileMockMvc
            .perform(fileUpload("/api/uploads/" + key + "/file")
                .file(mockFile)
                    .param("name", "xyz")
                    .contentType(MediaType.MULTIPART_FORM_DATA))
            .andExpect(status().isCreated())
            .andExpect(jsonPath("$.upload").exists())
            .andExpect(jsonPath("$.ID").exists())
            .andExpect(jsonPath("$.filename").exists())
            .andExpect(jsonPath("$.contentType").exists())
            .andExpect(jsonPath("$.length").exists());
}

@Test
public void testMissingFileUpload() throws Exception{
    String key = "shouldntNeedAKey";

    restFileMockMvc
            .perform(fileUpload("/api/uploads/" + key + "/file"))
            .andExpect(status().isBadRequest());
}
}

万一有人偶然发现此问题,我确实解决了此问题。

我只需MulipartFile在REST控制器中注释MulipartFile

@RequestParam(name = "file") ,@ @RequestPart也可以。

控制器现在看起来像这样:

@RequestMapping(method = POST)
@ApiOperation(value = "Multipart file upload", notes = "Upload a file to an ID")
@ApiResponses(value = { @ApiResponse(code = 200, message = "") })
public @ResponseBody String uploadFile(@RequestParam(name = "file") MultipartFile file, @PathVariable String key) {

// Store File

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM