2 Replies - 393 Views - Last Post: 10 May 2020 - 09:58 AM

#1 pragati_dev   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 10-May 20

Required part formData is not present- springboot

Posted 10 May 2020 - 05:21 AM

Here I want to write a test for following controller method, on executing method of the testclass I'm getting an error as required part formData is not present, I've tried finding a solution for testing an object having 'RequestPart' annotation but no luck, can someone please take a look


public class UserController{
@PostMapping("/user")
public ResponseEntity<UserDTO> createUser(@RequestPart(value = "files", required= false) MultipartFile[] files,
@RequestPart("formData") UserDTO userDTO) throws URISyntaxException {

userService.save(userDTO, files);

}
}
Inside TestClass-

@BeforeEach
public void initTest()
{
user = createEntity(em);// inside this method I've set all the properties of userDTO and 'user' is a reference variable to User class
}

public void createUser() throws Exception {

UserDTO userDTO = userMapper.toDto(user);

 mockMvc.
  perform(post("/api/user")
 .contentType(TestUtil.APPLICATION_JSON_UTF8)
 .content(TestUtil.convertObjectToJsonBytes(userDTO)))
 .andExpect(status().isCreated()); 
}





Is This A Good Question/Topic? 0
  • +

Replies To: Required part formData is not present- springboot

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15724
  • View blog
  • Posts: 62,994
  • Joined: 12-June 08

Re: Required part formData is not present- springboot

Posted 10 May 2020 - 07:33 AM

Please copy/paste the full error message here.
Was This Post Helpful? 0
  • +
  • -

#3 pragati_dev   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 10-May 20

Re: Required part formData is not present- springboot

Posted 10 May 2020 - 09:58 AM

2020-05-10 19:32:22.819 WARN 15324 --- [ Test worker] o.z.problem.spring.common.AdviceTraits : Bad Request: Required request part 'formData' is not present

Status expected:<201> but was:<400>
Expected :201
Actual :400
<Click to see difference>

java.lang.Assertionerror: Status expected:<201> but was:<400>
at org.springframework.test.util.Assertionerrors.fail(Assertionerrors.java:59)
at org.springframework.test.util.Assertionerrors.assertEquals(Assertionerrors.java:98)
at org.springframework.test.web.servlet.result.StatusResultMatchers.lambda$matcher$9(StatusResultMatchers.java:619)
at org.springframework.test.web.servlet.MockMvc$1.andExpect(MockMvc.java:196)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1