gpt4 book ai didi

java - Spring Boot多部分文件上传集成测试-空文件

转载 作者:行者123 更新时间:2023-12-01 22:07:08 24 4
gpt4 key购买 nike

我无法编写将文件上传到我的 Controller 的测试。

在发生的所有此问题中,我没有看到任何解决方案。

我能够从我的网络应用程序上传和存储文件,但是当我运行测试时, Controller 中的文件始终为空

申请

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);
}
}

Controller

@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());
}
}

最佳答案

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

我只需在 REST Controller 中注释 MulipartFile 即可:

@RequestParam(name = "file")@RequestPart 也有效。

Controller 现在看起来像这样:

@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

}

关于java - Spring Boot多部分文件上传集成测试-空文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32498786/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com