【问题标题】:Pass list object in Mockmvc using JUint5使用 JUint5 在 Mockmvc 中传递列表对象
【发布时间】:2020-03-02 08:04:35
【问题描述】:

我正在使用 JUnit5 进行集成测试。我有一个需要传递到终点的对象列表。 Mockmvc 的 content 参数只接受字符串。我怎样才能传递这个对象?任何想法将不胜感激。

这是我的代码

    Customer cust1 = Customer.builder().name("Syed")
            .location("India").build();
Customer cust2 = Customer.builder().name("Ali")
            .location("India").build();


    List<Customer> customers = Arrays.asList(cust1, cust2); --> This needs to be passed. 

    MvcResult result = mockMvc.perform(MockMvcRequestBuilders.post("/process")

            .content(customers) --> Compilation error here
            .contentType(MediaType.APPLICATION_JSON)
            .accept(MediaType.APPLICATION_JSON))
            .andExpect(status().isOk())
            .andReturn();

我该如何解决

【问题讨论】:

    标签: java mockito junit5


    【解决方案1】:

    您可以自动连接 ObjectMapper 并将您的对象 (customers) 转换为字符串:

    @Autowired
    private ObjectMapper;
    
    // in your method
    .content(objectMapper.writeValueAsString(customers))
    // ..
    

    完整示例:

    MvcResult result = mockMvc.perform(MockMvcRequestBuilders.post("/process")
    
        .content(objectMapper.writeValueAsString(customers)) // change applied
        .contentType(MediaType.APPLICATION_JSON)
        .accept(MediaType.APPLICATION_JSON))
        .andExpect(status().isOk())
        .andReturn();
    

    这假设你的类路径中有jacksoncom.fasterxml.jackson.core:jackson-databind 用于ObjectMapper,spring Boot 的JacksonAutoConfiguration 应该创建objectMapper bean)

    对maven的依赖

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.10.2</version>
    </dependency>
    

    要查找更新版本,请查看:https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind/

    【讨论】:

    • Objectmapper 为 null
    • Spring boot 版本有问题吗
    • @Syed,我已经更新了我的答案,以反映您的类路径(gradle/maven)中需要jackson,以便自动拥有objectMapper bean
    • Jackson for Maven 项目的依赖关系是什么
    • 即使添加了依赖 objectmapper 为 null
    【解决方案2】:

    问题在于 content() 方法需要 JSON String 或 byte[] 数组。 您可以使用 TestUtil(使用 Jackson objectMapper)进行 mockmvc 测试:

    public final class TestUtil {
    
        private static final ObjectMapper mapper = createObjectMapper();
    
        /** MediaType for JSON UTF8 */
        public static final MediaType APPLICATION_JSON_UTF8 = MediaType.APPLICATION_JSON_UTF8;
    
        private static ObjectMapper createObjectMapper() {
            ObjectMapper mapper = new ObjectMapper();
            mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
            mapper.registerModule(new JavaTimeModule());
            return mapper;
        }
    
        /**
         * Convert an object to JSON byte array.
         *
         * @param object the object to convert.
         * @return the JSON byte array.
         * @throws IOException
         */
        public static byte[] convertObjectToJsonBytes(Object object) throws IOException {
            return mapper.writeValueAsBytes(object);
        }
    
             /**
             * Convert an object to JSON String.
             *
             * @param object the object to convert.
             * @return the JSON String.
             * @throws IOException
             */
            public static String convertObjectToJsonBytes(Object object) throws IOException {
                return mapper.writeValueAsString(object);
            }
        }
    

    然后:

    Customer cust1 = Customer.builder().name("Syed")
                .location("India").build();
    Customer cust2 = Customer.builder().name("Ali")
                .location("India").build();
    
    List<Customer> customers = Arrays.asList(cust1, cust2); --> This needs to be passed. 
    
    MvcResult result = mockMvc.perform(MockMvcRequestBuilders.post("/process")
            .content(TestUtil.convertObjectToJsonBytes(customers))
            .contentType(TestUtil.APPLICATION_JSON_UTF8)
            .accept(TestUtil.APPLICATION_JSON_UTF8)
            .andExpect(status().isOk())
            .andReturn();
    

    【讨论】:

    • 为什么要使用 convertObjectToJsonBytes 方法?
    • Syed ,你可以做 writeValueAsString()。出于测试原因,这无关紧要:)
    • 我没有明白你使用“writeValueAsString”的意思。在您的代码中,您使用的是 writeValueAsBytes。应该使用哪一个?
    • Syed,可以使用其中之一。不管是哪个
    • 我会检查并回复您。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-02
    • 1970-01-01
    • 2011-03-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多