Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unit testing SpringMVC4.2:如何使用@RequestPart参数对控制器进行单元测试_Unit Testing_Spring Mvc_Testing_Mockmvc - Fatal编程技术网

Unit testing SpringMVC4.2:如何使用@RequestPart参数对控制器进行单元测试

Unit testing SpringMVC4.2:如何使用@RequestPart参数对控制器进行单元测试,unit-testing,spring-mvc,testing,mockmvc,Unit Testing,Spring Mvc,Testing,Mockmvc,我有一个表单的请求映射: @RequestMapping( value = "/submitCase", consumes = MediaType.MULTIPART_FORM_DATA_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE, method = RequestMethod.POST ) public Object submitCase( @RequestPart(name = "a

我有一个表单的请求映射:

@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为此编写一个测试。不过,我不能这样做

这里的挑战是请求处理程序使用多部分/表单数据,其中包括4个多部分文件和1个Json数据

有没有办法解决这个问题?请记住,我必须使用Spring 4.3


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

看看这里的一个很好的例子:

请注意,示例中使用的功能在Spring 4.3.0中还不可用。改为使用(在Spring5中已弃用)

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
中,JSON数据必须包装在与上载文件等效的
MockMultipartFile
中。确保类路径上有
jackson-core
jackson-databind
可用。

嘿,我尝试过使用它,但我还需要发送CaseDetails参数的json数据。我不能用文件上传来实现这一点。有什么想法吗?我更新了我的答案。从Spring
MockMvc
4.3.0的角度来看,多部分/表单数据请求上下文中的JSON与文件的处理方式相同:将数据包装在
MockMultipartFile
中。为了指出这一点,我扩展了这个例子。这对你有用吗?我的回答有助于解决你的问题吗?
@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();
        }
    }
}