Java 使用多部分文件测试Spring引导REST控制器时出错

Java 使用多部分文件测试Spring引导REST控制器时出错,java,spring-boot,spring-mvc,junit,mockito,Java,Spring Boot,Spring Mvc,Junit,Mockito,控制器 @PostMapping(value = "/agents/log/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) public ResponseEntity<CallbackResponse> agentsLogFileUpload(@RequestParam("eventId") String id, @RequestPart("file") MultipartFile file) thro

控制器

@PostMapping(value = "/agents/log/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<CallbackResponse> agentsLogFileUpload(@RequestParam("eventId") String id,
        @RequestPart("file") MultipartFile file) throws ResourceNotFoundException, IOException {

    agentLogService.saveLogFile(file, file.getName(), id);
    eventService.updateEvent(id, new ExecutionSummary(Status.SUCCEEDED),    
                                serviceAuthClient.getDefaultJwtTokenObserver().getJwtAccessToken());
    return new ResponseEntity<>(new CallbackResponse(true), HttpStatus.OK);
}
@PostMapping(value=“/agents/log/upload”,consumes=MediaType.MULTIPART\u FORM\u DATA\u value)
公共响应代理LogFileUpload(@RequestParam(“eventId”)字符串id,
@RequestPart(“文件”)多部分文件)抛出ResourceNotFoundException、IOException{
agentLogService.saveLogFile(文件,file.getName(),id);
eventService.updateEvent(id,新的ExecutionSummary(Status.successed)),
serviceAuthClient.getDefaultJwtTokenObserver().getJwtAccessToken());
返回新的ResponseEntity(newcallbackresponse(true),HttpStatus.OK);
}
我编写了以下测试方法

@Test
public void testAgentsLogFileUpload() throws Exception {

    MockMultipartFile file = new MockMultipartFile("file", "test.txt", "multipart/form-data", "Test".getBytes());
    AgentLog agentLog = new AgentLog();
    Mono<Event> event = Mono.just(new Event());

    Mockito.when(agentLogService.saveLogFile(Mockito.any(), Mockito.anyString(), Mockito.anyString()))
            .thenReturn(agentLog);
    Mockito.when(eventService.updateEvent(Mockito.anyString(), Mockito.any(), Mockito.anyString()))
            .thenReturn(event);

    RequestBuilder requestBuilder = MockMvcRequestBuilders.multipart("/agents/log/upload").file(file)
            .contentType(MediaType.MULTIPART_FORM_DATA_VALUE).param("eventId", "123");
    MvcResult result = mvc.perform(requestBuilder).andDo(MockMvcResultHandlers.print()).andReturn();
    assertEquals(HttpStatus.OK.value(), result.getResponse().getStatus());
}
@测试
public void testAgentsLogFileUpload()引发异常{
MockMultipartFile file=新的MockMultipartFile(“file”、“test.txt”、“multipart/form data”、“test.getBytes());
AgentLog AgentLog=new AgentLog();
Mono event=Mono.just(new event());
Mockito.when(agentLogService.saveLogFile(Mockito.any(),Mockito.anyString(),Mockito.anyString())
.然后返回(代理日志);
Mockito.when(eventService.updateEvent(Mockito.anyString(),Mockito.any(),Mockito.anyString())
.然后返回(事件);
RequestBuilder RequestBuilder=MockMvcRequestBuilders.multipart(“/agents/log/upload”).file(file)
.contentType(MediaType.MULTIPART\u FORM\u DATA\u VALUE).param(“eventId”,“123”);
mvcresultresult=mvc.perform(requestBuilder.andDo(MockMvcResultHandlers.print()).andReturn();
assertEquals(HttpStatus.OK.value(),result.getResponse().getStatus());
}
我在下面添加了我的测试方法

    org.opentest4j.AssertionFailedError: expected: <200> but was: <500>
        at org.junit.jupiter.api.AssertionUtils.fail(AssertionUtils.java:55)
        at org.junit.jupiter.api.AssertionUtils.failNotEqual(AssertionUtils.java:62)
        at org.junit.jupiter.api.AssertEquals.assertEquals(AssertEquals.java:150)
        at org.junit.jupiter.api.AssertEquals.assertEquals(AssertEquals.java:145)
        at org.junit.jupiter.api.Assertions.assertEquals(Assertions.java:510)
        at com.cygilant.endpoint.security.cloud.rest.controller.ExecutionControllerTest.testAgentsLogFileUpload(ExecutionControllerTest.java:204)
org.opentest4j.AssertionFailedError:应为:但为:
位于org.junit.jupiter.api.AssertionUtils.fail(AssertionUtils.java:55)
位于org.junit.jupiter.api.AssertionUtils.failNotEqual(AssertionUtils.java:62)
位于org.junit.jupiter.api.AssertEquals.AssertEquals(AssertEquals.java:150)
位于org.junit.jupiter.api.AssertEquals.AssertEquals(AssertEquals.java:145)
位于org.junit.jupiter.api.Assertions.assertEquals(Assertions.java:510)
位于com.cygilant.endpoint.security.cloud.rest.controller.ExecutionControllerTest.testAgentsLogFileUpload(ExecutionControllerTest.java:204)

当我运行这个测试时,我得到HTTP错误500和NullPointerException。有人能告诉我哪里做错了吗?

如果有错误500,应该有例外。你能提供它吗?空指针例外为什么测试映射是“/agents/log/upload”而postMapping值是“/managers/log/upload”?我添加了错误的方法我正在升级它也添加了错误跟踪以更好地理解错误如果出现错误500,应该会出现异常。你能提供它吗?空指针例外为什么测试映射是“/agents/log/upload”而postMapping值是“/managers/log/upload”?我添加了错误的方法我正在升级它也添加了错误跟踪以便更好地理解