Rest Assured 不使用自定义 ObjectMapper

2024-03-27

我正在使用 Rest Assured 为我的 Spring Boot 项目进行控制器测试。我们在序列化对象上使用 Java 8 ZonedDateTime 字段,并使用 Jackson 2 进行序列化。运行项目时,序列化按预期工作,但运行放心测试时,日期未正确序列化。我知道 Rest Assured 使用自己的 ObjectMapper 配置,但我的问题是,无论我做什么,“放心”似乎忽略了我的 ObjectMapper 配置。

以下是我测试的相关部分:

@RunWith(SpringRunner.class)
@SpringBootTest(classes=MyApplication.class)
@WebAppConfiguration
@Transactional
public class MAppControllerTest {

    @Autowired
    private WebApplicationContext context;

    private MockMvc mockMvc;

    @Before
    public void setUp() throws Exception {

        RestAssured.config = RestAssuredConfig.config().objectMapperConfig(
              ObjectMapperConfig.objectMapperConfig().jackson2ObjectMapperFactory(new Jackson2ObjectMapperFactory() {
                  @SuppressWarnings("rawtypes") 
                  @Override 
                  public ObjectMapper create(Class cls, String charset) { 
                    ObjectMapper objectMapper = new ObjectMapper(); 
                    objectMapper.registerModule(new JavaTimeModule()); 
                    objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); 
                    objectMapper.setTimeZone(TimeZone.getTimeZone("America/New_York"));
                    objectMapperconfigure(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE, true);
                    return objectMapper; 
                  } 
            })
         );

        mockMvc = MockMvcBuilders.webAppContextSetup(context)
            .apply(springSecurity())
            .build();
    }

    @Test
    @WithMockUser(roles = {"APP_ADMIN"}, username = "TEST_USER")
    @Sql(scripts = "/db-scripts/test-data.sql")
    public void testCreateItem() {
        Item postBody = new Item();
        postBody.setDate(ZonedDateTime.now());

        Item result =
        given().
            mockMvc(mockMvc).
            contentType(ContentType.JSON).
            body(postBody).
            log().all().
        when().
            post("/items").
        then().
            log().all().
            statusCode(201).
            body(matchesJsonSchemaInClasspath("json-schemas/item.json")).
            extract().as(Item.class);

        assertThat(result.getDate(), equalTo(postBody.getDate());
    }

物品类别:

@Data
@Entity
@Table(name = "ITEM", schema = "MY_APP")
@EqualsAndHashCode(of = "id")
@NoArgsConstructor
@AllArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
public class Item implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "ITEM_ID_SEQ")
    @SequenceGenerator(name = "ITEM_ID_SEQ", sequenceName = "MY_APP.ITEM_ID_SEQ")
    private Long id;

    @Column(name = "DATE")
    private ZonedDateTime date;
}

我们使用的是 spring-boot-starter-data-jpa,所以我们只是声明了一个扩展 CrudRepository(Item, Long) 的 ItemRepository 接口(我只是在这里使用了括号,因为堆栈溢出不喜欢尖括号)并且控制器调用该服务调用内置的保存方法来持久化项目并返回持久化的项目,因此在我的测试中,我期望我发送的保存日期与返回时的日期相同。

我遇到的主要问题是返回的日期是 GMT,而我的期望是 EST/EDT,因此断言失败,因为 ZonedDateTime.now() 使用本地时区,但无论什么原因是,放心,在某个地方,将时区更改为 GMT:

java.lang.AssertionError:
Expected: <2017-03-30T14:53:19.102-04:00[America/New_York]>
but: was <2017-03-30T18:53:19.102Z[GMT]>

还有一个日期甚至没有被序列化为 ISO,而是一个奇怪的小数。不知道这是怎么回事,但项目运行时不会发生这种情况;这些问题都没有。仅当运行放心测试时才会出现这种情况。

无论我在测试设置中配置什么 ObjectMapper,我都会收到此错误...无论我是否有 ObjectMapper,它都会被完全忽略。

相关技术版本:

  • Spring Boot:1.5.2.RELEASE
  • com.fasterxml.jackson.core:jackson-databind: 2.6.4
  • com.fasterxml.jackson.core:杰克逊注释:2.6.4
  • com.fasterxml.jackson.core:jackson-core: 2.6.4
  • com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.6.4
  • com.jayway.restassured:rest-assured: 2.9.0
  • com.jayway.restassured:json-schema-validator:2.9.0
  • com.jayway.restassured:spring-mock-mvc: 2.9.0

你不应该使用RestAssured.config and RestAssuredConfig将 RestAssured 与 MockMvc 结合使用时。而是使用io.restassured.module.mockmvc.RestAssuredMockMvc and io.restassured.module.mockmvc.config.RestAssuredMockMvcConfig。 IE。替换您的代码:

RestAssured.config = RestAssuredConfig.config().objectMapperConfig(
   ...
);

with:

RestAssuredMockMvc.config = RestAssuredConfigMockMvc.config().objectMapperConfig(
    ...
);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Rest Assured 不使用自定义 ObjectMapper 的相关文章

随机推荐