gpt4 book ai didi

java - Spring Boot 2 测试json序列化

转载 作者:行者123 更新时间:2023-11-29 04:14:45 24 4
gpt4 key购买 nike

我有以下测试并且工作正常。然而在我看来它有点矫枉过正。 (也需要一段时间)调出一个完整的 spring 实例来测试一些 json 序列化。

@RunWith(SpringRunner.class)
@SpringBootTest
public class WirelessSerializationTest {

@Autowired
ObjectMapper objectMapper;

@Test
public void testDateSerialization() throws IOException {

Resource resource = new ClassPathResource("subscription.json");
File file = resource.getFile();

CustomerSubscriptionDto customerSubscriptionDto = objectMapper.readValue(file, CustomerSubscriptionDto.class);
LocalDateTime actualResult = customerSubscriptionDto.getEarliestExpiryDate();

LocalDate expectedDate = LocalDate.of(2018, 10, 13);
LocalTime expectedTime = LocalTime.of( 10, 18, 48);
LocalDateTime expectedResult = LocalDateTime.of(expectedDate,expectedTime);
Assert.assertEquals("serialised date ok", expectedResult, actualResult);

String jsonOutput = objectMapper.writeValueAsString(customerSubscriptionDto);
String expectedExpiryDate = "\"earliestExpiryDate\":\"2018-10-13T10:18:48Z\"";

}

}

现在我已经能够通过删除 SpringRunner 来简化它。但是我没有在此处加载 springs jackson 配置。

public class WirelessSerializationTest {

//@Autowired
ObjectMapper objectMapper = new ObjectMapper();

所以我的问题是这样的。我可以在测试中测试和加载 Springs ObjectMapper 实例而不需要加载所有 Spring 吗?

最佳答案

关于java - Spring Boot 2 测试json序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53098758/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com