I am writing test case for file upload controller. I need to pass value in the URL. I need some help.
@Test
public void testHandleFileUpload() throws Exception {
//String fileType = "Seller";
InputStream uploadStream = PALFileUploadController.class.getClassLoader().getResourceAsStream("");
MockMultipartFile file = new MockMultipartFile("file", uploadStream);
assert uploadStream != null;
this.mockMvc.perform(( fileUpload("/uploadFile/{fileType}"))
.file(file))
.andExpect(status().isOk());
}
The error message I see when running this code is
java.lang.IllegalArgumentException: Not enough variable values available to expand 'fileType'
at org.springframework.web.util.UriComponents$VarArgsTemplateVariables.getValue(UriComponents.java:327)
at org.springframework.web.util.UriComponents.expandUriComponent(UriComponents.java:230)
at org.springframework.web.util.HierarchicalUriComponents$FullPathComponent.expand(HierarchicalUriComponents.java:685)
at org.springframework.web.util.HierarchicalUriComponents.expandInternal(HierarchicalUriComponents.java:328)
at org.springframework.web.util.HierarchicalUriComponents.expandInternal(HierarchicalUriComponents.java:47)
at org.springframework.web.util.UriComponents.expand(UriComponents.java:163)
at org.springframework.web.util.UriComponentsBuilder.buildAndExpand(UriComponentsBuilder.java:412)
at org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder.<init>(MockHttpServletRequestBuilder.java:122)
at org.springframework.test.web.servlet.request.MockMultipartHttpServletRequestBuilder.<init>(MockMultipartHttpServletRequestBuilder.java:52)
at org.springframework.test.web.servlet.request.MockMvcRequestBuilders.fileUpload(MockMvcRequestBuilders.java:201)
at com.ibm.finance.palink.web.test.PALFileUploadControllerTest.testHandleFileUpload(PALFileUploadControllerTest.java:53)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
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:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
But I know I need to pass value as "Seller" or "Approver" for fileType.
Please tell me How to pass value to fileType variable. Thanks in advance.
Without seeing your controller code, it is difficult to answer, but I'm assuming that your controller looks something like:
@RequestMapping(value = "/uploadFile/{fileType}", method = RequestMethod.POST)
public void fileUpload(@RequestParam MultipartFile file, @PathVariable String fileType) {
// ...
}
Your test should be something like:
mockMvc.perform(MockMvcRequestBuilders.fileUpload("/uploadFile")
.file(file)
.param("fileType", "Seller")
.andExpect(status().isOk());