Skip to content

Instantly share code, notes, and snippets.

@mstrYoda
Created August 24, 2018 22:57
Show Gist options
  • Save mstrYoda/b95d9cfb08fed62709bfb66f57261b89 to your computer and use it in GitHub Desktop.
Save mstrYoda/b95d9cfb08fed62709bfb66f57261b89 to your computer and use it in GitHub Desktop.
multipartfile controller unit test for medium
@RunWith(SpringRunner.class)
@WebMvcTest(UserController.class)
public class UserControllerTest {
@Autowired
MockMvc mockMvc;
@MockBean
UserService userService;
@Test
public void test() throws Exception {
UserCreateRequest request = new UserCreateRequest();
request.setName("test");
request.setBirthDate(new Date());
request.setGender("male");
MockMultipartFile file =
new MockMultipartFile(
"file",
"test contract.pdf",
MediaType.APPLICATION_PDF_VALUE,
"<<pdf data>>".getBytes(StandardCharsets.UTF_8));
ObjectMapper objectMapper = new ObjectMapper();
MockMultipartFile metadata =
new MockMultipartFile(
"request",
"request",
MediaType.APPLICATION_JSON_VALUE,
objectMapper.writeValueAsString(request).getBytes(StandardCharsets.UTF_8));
mockMvc.perform(
multipart("/users/")
.file(file)
.file(metadata)
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().string("testcontract.pdf"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment