编写Junit测试用例文件上传控制器

时间:2018-05-04 07:24:28

标签: file unit-testing junit junit4

我正在编写文件上传控制器的测试用例。我需要在URL中传递值。我需要一些帮助。

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

运行此代码时看到的错误消息是

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)

但我知道我需要为fileType传递值为“Seller”或“Approver”。

请告诉我如何将值传递给fileType变量。 提前谢谢。

1 个答案:

答案 0 :(得分:0)

没有看到您的控制器代码,很难回答,但我假设您的控制器看起来像:

mockMvc.perform(MockMvcRequestBuilders.fileUpload("/uploadFile")
    .file(file)
    .param("fileType", "Seller")
    .andExpect(status().isOk());

您的测试应该是这样的:

{{1}}