I am trying to apply unit test for my image upload operation but when i try to run test MultipartFile doesnt inject MultipartFile of ImageFileModel on the controller.
this is my 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;
}
}
This is My ImageController class :
@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/";
}
}
This is my ImageControllerTest class :
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());
}
}
the error i am getting is :
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)
I ran my test on debug mode then I saw id value inside ImageFileModel passed on the controller but the file(MultiPartFile) didnt pass. How can I solve this problem?
Aucun commentaire:
Enregistrer un commentaire