Java Spring引导多部分文件上载集成测试-空文件
我无法编写将文件上载到控制器的测试 在这一问题发生的所有事件中,我没有看到任何事情对我有效 我可以从我的webapp上传和存储文件,但是当我运行测试时,控制器中的文件总是空的 应用程序Java Spring引导多部分文件上载集成测试-空文件,java,spring,spring-boot,Java,Spring,Spring Boot,我无法编写将文件上载到控制器的测试 在这一问题发生的所有事件中,我没有看到任何事情对我有效 我可以从我的webapp上传和存储文件,但是当我运行测试时,控制器中的文件总是空的 应用程序 import com.mangofactory.swagger.configuration.SpringSwaggerConfig; import com.mangofactory.swagger.plugin.EnableSwagger; import com.mangofactory.swagger.plug
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());
}
}
如果有人偶然发现了这个问题,我确实解决了这个问题 我只需在我的REST控制器中为
mulipart文件
添加注释:
@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
}
如果有人偶然发现了这个问题,我确实解决了这个问题 我只需在我的REST控制器中为
mulipart文件
添加注释:
@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
}
如果有人偶然发现了这个问题,我确实解决了这个问题 我只需在我的REST控制器中为
mulipart文件
添加注释:
@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
}
如果有人偶然发现了这个问题,我确实解决了这个问题 我只需在我的REST控制器中为
mulipart文件
添加注释:
@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
}
您是否在mvc配置中配置了
CommonsMultipartResolver
;您是否在mvc配置中配置了CommonsMultipartResolver
;您是否在mvc配置中配置了CommonsMultipartResolver
;您是否在mvc配置中配置了CommonsMultipartResolver
;