使用MockMVC在Junit5春季启动中jsonPath cast问题

问题描述

我有以下测试用例,导入为

import org.springframework.http.MediaType;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.web.servlet.MockMvc;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.assertj.core.internal.bytebuddy.matcher.ElementMatchers.is;
import static org.mockito.Mockito.doReturn;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
      @Test
            @DisplayName("Should return product based on the specified Id")
            void shouldReturnProductBasedOnTheSpecifiedId() throws Exception {
                String Id = java.util.UUID.randomUUID().toString();
                ProductViewModel productViewModel = new ProductViewModel(Id,"Product 1",100,"Product 1 description",0);
                doReturn(productViewModel).when(productService).findById(Id);
                mockMvc.perform(get(String.format("/api/v1/product/%s",Id)))
    
                        //Validate the response code and content type
                        .andExpect(status().isOk())
                        .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
    
                        //validate the headers
                        .andExpect(header().string(HttpHeaders.ETAG,String.format("\"%s\"",Id)))
                        .andExpect(header().string(HttpHeaders.LOCATION,String.format("/%s",Id)))
    
                        //Validate the return fields
                        .andExpect(jsonPath("$.id",is(Id)));
                        //.andExpect((ResultMatcher) jsonPath("$.name",is("Product 1")))
                        //.andExpect((ResultMatcher) jsonPath("$.price",is(100)))
                        //.andExpect((ResultMatcher) jsonPath("$.description",is("Product 1 description")))
                        //.andExpect((ResultMatcher) jsonPath("$.version",is(0)));
    
            }

获取错误信息

enter image description here

如果投射对象,则会收到投射错误消息。

enter image description here

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)