Spring MVC 4.2:如何使用@RequestPart参数对控制器进行单元测试

时间:2019-02-20 11:11:40

标签: unit-testing spring-mvc testing mockmvc

我有一个requestMapping格式:

@RequestMapping(
    value = "/submitCase",
    consumes =  MediaType.MULTIPART_FORM_DATA_VALUE,
    produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
    method = RequestMethod.POST
)
public Object submitCase(
    @RequestPart(name = "attachment[0]", required = false) MultipartFile attachment1,
    @RequestPart(name = "attachment[1]", required = false) MultipartFile attachment2,
    @RequestPart(name = "attachment[2]", required = false) MultipartFile attachment3,
    @RequestPart(name = "attachment[3]", required = false) MultipartFile attachment4,
    @RequestPart(name = "attachment[4]", required = false) MultipartFile attachment5,
    @RequestPart(name = "caseDetails") CaseDetails caseDetails) {}

现在我想用MockMvcBuilders为此编写一个测试。但是我无法这样做。

这里的挑战是请求处理程序使用multipart / form-data,它由4个Multipart文件和1个Json数据组成。

关于如何解决此问题的任何想法?请记住,我必须使用Spring 4.3。

如果您需要更多信息,请告诉我。

1 个答案:

答案 0 :(得分:0)

在这里看看一个很好的例子:https://stackoverflow.com/a/21805186/3976662

请注意,示例中使用的MockMvcRequestBuilders.html#multipart在Spring 4.3.0中尚不可用。请改用MockMvcRequestBuilders.html#fileUpload(在Spring 5中弃用)。

CaseDetails.java:

public class CaseDetails {
    private String exampleAttr;

    public String getExampleAttr() {
        return exampleAttr;
    }

    public void setExampleAttr(String exampleAttr) {
        this.exampleAttr = exampleAttr;
    }
}

UploadController.java:

@Controller
public class UploadController {

    @RequestMapping(
            value = "/submitCase",
            consumes =  MediaType.MULTIPART_FORM_DATA_VALUE,
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
            method = RequestMethod.POST
    )
    @ResponseBody
    public Object submitCase(
            @RequestPart(name = "attachment[0]", required = false) MultipartFile attachment1,
            @RequestPart(name = "attachment[1]", required = false) MultipartFile attachment2,
            @RequestPart(name = "attachment[2]", required = false) MultipartFile attachment3,
            @RequestPart(name = "attachment[3]", required = false) MultipartFile attachment4,
            @RequestPart(name = "attachment[4]", required = false) MultipartFile attachment5,
            @RequestPart(name = "caseDetails") CaseDetails caseDetails) {

        Map<String,String> result = new HashMap<>();
        result.put("success", "true");

        return result;
    }
}

UploadControllerTest.java:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = UploadControllerTest.TestConfig.class)
public class UploadControllerTest {

    @Autowired
    private UploadController uploadController;

    @Test
    public void testSubmitCase() throws Exception {
        MockMultipartFile file1 = new MockMultipartFile("attachment[0]", "filename-1.txt", "text/plain", "1".getBytes());
        MockMultipartFile file2 = new MockMultipartFile("attachment[1]", "filename-2.txt", "text/plain", "2".getBytes());
        MockMultipartFile file3 = new MockMultipartFile("attachment[2]", "filename-3.txt", "text/plain", "3".getBytes());
        MockMultipartFile file4 = new MockMultipartFile("attachment[3]", "filename-4.txt", "text/plain", "4".getBytes());
        MockMultipartFile file5 = new MockMultipartFile("attachment[4]", "filename-5.txt", "text/plain", "5".getBytes());

        MockMultipartFile caseDetailsJson = new MockMultipartFile("caseDetails", "", "application/json","{\"exampleAttr\": \"someValue\"}".getBytes());

        MockMvc mockMvc = MockMvcBuilders.standaloneSetup(uploadController).build();
        mockMvc.perform(MockMvcRequestBuilders.fileUpload("/submitCase")
                .file(file1)
                .file(file2)
                .file(file3)
                .file(file4)
                .file(file5)
                .file(caseDetailsJson))
                    .andDo(MockMvcResultHandlers.print())
                    .andExpect(MockMvcResultMatchers.content().string("{\"success\":\"true\"}"))
                    .andReturn();
    }

    @Configuration
    static class TestConfig {

        @Bean
        public UploadController uploadController() {
            return new UploadController();
        }
    }
}

请注意,在UploadControllerTest中,JSON数据必须包装在MockMultipartFile中-等同于上载的文件。确保jackson-corejackson-databind在类路径上可用。