• Chapter 2: Annotations - @JsonTest
    • 例子1:简单例子
    • 例子2: 测试@JsonComponent
    • 例子3: 使用@ContextConfiguration
    • 参考文档

    Chapter 2: Annotations - @JsonTest

    @JsonTest是Spring Boot提供的方便测试JSON序列化反序列化的测试工具,在Spring Boot的文档中有一些介绍。

    需要注意的是@JsonTest需要Jackson的ObjectMapper,事实上如果你的Spring Boot项目添加了spring-web的Maven依赖,JacksonAutoConfiguration就会自动为你配置一个:

    1. <dependency>
    2. <groupId>org.springframework.boot</groupId>
    3. <artifactId>spring-boot-autoconfigure</artifactId>
    4. </dependency>
    5. <dependency>
    6. <groupId>org.springframework</groupId>
    7. <artifactId>spring-web</artifactId>
    8. </dependency>

    这里没有提供关于日期时间的例子,关于这个比较复杂,可以看我的另一篇文章:Spring Boot Jackson对于日期时间类型处理的例子。

    例子1:简单例子

    源代码见SimpleJsonTest:

    1. @SpringBootTest(classes = SimpleJsonTest.class)
    2. @JsonTest
    3. public class SimpleJsonTest extends AbstractTestNGSpringContextTests {
    4. @Autowired
    5. private JacksonTester<Foo> json;
    6. @Test
    7. public void testSerialize() throws Exception {
    8. Foo details = new Foo("Honda", 12);
    9. // 使用通包下的json文件测试结果是否正确
    10. assertThat(this.json.write(details)).isEqualToJson("expected.json");
    11. // 或者使用基于JSON path的校验
    12. assertThat(this.json.write(details)).hasJsonPathStringValue("@.name");
    13. assertThat(this.json.write(details)).extractingJsonPathStringValue("@.name").isEqualTo("Honda");
    14. assertThat(this.json.write(details)).hasJsonPathNumberValue("@.age");
    15. assertThat(this.json.write(details)).extractingJsonPathNumberValue("@.age").isEqualTo(12);
    16. }
    17. @Test
    18. public void testDeserialize() throws Exception {
    19. String content = "{\"name\":\"Ford\",\"age\":13}";
    20. Foo actual = this.json.parseObject(content);
    21. assertThat(actual).isEqualTo(new Foo("Ford", 13));
    22. assertThat(actual.getName()).isEqualTo("Ford");
    23. assertThat(actual.getAge()).isEqualTo(13);
    24. }
    25. }

    例子2: 测试@JsonComponent

    @JsonTest可以用来测试@JsonComponent。

    这个例子里使用了自定义的@JsonComponent FooJsonComponent:

    1. @JsonComponent
    2. public class FooJsonComponent {
    3. public static class Serializer extends JsonSerializer<Foo> {
    4. @Override
    5. public void serialize(Foo value, JsonGenerator gen, SerializerProvider serializers)
    6. throws IOException, JsonProcessingException {
    7. // ...
    8. }
    9. }
    10. public static class Deserializer extends JsonDeserializer<Foo> {
    11. @Override
    12. public Foo deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
    13. // ...
    14. }
    15. }
    16. }

    测试代码JsonComponentJsonTest:

    1. @SpringBootTest(classes = { JsonComponentJacksonTest.class, FooJsonComponent.class })
    2. @JsonTest
    3. public class JsonComponentJacksonTest extends AbstractTestNGSpringContextTests {
    4. @Autowired
    5. private JacksonTester<Foo> json;
    6. @Test
    7. public void testSerialize() throws Exception {
    8. Foo details = new Foo("Honda", 12);
    9. assertThat(this.json.write(details).getJson()).isEqualTo("\"name=Honda,age=12\"");
    10. }
    11. @Test
    12. public void testDeserialize() throws Exception {
    13. String content = "\"name=Ford,age=13\"";
    14. Foo actual = this.json.parseObject(content);
    15. assertThat(actual).isEqualTo(new Foo("Ford", 13));
    16. assertThat(actual.getName()).isEqualTo("Ford");
    17. assertThat(actual.getAge()).isEqualTo(13);
    18. }
    19. }

    例子3: 使用@ContextConfiguration

    事实上@JsonTest也可以配合@ContextConfiguration一起使用。

    源代码见ThinJsonTest:

    1. @JsonTest
    2. @ContextConfiguration(classes = JsonTest.class)
    3. public class ThinJsonTest extends AbstractTestNGSpringContextTests {
    4. @Autowired
    5. private JacksonTester<Foo> json;
    6. @Test
    7. public void testSerialize() throws Exception {
    8. // ...
    9. }
    10. @Test
    11. public void testDeserialize() throws Exception {
    12. // ...
    13. }
    14. }

    参考文档

    • Spring Framework Testing
    • Spring Boot Testing
    • @JsonTest
    • JsonComponent
    • JacksonAutoConfiguration
    • JacksonTester
    • GsonTester
    • BasicJsonTester