我有以下测试,它工作正常。然而在我看来它有点矫枉过正。(也需要一段时间)调出一个完整的 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 来简化它。但是我没有在这里加载弹簧杰克逊配置。
public class WirelessSerializationTest {
//@Autowired
ObjectMapper objectMapper = new ObjectMapper();
所以我的问题是这个。我可以在测试中测试和加载 Springs ObjectMapper 实例而不需要加载所有 Spring 吗?
叮当猫咪
交互式爱情
相关分类