I have a simple PUT endpoint in a Spring Boot application:
@PutMapping()
public ResponseEntity<String> upload(@RequestParam("cats") MultipartFile file) throws IOException {
I'm trying to create a test for the controller using MockMVC
:
import org.springframework.test.web.servlet.MockMvc;
@RunWith(SpringRunner.class)
@WebMvcTest(CatController.class)
public class CatControllerTest {
@Autowired
private MockMvc mockMvc;
...
For POST
endpoints I use something like that:
MockMultipartFile multipartFile = new MockMultipartFile("file", new FileInputStream("myCats.csv"));
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).alwaysDo(print()).build();
MvcResult result = mockMvc.perform(MockMvcRequestBuilders.multipart("/uploadCats").file(multipartFile))
.andExpect(status().isOk())
.andReturn();
But when trying to translate the above call to PUT endpoint I find that I can create
MockMvcRequestBuilders.put("/catUpload")
But then I can't chain to it the multipart
Or I can do:
MockMvcRequestBuilders.multipart(...)
But then I can't chain to it the put
.
I saw some post about this problem, but they all were few years old. Is there a way to do it?