我有下面的电子邮件控制器,用于发送带有RequestPart DTO对象(userDTO)和Multipart文件(最多上传3个文件)的电子邮件。其中userDTO是JSON对象
我尝试使用邮递员,它非常适合发送带有附件的电子邮件,但是我需要使用MockMVC进行单元测试,而我无法在Multipart和Request Part的这些组合中找到任何合适的示例。当我尝试使用下面的测试类时,我无法点击将触发电子邮件的控制器。
我的控制器
@PostMapping(path = "/api/email/sendEmail)
public ResponseEntity<UserDto> sendEmail(@RequestPart(value = "file", required = false) MultipartFile[] uploadfile,
@RequestPart UserDto userDTO, WebRequest webRequest) {
webRequest.setAttribute("userDTO", userDTO, RequestAttributes.SCOPE_REQUEST);
UserDto obj = emailService.sendEmail(userDTO, uploadfile);
return new ResponseEntity<>(obj, HttpStatus.OK);
}
请求部分附带的我的JSON(userDTO)
{
"sender":"sender@gmail.com",
"recipients":"receiver@gmail.com",
"subject":"Hi Testing Mail API",
"content":"This is my email"
}
我的测试班
@ContextConfiguration
@WebAppConfiguration
public class ServicesApplicationTests {
@Autowired
private WebApplicationContext webApplicationContext;
private MockMvc mockMvc;
@Test
public void testEmail() throws Exception {
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
mockMvc.perform(post("/api/email/sendEmail")
.contentType(MediaType.APPLICATION_JSON)
.content("{ \"sender\":\"sender@gmail.com\",\"recipients\":\"receiver@gmail.com\",\"subject\":\"Hi Testing Mail API\",\"content\":\"This is my email\"}")
.accept(MediaType.APPLICATION_JSON));
}
任何线索都将不胜感激。谢谢