Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java MockMVC JUnit 5后UUID测试_Java_Spring_Mocking_Uuid_Junit5 - Fatal编程技术网

Java MockMVC JUnit 5后UUID测试

Java MockMVC JUnit 5后UUID测试,java,spring,mocking,uuid,junit5,Java,Spring,Mocking,Uuid,Junit5,你好,我的控制器有问题。当我创建模拟测试时,我得到与UUID连接的验证。我不知道哪里出了问题。我试图处理手动JSON post对象,我试图删除引号,什么都不做。我不知道哪里出了问题 @ExtendWith(MockitoExtension.class) class UserControllerTest { @Mock UserService userService; @InjectMocks UserController userController;

你好,我的控制器有问题。当我创建模拟测试时,我得到与UUID连接的验证。我不知道哪里出了问题。我试图处理手动JSON post对象,我试图删除引号,什么都不做。我不知道哪里出了问题

@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();
    }
例外:

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]

链接到github repo:

您必须向
ChangePasswordRequest
添加默认构造函数,哇,它可以工作,但为什么?你能给我解释一下吗?你需要默认构造函数,否则它不能从对象值反序列化。所以反序列化是必要的。谢谢,伙计,我从2天开始寻找解决这个问题的方法,我认为这是一个UUID问题。
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"}
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]