使用 JUint5 在 Mockmvc 中传递列表对象

Pass list object in Mockmvc using JUint5

我正在使用 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();

我该如何解决

您可以自动装配 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/

问题是 content() 方法需要 JSON 字符串或 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();