Created
August 24, 2018 22:57
-
-
Save mstrYoda/b95d9cfb08fed62709bfb66f57261b89 to your computer and use it in GitHub Desktop.
multipartfile controller unit test for medium
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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