我正在尝试为我的图像上传操作应用单元测试,但是当我尝试运行测试时,imageFileModel的多部分不会在控制器上注入。
这是我的ImageFileModel:
public class ImageFileModel {
private Long id;
private MultipartFile file;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public MultipartFile getFile() {
return file;
}
public void setFile(MultipartFile file) {
this.file = file;
}
}
这是我的ImageController类:
@Controller
public class ImageController {
private final ImageService imageService;
public ImageController(ImageService imageService) {
this.imageService = imageService;
}
@PostMapping("motorcycle/{id}/image")
public String handleImagePost(@PathVariable String id, @ModelAttribute ImageFileModel fileModel, BindingResult bindingResult){
new ImageValidator().validate(fileModel,bindingResult);
if(bindingResult.hasErrors()){
System.out.println("DEBUG INFO ::::::::::::::: inside error condition for this id : " + id);
return "motorcycle/imageuploadform";
}
imageService.saveImageFile(Long.valueOf(id),fileModel.getFile());
return "redirect:/motorcycle/" + id + "/show/";
}
}
这是我的ImageControllerTest类:
public class ImageControllerTest {
@Mock
ImageService imageService;
@Mock
MotorcycleService motorcycleService;
ImageController controller;
MockMvc mockMvc;
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
controller = new ImageController(imageService,motorcycleService);
mockMvc = MockMvcBuilders.standaloneSetup(controller)
.setControllerAdvice(new ControllerExceptionHandler())
.build();
}
@Test
public void handleImagePost() throws Exception {
//GIVEN
MockMultipartFile multipartFile =
new MockMultipartFile("imagefile", "image.jpg", "text/plain",
"image byte data..".getBytes());
//WHEN & THEN
mockMvc.perform(MockMvcRequestBuilders.fileUpload("/motorcycle/1/image")
.file(multipartFile)
.param("id", "2"))
.andExpect(status().is3xxRedirection())
.andExpect(header().string("Location", "/motorcycle/1/show/"));
verify(imageService, times(1)).saveImageFile(anyLong(), any());
}
}
我遇到的错误是:
java.lang.NullPointerException: null
at com.sam.springbootimagecrudexample.validator.ImageValidator.validate(ImageValidator.java:21)
at com.sam.springbootimagecrudexample.controller.ImageController.handleImagePost(ImageController.java:74)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:209)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:871)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:777)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:991)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:925)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:978)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:881)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:661)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:855)
at org.springframework.test.web.servlet.TestDispatcherServlet.service(TestDispatcherServlet.java:66)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
at org.springframework.mock.web.MockFilterChain$ServletFilterProxy.doFilter(MockFilterChain.java:166)
at org.springframework.mock.web.MockFilterChain.doFilter(MockFilterChain.java:133)
at org.springframework.test.web.servlet.MockMvc.perform(MockMvc.java:166)
at com.sam.springbootimagecrudexample.controller.ImageControllerTest.handleImagePost(ImageControllerTest.java:117)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:538)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)
我在调试模式下进行了测试,然后在控制器上传递了ImageFileModel内部的ID值,但文件(多矩形(未通过。我怎么解决这个问题?
您可以这样做,对我来说很好。
字符串路径=; e: text.csv&quort;
FileInputStream fi2 = new FileInputStream(new File(path));
MockMultipartFile secmp = new MockMultipartFile("file", "test.csv", DEFAULT_BINARY.toString(), fi2);
mockMvc.perform(multipart(ENTITY_API_URL).file(secmp).param("id", 12)
).andExpect(status().isCreated());
https://www.baeldung.com/sprint-boot-multipart-requests