测试人社区

【求助】mock接口测试上传文件和传参无法做到统一,返回201,文件为空

如下述代码所示,我想要模拟传参和上传文件请求,运行后返回了201,不是200,查看上传的文件也为空,想知道如何解决这个问题?

 @Test
    @Parameters(source = DataSource.class, method = "crashReportData")
    public void crashReportingTest1(Object appId, Object sign, Object data, Object apiVersion, Object zipUrl, Object result, Object logs) throws Exception {
        // 创建一个输出记录器listAppender
        ListAppender<ILoggingEvent> listAppender = new ListAppender<>();
        listAppender.start();
        // 将输出加到list里
        ReportControllerLOGGER.addAppender(listAppender);
        String version=null;
        if(apiVersion !=null) {
            version = String.valueOf(apiVersion);
        }
        File file=new File(String.valueOf(zipUrl));
//        InputStream fis = new FileInputStream("E:/ZipFile/data2.txt");
//        String str=String.valueOf(zipUrl);
//                InputStream is = new ByteArrayInputStream(str.getBytes());
        MockMultipartFile mfile = new MockMultipartFile("file", file.getName(), "txt", new FileInputStream(file));
//        this.ReportController.crashReporting(mfile, request, cmd)
        this.mockMvc.perform(MockMvcRequestBuilders.fileUpload("/crash/reporting")
                .file(mfile)
//                .contentType(MediaType.MULTIPART_FORM_DATA)
                .param("appId", String.valueOf(appId))
                .param("sign", String.valueOf(sign))
                .param("data", String.valueOf(data)))
                .andExpect(status().is(Integer.valueOf(String.valueOf(result))))
                .andReturn().getResponse().getContentAsString();
        //junit 日志断言
        List<ILoggingEvent> logsList = listAppender.list;
        System.out.println("log" + logsList);
        //list<object>转换成list<String>
        List<String> strs = (List<String>) (List) logs;
        System.out.println(strs);
        //AssertJ流式断言还可断言列表元素的某些特定属性
        if (strs != null) {
            if ((strs.size()) > 3) {
                Assertions.assertThat(listAppender.list)
                        .extracting(ILoggingEvent::getMessage, ILoggingEvent::getLevel)
                        .contains(Tuple.tuple(strs.get(0), Level.INFO), Tuple.tuple(strs.get(2), Level.ERROR), Tuple.tuple(strs.get(3), Level.INFO));
                Assert.assertThat(logsList.get(2).getMessage(), containsString(strs.get(1)));
            } else if ((strs.size()) > 1) {
                Assertions.assertThat(listAppender.list)
                        .extracting(ILoggingEvent::getMessage, ILoggingEvent::getLevel)
                        .contains(Tuple.tuple(strs.get(0), Level.INFO),
                                Tuple.tuple(strs.get(1), Level.INFO), Tuple.tuple(strs.get(2), Level.INFO));
            } else {
                String logresult = logsList.get(0).getMessage();
//            System.out.println(logsList.get(0).getMessage());
                Assert.assertThat(logresult, containsString(String.valueOf(logs)));
                assertEquals(Level.INFO, logsList.get(0).getLevel());
            }
        }
    }

请按照提bug的方式提问,不然无法给你任何有效的解答