[英]MockMVC JUnit 5 post UUID test
Hello I have a problem with post my controller.您好,我在发布我的控制器时遇到问题。 When I create mock test I get en exeption connected with UUID.
当我创建模拟测试时,我得到了与 UUID 相关的 exeption。 I dont know where is a problem.
不知道哪里出了问题。 I try to treate manual JSON post object, I try to delete quotes and nothing.
我尝试处理手动 JSON 帖子对象,我尝试删除引号,什么也不删除。 I have no idea where is a problem.
我不知道哪里出了问题。
@ExtendWith(MockitoExtension.class)
class UserControllerTest {
@Mock
UserService userService;
@InjectMocks
UserController userController;
MockMvc mockMvc;
@BeforeEach
void setUp() {
mockMvc = MockMvcBuilders.standaloneSetup(userController).build();
}
@Test
void name() throws Exception {
//Given
ResponseEntity<ApiResponse> response = ResponseEntity.ok((new ApiResponse(true,"Test Ok")));
ChangePasswordRequest changePasswordRequest = new ChangePasswordRequest("Password 2",UUID.randomUUID(), "Password");
given(userService.changeEmail(any(ChangeMailRequest.class))).willReturn(response);
Gson gson = new Gson();
String requestJson = gson.toJson(changePasswordRequest);
System.out.println(requestJson);
//When
MvcResult result = mockMvc.perform(post("/changePassword")
.contentType(MediaType.APPLICATION_JSON)
.characterEncoding("UTF-8")
.content(requestJson))
.andExpect(status().isOk())
.andReturn();
}
Controller class:控制器类:
@CrossOrigin("*")
@RestController
public class UserController {
final private UserService userService;
@PostMapping("changePassword")
@PreAuthorize("hasRole('USER')")
public ResponseEntity<?> changePassword(@RequestBody @Valid ChangePasswordRequest changePassword){
return userService.changePassword(changePassword);
}
Object:目的:
17:50:02.411 [main] INFO org.springframework.test.web.servlet.TestDispatcherServlet - Completed initialization in 3 ms
{"newPassword":"Password 2","privateId":"976575ae-c8aa-420f-a5f6-96e55cbe011f","Password":"Password"}
An Exeption:一个例外:
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.http.converter.HttpMessageConversionException: Type definition error: [simple type, class com.juniorstart.juniorstart.payload.ChangePasswordRequest]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `com.juniorstart.juniorstart.payload.ChangePasswordRequest` (no Creators, like default constructor, exist): cannot deserialize from Object value (no delegate- or property-based Creator)
at [Source: (PushbackInputStream); line: 1, column: 2]
link to github repo: https://github.com/hosu794/juniorstart-backend/tree/%23B041Adding_tests_for_UserController github 存储库链接: https : //github.com/hosu794/juniorstart-backend/tree/%23B041Adding_tests_for_UserController
您必须向ChangePasswordRequest
添加一个默认构造函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.