From 7f68047941633f3841ed8e23bb1523408f71cbf1 Mon Sep 17 00:00:00 2001 From: Grzegorz Piwowarek Date: Sun, 13 Oct 2024 21:26:47 +0200 Subject: [PATCH] Descriptive test names (#222) --- .../io/vavr/jackson/datatype/CharSeqTest.java | 12 +- .../io/vavr/jackson/datatype/EitherTest.java | 105 +++++++++--------- .../vavr/jackson/datatype/FunctionTest.java | 101 ++++++++++------- .../io/vavr/jackson/datatype/LazyTest.java | 16 +-- .../io/vavr/jackson/datatype/MixedTest.java | 30 ++--- .../jackson/datatype/OptionPlainTest.java | 15 +-- .../io/vavr/jackson/datatype/OptionTest.java | 58 +++++----- .../jackson/datatype/PriorityQueueTest.java | 38 +++---- .../io/vavr/jackson/datatype/ScalarTest.java | 63 ++++++++--- .../datatype/SerializationFeatureTest.java | 41 +++---- .../jackson/datatype/ServiceLoaderTest.java | 32 +++--- 11 files changed, 275 insertions(+), 236 deletions(-) diff --git a/src/test/java/io/vavr/jackson/datatype/CharSeqTest.java b/src/test/java/io/vavr/jackson/datatype/CharSeqTest.java index f9be638..c9b242f 100644 --- a/src/test/java/io/vavr/jackson/datatype/CharSeqTest.java +++ b/src/test/java/io/vavr/jackson/datatype/CharSeqTest.java @@ -1,7 +1,5 @@ package io.vavr.jackson.datatype; -import static org.junit.jupiter.api.Assertions.assertThrows; - import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectWriter; @@ -11,10 +9,12 @@ import java.io.IOException; +import static org.junit.jupiter.api.Assertions.assertThrows; + class CharSeqTest extends BaseTest { @Test - void test1() throws IOException { + void shouldSerializeAndDeserializeCharSeq() throws IOException { ObjectWriter writer = mapper().writer(); CharSeq src = CharSeq.of("abc"); String json = writer.writeValueAsString(src); @@ -24,7 +24,7 @@ void test1() throws IOException { } @Test - void test2() throws IOException { + void shouldSerializeAndDeserializeWrappedCharSeqAsObject() throws IOException { ObjectMapper mapper = mapper().addMixIn(CharSeq.class, WrapperObject.class); CharSeq src = CharSeq.of("abc"); String plainJson = mapper().writeValueAsString(src); @@ -35,7 +35,7 @@ void test2() throws IOException { } @Test - void test3() throws IOException { + void shouldSerializeAndDeserializeWrappedCharSeqAsArray() throws IOException { ObjectMapper mapper = mapper().addMixIn(CharSeq.class, WrapperArray.class); CharSeq src = CharSeq.of("abc"); String plainJson = mapper().writeValueAsString(src); @@ -46,7 +46,7 @@ void test3() throws IOException { } @Test - void test4() { + void shouldThrowExceptionWhenDeserializingInvalidCharSeq() { assertThrows(JsonMappingException.class, () -> mapper().readValue("42", CharSeq.class)); } } diff --git a/src/test/java/io/vavr/jackson/datatype/EitherTest.java b/src/test/java/io/vavr/jackson/datatype/EitherTest.java index 5267629..8beaac5 100644 --- a/src/test/java/io/vavr/jackson/datatype/EitherTest.java +++ b/src/test/java/io/vavr/jackson/datatype/EitherTest.java @@ -4,30 +4,27 @@ import com.fasterxml.jackson.annotation.JsonTypeName; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.JsonMappingException; - import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.module.scala.DefaultScalaModule; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - -import java.io.IOException; -import java.math.BigInteger; - import io.vavr.Tuple; import io.vavr.collection.HashSet; import io.vavr.collection.List; import io.vavr.collection.Set; import io.vavr.control.Either; import io.vavr.control.Option; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.math.BigInteger; import static io.vavr.control.Option.none; import static io.vavr.control.Option.some; public class EitherTest extends BaseTest { - @SuppressWarnings("unchecked") @Test - void testWrapperObject() throws IOException { + void shouldSerializeAndDeserializeWrappedEitherObject() throws IOException { ObjectMapper mapper = mapper().addMixIn(Either.class, WrapperObject.class); Either src = Either.right(1); String plainJson = mapper().writeValueAsString(src); @@ -37,18 +34,16 @@ void testWrapperObject() throws IOException { Assertions.assertEquals(src, restored); } - @SuppressWarnings("unchecked") @Test - void test1() throws IOException { + void shouldSerializeAndDeserializeLeftEither() throws IOException { Either left = Either.left("left"); String json = mapper().writer().writeValueAsString(left); Either restored = mapper().readValue(json, Either.class); Assertions.assertEquals(left, restored); } - @SuppressWarnings("unchecked") @Test - void test2() throws IOException { + void shouldSerializeAndDeserializeRightEither() throws IOException { Either right = Either.right(1); String json = mapper().writer().writeValueAsString(right); Either restored = mapper().readValue(json, Either.class); @@ -56,7 +51,7 @@ void test2() throws IOException { } @Test - void test3() throws IOException { + void shouldThrowExceptionWhenInvalidJsonHasExtraRightValue() throws IOException { Assertions.assertThrows(JsonMappingException.class, () -> { String json = "[\"right\", 2, 3]"; mapper().readValue(json, Either.class); @@ -64,7 +59,7 @@ void test3() throws IOException { } @Test - void test4() throws IOException { + void shouldThrowExceptionWhenInvalidJsonHasMissingRightValue() throws IOException { Assertions.assertThrows(JsonMappingException.class, () -> { String json = "[\"right\"]"; mapper().readValue(json, Either.class); @@ -72,7 +67,7 @@ void test4() throws IOException { } @Test - void test5() throws IOException { + void shouldThrowExceptionWhenInvalidJsonHasMalformedLeftType() throws IOException { Assertions.assertThrows(JsonMappingException.class, () -> { String json = "[\"lEft\", 42]"; mapper().readValue(json, Either.class); @@ -80,38 +75,41 @@ void test5() throws IOException { } @Test - void test6() throws IOException { + void shouldThrowExceptionWhenInvalidJsonHasNonStringLeftOrRight() throws IOException { Assertions.assertThrows(JsonMappingException.class, () -> { String json = "[42, 42]"; mapper().readValue(json, Either.class); }); } - @SuppressWarnings("unchecked") @Test - void test7() throws IOException { + void shouldSerializeAndDeserializeLeftEitherWithGenericTypes() throws IOException { Either, Set> either = Either.left(List.of(42)); String json = mapper().writer().writeValueAsString(either); - Either, Set> restored = mapper().readValue(json, new TypeReference, Set>>() {}); + Either, Set> restored = mapper().readValue(json, new TypeReference, Set>>() { + }); Assertions.assertEquals(either, restored); } - @SuppressWarnings("unchecked") @Test - void test8() throws IOException { + void shouldSerializeAndDeserializeRightEitherWithGenericTypes() throws IOException { Either, Set> either = Either.right(HashSet.of(42.0)); String json = mapper().writer().writeValueAsString(either); - Either, Set> restored = mapper().readValue(json, new TypeReference, Set>>() {}); + Either, Set> restored = mapper().readValue(json, new TypeReference, Set>>() { + }); Assertions.assertEquals(either, restored); } - @SuppressWarnings("unchecked") @Test - void testNullSerialization() throws IOException { + void shouldHandleNullInLeftEitherDuringSerialization() throws IOException { Either left = Either.left(null); String leftJson = mapper().writer().writeValueAsString(left); Either restoredLeft = mapper().readValue(leftJson, Either.class); Assertions.assertEquals(left, restoredLeft); + } + + @Test + void shouldHandleNullInRightEitherDuringSerialization() throws IOException { Either right = Either.left(null); String rightJson = mapper().writer().writeValueAsString(right); Either restoredRight = mapper().readValue(rightJson, Either.class); @@ -119,35 +117,19 @@ void testNullSerialization() throws IOException { } @Test - void testWithOption() throws IOException { - TypeReference, Option>> typeReference = new TypeReference, Option>>() {}; + void shouldSerializeAndDeserializeEitherWithOption() throws IOException { + TypeReference, Option>> typeReference = new TypeReference, Option>>() { + }; verifySerialization(typeReference, List.of( - Tuple.of(Either.left(none()), genJsonList("left", null)), - Tuple.of(Either.right(none()), genJsonList("right", null)), - Tuple.of(Either.left(some("value")), genJsonList("left", "value")), - Tuple.of(Either.right(some("value")), genJsonList("right", "value")) + Tuple.of(Either.left(none()), genJsonList("left", null)), + Tuple.of(Either.right(none()), genJsonList("right", null)), + Tuple.of(Either.left(some("value")), genJsonList("left", "value")), + Tuple.of(Either.right(some("value")), genJsonList("right", "value")) )); } - @JsonTypeInfo( - use = JsonTypeInfo.Id.NAME, - include = JsonTypeInfo.As.WRAPPER_OBJECT, - property = "type") - @JsonTypeName("card") - private static class TestSerialize { - public String type = "hello"; - } - - private static class Left { - public Either f = Either.left(new TestSerialize()); - } - - private static class Right { - public Either f = Either.right(new TestSerialize()); - } - @Test - void testJsonTypeInfo() throws IOException { + void shouldSerializeAndDeserializeCustomTypeWithJsonTypeInfo() throws IOException { String javaUtilValue; javaUtilValue = mapper().writeValueAsString(new Left()); Assertions.assertEquals("{\"f\":[\"left\",{\"card\":{\"type\":\"hello\"}}]}", javaUtilValue); @@ -160,19 +142,38 @@ void testJsonTypeInfo() throws IOException { } @Test - void testDeserializingScalaEither() throws IOException { + void shouldSerializeAndDeserializeScalaEither() throws IOException { final scala.util.Either left = scala.util.Left.apply("test"); final scala.util.Either right = scala.util.Right.apply(BigInteger.ONE); final ObjectMapper mapper = mapper().registerModule(new DefaultScalaModule()); final String serializedLeft = mapper.writeValueAsString(left); final Either deserializedLeft = - mapper.readValue(serializedLeft, new TypeReference>() { }); + mapper.readValue(serializedLeft, new TypeReference>() { + }); Assertions.assertEquals("test", deserializedLeft.getLeft()); final String serializedRight = mapper.writeValueAsString(right); final Either deserializedRight = - mapper.readValue(serializedRight, new TypeReference>() { }); + mapper.readValue(serializedRight, new TypeReference>() { + }); Assertions.assertEquals(BigInteger.ONE, deserializedRight.get()); } + + @JsonTypeInfo( + use = JsonTypeInfo.Id.NAME, + include = JsonTypeInfo.As.WRAPPER_OBJECT, + property = "type") + @JsonTypeName("card") + private static class TestSerialize { + public String type = "hello"; + } + + private static class Left { + public Either f = Either.left(new TestSerialize()); + } + + private static class Right { + public Either f = Either.right(new TestSerialize()); + } } diff --git a/src/test/java/io/vavr/jackson/datatype/FunctionTest.java b/src/test/java/io/vavr/jackson/datatype/FunctionTest.java index 5acf351..d9221eb 100644 --- a/src/test/java/io/vavr/jackson/datatype/FunctionTest.java +++ b/src/test/java/io/vavr/jackson/datatype/FunctionTest.java @@ -1,19 +1,35 @@ package io.vavr.jackson.datatype; -import static org.junit.jupiter.api.Assertions.assertThrows; - import com.fasterxml.jackson.core.type.TypeReference; - -import io.vavr.*; -import org.junit.jupiter.api.Assertions; +import io.vavr.CheckedFunction0; +import io.vavr.CheckedFunction1; +import io.vavr.CheckedFunction2; +import io.vavr.CheckedFunction3; +import io.vavr.CheckedFunction4; +import io.vavr.CheckedFunction5; +import io.vavr.CheckedFunction6; +import io.vavr.CheckedFunction7; +import io.vavr.CheckedFunction8; +import io.vavr.Function0; +import io.vavr.Function1; +import io.vavr.Function2; +import io.vavr.Function3; +import io.vavr.Function4; +import io.vavr.Function5; +import io.vavr.Function6; +import io.vavr.Function7; +import io.vavr.Function8; import org.junit.jupiter.api.Test; import java.io.IOException; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + class FunctionTest extends BaseTest { @Test - void testError() { + void shouldThrowExceptionForInvalidFunctionSerialization() { assertThrows(IllegalStateException.class, () -> { Function1 src = i1 -> i1 + 42; String json = mapper().writer().writeValueAsString(src); @@ -23,147 +39,146 @@ void testError() { } @Test - void testF0() throws IOException { + void shouldSerializeAndDeserializeFunction0() throws IOException { Function0 src = () -> "42"; String json = mapper().writer().writeValueAsString(src); Function0 res = mapper().readValue(json, new TypeReference>() {}); - Assertions.assertEquals(res.apply(), "42"); + assertEquals(res.apply(), "42"); } @Test - void testF1() throws IOException { + void shouldSerializeAndDeserializeFunction1() throws IOException { Function1 src = i1 -> i1 + 42; String json = mapper().writer().writeValueAsString(src); Function1 res = mapper().readValue(json, new TypeReference>() {}); - Assertions.assertEquals(res.apply("1/"), "1/42"); + assertEquals(res.apply("1/"), "1/42"); } @Test - void testF2() throws IOException { + void shouldSerializeAndDeserializeFunction2() throws IOException { Function2 src = (i1, i2) -> i1 + i2 + 42; String json = mapper().writer().writeValueAsString(src); Function2 res = mapper().readValue(json, new TypeReference>() {}); - Assertions.assertEquals(res.apply("1/", "2/"), "1/2/42"); + assertEquals(res.apply("1/", "2/"), "1/2/42"); } @Test - void testF3() throws IOException { + void shouldSerializeAndDeserializeFunction3() throws IOException { Function3 src = (i1, i2, i3) -> i1 + i2 + i3 + 42; String json = mapper().writer().writeValueAsString(src); Function3 res = mapper().readValue(json, new TypeReference>() {}); - Assertions.assertEquals(res.apply("1/", "2/", "3/"), "1/2/3/42"); + assertEquals(res.apply("1/", "2/", "3/"), "1/2/3/42"); } @Test - void testF4() throws IOException { + void shouldSerializeAndDeserializeFunction4() throws IOException { Function4 src = (i1, i2, i3, i4) -> i1 + i2 + i3 + i4 + 42; String json = mapper().writer().writeValueAsString(src); Function4 res = mapper().readValue(json, new TypeReference>() {}); - Assertions.assertEquals(res.apply("1/", "2/", "3/", "4/"), "1/2/3/4/42"); + assertEquals(res.apply("1/", "2/", "3/", "4/"), "1/2/3/4/42"); } @Test - void testF5() throws IOException { + void shouldSerializeAndDeserializeFunction5() throws IOException { Function5 src = (i1, i2, i3, i4, i5) -> i1 + i2 + i3 + i4 + i5 + 42; String json = mapper().writer().writeValueAsString(src); Function5 res = mapper().readValue(json, new TypeReference>() {}); - Assertions.assertEquals(res.apply("1/", "2/", "3/", "4/", "5/"), "1/2/3/4/5/42"); + assertEquals(res.apply("1/", "2/", "3/", "4/", "5/"), "1/2/3/4/5/42"); } @Test - void testF6() throws IOException { + void shouldSerializeAndDeserializeFunction6() throws IOException { Function6 src = (i1, i2, i3, i4, i5, i6) -> i1 + i2 + i3 + i4 + i5 + i6 + 42; String json = mapper().writer().writeValueAsString(src); Function6 res = mapper().readValue(json, new TypeReference>() {}); - Assertions.assertEquals(res.apply("1/", "2/", "3/", "4/", "5/", "6/"), "1/2/3/4/5/6/42"); + assertEquals(res.apply("1/", "2/", "3/", "4/", "5/", "6/"), "1/2/3/4/5/6/42"); } @Test - void testF7() throws IOException { + void shouldSerializeAndDeserializeFunction7() throws IOException { Function7 src = (i1, i2, i3, i4, i5, i6, i7) -> i1 + i2 + i3 + i4 + i5 + i6 + i7 + 42; String json = mapper().writer().writeValueAsString(src); Function7 res = mapper().readValue(json, new TypeReference>() {}); - Assertions.assertEquals(res.apply("1/", "2/", "3/", "4/", "5/", "6/", "7/"), "1/2/3/4/5/6/7/42"); + assertEquals(res.apply("1/", "2/", "3/", "4/", "5/", "6/", "7/"), "1/2/3/4/5/6/7/42"); } @Test - void testF8() throws IOException { + void shouldSerializeAndDeserializeFunction8() throws IOException { Function8 src = (i1, i2, i3, i4, i5, i6, i7, i8) -> i1 + i2 + i3 + i4 + i5 + i6 + i7 + i8 + 42; String json = mapper().writer().writeValueAsString(src); Function8 res = mapper().readValue(json, new TypeReference>() {}); - Assertions.assertEquals(res.apply("1/", "2/", "3/", "4/", "5/", "6/", "7/", "8/"), "1/2/3/4/5/6/7/8/42"); + assertEquals(res.apply("1/", "2/", "3/", "4/", "5/", "6/", "7/", "8/"), "1/2/3/4/5/6/7/8/42"); } @Test - void testC0() throws Throwable { + void shouldSerializeAndDeserializeCheckedFunction0() throws Throwable { CheckedFunction0 src = () -> "42"; String json = mapper().writer().writeValueAsString(src); CheckedFunction0 res = mapper().readValue(json, new TypeReference>() {}); - Assertions.assertEquals(res.apply(), "42"); + assertEquals(res.apply(), "42"); } @Test - void testC1() throws Throwable { + void shouldSerializeAndDeserializeCheckedFunction1() throws Throwable { CheckedFunction1 src = i1 -> i1 + 42; String json = mapper().writer().writeValueAsString(src); CheckedFunction1 res = mapper().readValue(json, new TypeReference>() {}); - Assertions.assertEquals(res.apply("1/"), "1/42"); + assertEquals(res.apply("1/"), "1/42"); } @Test - void testC2() throws Throwable { + void shouldSerializeAndDeserializeCheckedFunction2() throws Throwable { CheckedFunction2 src = (i1, i2) -> i1 + i2 + 42; String json = mapper().writer().writeValueAsString(src); CheckedFunction2 res = mapper().readValue(json, new TypeReference>() {}); - Assertions.assertEquals(res.apply("1/", "2/"), "1/2/42"); + assertEquals(res.apply("1/", "2/"), "1/2/42"); } @Test - void testC3() throws Throwable { + void shouldSerializeAndDeserializeCheckedFunction3() throws Throwable { CheckedFunction3 src = (i1, i2, i3) -> i1 + i2 + i3 + 42; String json = mapper().writer().writeValueAsString(src); CheckedFunction3 res = mapper().readValue(json, new TypeReference>() {}); - Assertions.assertEquals(res.apply("1/", "2/", "3/"), "1/2/3/42"); + assertEquals(res.apply("1/", "2/", "3/"), "1/2/3/42"); } @Test - void testC4() throws Throwable { + void shouldSerializeAndDeserializeCheckedFunction4() throws Throwable { CheckedFunction4 src = (i1, i2, i3, i4) -> i1 + i2 + i3 + i4 + 42; String json = mapper().writer().writeValueAsString(src); CheckedFunction4 res = mapper().readValue(json, new TypeReference>() {}); - Assertions.assertEquals(res.apply("1/", "2/", "3/", "4/"), "1/2/3/4/42"); + assertEquals(res.apply("1/", "2/", "3/", "4/"), "1/2/3/4/42"); } @Test - void testC5() throws Throwable { + void shouldSerializeAndDeserializeCheckedFunction5() throws Throwable { CheckedFunction5 src = (i1, i2, i3, i4, i5) -> i1 + i2 + i3 + i4 + i5 + 42; String json = mapper().writer().writeValueAsString(src); CheckedFunction5 res = mapper().readValue(json, new TypeReference>() {}); - Assertions.assertEquals(res.apply("1/", "2/", "3/", "4/", "5/"), "1/2/3/4/5/42"); + assertEquals(res.apply("1/", "2/", "3/", "4/", "5/"), "1/2/3/4/5/42"); } @Test - void testC6() throws Throwable { + void shouldSerializeAndDeserializeCheckedFunction6() throws Throwable { CheckedFunction6 src = (i1, i2, i3, i4, i5, i6) -> i1 + i2 + i3 + i4 + i5 + i6 + 42; String json = mapper().writer().writeValueAsString(src); CheckedFunction6 res = mapper().readValue(json, new TypeReference>() {}); - Assertions.assertEquals(res.apply("1/", "2/", "3/", "4/", "5/", "6/"), "1/2/3/4/5/6/42"); + assertEquals(res.apply("1/", "2/", "3/", "4/", "5/", "6/"), "1/2/3/4/5/6/42"); } @Test - void testC7() throws Throwable { + void shouldSerializeAndDeserializeCheckedFunction7() throws Throwable { CheckedFunction7 src = (i1, i2, i3, i4, i5, i6, i7) -> i1 + i2 + i3 + i4 + i5 + i6 + i7 + 42; String json = mapper().writer().writeValueAsString(src); CheckedFunction7 res = mapper().readValue(json, new TypeReference>() {}); - Assertions.assertEquals(res.apply("1/", "2/", "3/", "4/", "5/", "6/", "7/"), "1/2/3/4/5/6/7/42"); + assertEquals(res.apply("1/", "2/", "3/", "4/", "5/", "6/", "7/"), "1/2/3/4/5/6/7/42"); } @Test - void testC8() throws Throwable { + void shouldSerializeAndDeserializeCheckedFunction8() throws Throwable { CheckedFunction8 src = (i1, i2, i3, i4, i5, i6, i7, i8) -> i1 + i2 + i3 + i4 + i5 + i6 + i7 + i8 + 42; String json = mapper().writer().writeValueAsString(src); CheckedFunction8 res = mapper().readValue(json, new TypeReference>() {}); - Assertions.assertEquals(res.apply("1/", "2/", "3/", "4/", "5/", "6/", "7/", "8/"), "1/2/3/4/5/6/7/8/42"); + assertEquals(res.apply("1/", "2/", "3/", "4/", "5/", "6/", "7/", "8/"), "1/2/3/4/5/6/7/8/42"); } - } diff --git a/src/test/java/io/vavr/jackson/datatype/LazyTest.java b/src/test/java/io/vavr/jackson/datatype/LazyTest.java index 21f3f52..7d34307 100644 --- a/src/test/java/io/vavr/jackson/datatype/LazyTest.java +++ b/src/test/java/io/vavr/jackson/datatype/LazyTest.java @@ -16,8 +16,9 @@ import static org.junit.jupiter.api.Assertions.assertEquals; public class LazyTest extends BaseTest { + @Test - void test1() throws IOException { + void shouldSerializeAndDeserializeLazyWithValue() throws IOException { Lazy src = Lazy.of(() -> 1); String json = mapper().writer().writeValueAsString(src); assertEquals("1", json); @@ -26,7 +27,7 @@ void test1() throws IOException { } @Test - void test2() throws IOException { + void shouldSerializeAndDeserializeLazyWithNullValue() throws IOException { Lazy src = Lazy.of(() -> null); String json = mapper().writer().writeValueAsString(src); assertEquals("null", json); @@ -35,17 +36,16 @@ void test2() throws IOException { } @Test - void testTwoLevelLazy() throws IOException { + void shouldSerializeAndDeserializeTwoLevelLazy() throws IOException { Lazy> src = Lazy.of(() -> Lazy.of(() -> 1)); String json = mapper().writeValueAsString(src); assertEquals("1", json); - Lazy restored = mapper().readValue(json, new TypeReference>>() { - }); + Lazy restored = mapper().readValue(json, new TypeReference>>() {}); assertEquals(src, restored); } @Test - void testSerializeWithoutContext() throws IOException { + void shouldSerializeAndDeserializeLazyWithLocalDateWithoutContext() throws IOException { // Given a lazy date Lazy src = Lazy.of(() -> LocalDate.of(2019, 12, 25)); @@ -58,11 +58,11 @@ void testSerializeWithoutContext() throws IOException { assertEquals("[2019,12,25]", json); // And the deserialization is successful - Lazy src2 = mapper.readValue(json, new TypeReference>() { - }); + Lazy src2 = mapper.readValue(json, new TypeReference>() {}); assertEquals(src, src2); } + static class FrenchDate { @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd/MM/yyyy") @JsonProperty("date") diff --git a/src/test/java/io/vavr/jackson/datatype/MixedTest.java b/src/test/java/io/vavr/jackson/datatype/MixedTest.java index 060dd8d..0bbaa6e 100644 --- a/src/test/java/io/vavr/jackson/datatype/MixedTest.java +++ b/src/test/java/io/vavr/jackson/datatype/MixedTest.java @@ -10,57 +10,59 @@ import java.io.IOException; import java.util.Arrays; +import static org.junit.jupiter.api.Assertions.assertEquals; + class MixedTest extends BaseTest { @Test - void test1() throws IOException { + void shouldSerializeAndDeserializeVavrHashMapWithListValues() throws IOException { Object src = HashMap.empty().put("key1", List.of(1, 2)).put("key2", List.of(3, 4)); String json = mapper().writer().writeValueAsString(src); - Assertions.assertEquals(mapper().readValue(json, new TypeReference>>() {}), src); + assertEquals(mapper().readValue(json, new TypeReference>>() {}), src); } @Test - void test2() throws IOException { + void shouldSerializeAndDeserializeListOfVavrHashMaps() throws IOException { Object src = List.of(HashMap.empty().put("key1", 1), HashMap.empty().put("key2", 2)); String json = mapper().writer().writeValueAsString(src); - Assertions.assertEquals(mapper().readValue(json, new TypeReference>>() {}), src); + assertEquals(mapper().readValue(json, new TypeReference>>() {}), src); } @Test - void test3() throws IOException { + void shouldSerializeAndDeserializeListContainingJavaMap() throws IOException { java.util.Map javaHMap = new java.util.HashMap<>(); javaHMap.put("1", "2"); List src = List.of(javaHMap); List restored = mapper().readValue(mapper().writer().writeValueAsString(src), List.class); - Assertions.assertEquals(restored, src); + assertEquals(restored, src); } @Test - void test4() { + void shouldThrowJsonMappingExceptionForInvalidListElements() { Assertions.assertThrows(JsonMappingException.class, () -> { mapper().readValue("[\"s\"]", new TypeReference>() {}); }); } @Test - void test5() throws IOException { + void shouldSerializeAndDeserializeNestedVavrLists() throws IOException { Object src = List.of(List.of(1)); String json = mapper().writer().writeValueAsString(src); - Assertions.assertEquals(mapper().readValue(json, new TypeReference>>() {}), src); + assertEquals(mapper().readValue(json, new TypeReference>>() {}), src); } @Test - void test6() throws IOException { + void shouldSerializeAndDeserializeVavrHashMapWithNestedHashMapValues() throws IOException { Object src = HashMap.empty().put("1", HashMap.empty().put("X", "Y")); String json = mapper().writer().writeValueAsString(src); - Assertions.assertEquals(mapper().readValue(json, new TypeReference>>() {}), src); + assertEquals(mapper().readValue(json, new TypeReference>>() {}), src); } @Test - void test7() throws IOException { + void shouldSerializeAndDeserializeListContainingJavaList() throws IOException { Object src = List.of(Arrays.asList(1, 2)); String json = mapper().writer().writeValueAsString(src); - Assertions.assertEquals(mapper().readValue(json, List.class), src); - Assertions.assertEquals(mapper().readValue(json, new TypeReference>>() {}), src); + assertEquals(mapper().readValue(json, List.class), src); + assertEquals(mapper().readValue(json, new TypeReference>>() {}), src); } } diff --git a/src/test/java/io/vavr/jackson/datatype/OptionPlainTest.java b/src/test/java/io/vavr/jackson/datatype/OptionPlainTest.java index db295df..62ea3d1 100644 --- a/src/test/java/io/vavr/jackson/datatype/OptionPlainTest.java +++ b/src/test/java/io/vavr/jackson/datatype/OptionPlainTest.java @@ -1,28 +1,29 @@ package io.vavr.jackson.datatype; import io.vavr.control.Option; -import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import java.io.IOException; +import static org.junit.jupiter.api.Assertions.assertEquals; + class OptionPlainTest extends BaseTest { @Test - void test1() throws IOException { + void shouldSerializeAndDeserializeDefinedOptionPlainValue() throws IOException { Option src = Option.of(1); String json = mapper().writer().writeValueAsString(src); - Assertions.assertEquals("1", json); + assertEquals("1", json); Option restored = mapper().readValue(json, Option.class); - Assertions.assertEquals(src, restored); + assertEquals(src, restored); } @Test - void test2() throws IOException { + void shouldSerializeAndDeserializeNoneOptionPlainValue() throws IOException { Option src = Option.none(); String json = mapper().writer().writeValueAsString(src); - Assertions.assertEquals("null", json); + assertEquals("null", json); Option restored = mapper().readValue(json, Option.class); - Assertions.assertEquals(src, restored); + assertEquals(src, restored); } } diff --git a/src/test/java/io/vavr/jackson/datatype/OptionTest.java b/src/test/java/io/vavr/jackson/datatype/OptionTest.java index fa3bf78..e360938 100644 --- a/src/test/java/io/vavr/jackson/datatype/OptionTest.java +++ b/src/test/java/io/vavr/jackson/datatype/OptionTest.java @@ -1,7 +1,5 @@ package io.vavr.jackson.datatype; -import static org.junit.jupiter.api.Assertions.assertThrows; - import com.fasterxml.jackson.annotation.JsonTypeInfo; import com.fasterxml.jackson.annotation.JsonTypeName; import com.fasterxml.jackson.databind.JsonMappingException; @@ -12,42 +10,44 @@ import java.io.IOException; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + class OptionTest extends BaseTest { - private static VavrModule.Settings optSettings = - new VavrModule.Settings().useOptionInPlainFormat(false); + private static VavrModule.Settings optSettings = new VavrModule.Settings().useOptionInPlainFormat(false); @Test - void test1() throws IOException { + void shouldSerializeAndDeserializeDefinedOptionWithValue() throws IOException { Option src = Option.of(1); String json = mapper(optSettings).writer().writeValueAsString(src); - Assertions.assertEquals("[\"defined\",1]", json); + assertEquals("[\"defined\",1]", json); Option restored = mapper(optSettings).readValue(json, Option.class); - Assertions.assertEquals(src, restored); + assertEquals(src, restored); } @Test - void test1null() throws IOException { + void shouldSerializeAndDeserializeDefinedOptionWithNull() throws IOException { Option src = Option.some(null); String json = mapper(optSettings).writer().writeValueAsString(src); - Assertions.assertEquals("[\"defined\",null]", json); + assertEquals("[\"defined\",null]", json); Option restored = mapper(optSettings).readValue(json, Option.class); - Assertions.assertEquals(src, restored); + assertEquals(src, restored); } @Test - void test2() throws IOException { + void shouldSerializeAndDeserializeNoneOption() throws IOException { Option src = Option.none(); String json = mapper(optSettings).writer().writeValueAsString(src); - Assertions.assertEquals("[\"undefined\"]", json); + assertEquals("[\"undefined\"]", json); Option restored = mapper(optSettings).readValue(json, Option.class); - Assertions.assertEquals(src, restored); + assertEquals(src, restored); Option plain = mapper(optSettings).readValue("null", Option.class); - Assertions.assertEquals(src, plain); + assertEquals(src, plain); } @Test - void test3() throws IOException { + void shouldThrowExceptionForInvalidOptionWithExtraValue() { assertThrows(JsonMappingException.class, () -> { String json = "[\"defined\", 2, 3]"; mapper(optSettings).readValue(json, Option.class); @@ -55,7 +55,7 @@ void test3() throws IOException { } @Test - void test4() throws IOException { + void shouldThrowExceptionForDefinedOptionWithoutValue() { assertThrows(JsonMappingException.class, () -> { String json = "[\"defined\"]"; mapper(optSettings).readValue(json, Option.class); @@ -63,7 +63,7 @@ void test4() throws IOException { } @Test - void test5() { + void shouldThrowExceptionForEmptyArrayOption() { assertThrows(JsonMappingException.class, () -> { String json = "[]"; mapper(optSettings).readValue(json, Option.class); @@ -71,7 +71,7 @@ void test5() { } @Test - void test6() { + void shouldThrowExceptionForUndefinedOptionWithExtraValues() { assertThrows(JsonMappingException.class, () -> { String json = "[\"undefined\", 2, 3]"; mapper(optSettings).readValue(json, Option.class); @@ -79,7 +79,7 @@ void test6() { } @Test - void test7() { + void shouldThrowExceptionForUnrecognizedOptionType() { assertThrows(JsonMappingException.class, () -> { String json = "[\"test\"]"; mapper(optSettings).readValue(json, Option.class); @@ -87,16 +87,16 @@ void test7() { } @Test - void test8() throws IOException { + void shouldDeserializeNullToEmptyOption() throws IOException { String json = "null"; Option option = mapper(optSettings).readValue(json, Option.class); Assertions.assertTrue(option.isEmpty()); } @JsonTypeInfo( - use = JsonTypeInfo.Id.NAME, - include = JsonTypeInfo.As.WRAPPER_OBJECT, - property = "type") + use = JsonTypeInfo.Id.NAME, + include = JsonTypeInfo.As.WRAPPER_OBJECT, + property = "type") @JsonTypeName("card") private static class TestSerialize { public String type = "hello"; @@ -107,19 +107,19 @@ private static class A { } @Test - void testJsonTypeInfo1() throws IOException { + void shouldSerializeAndDeserializeWithDefaultJsonTypeInfo() throws IOException { String javaUtilValue = mapper().writeValueAsString(new A()); - Assertions.assertEquals("{\"f\":{\"card\":{\"type\":\"hello\"}}}", javaUtilValue); + assertEquals("{\"f\":{\"card\":{\"type\":\"hello\"}}}", javaUtilValue); A restored = mapper().readValue(javaUtilValue, A.class); - Assertions.assertEquals("hello", restored.f.get().type); + assertEquals("hello", restored.f.get().type); } @Test - void testJsonTypeInfo2() throws IOException { + void shouldSerializeAndDeserializeWithOptionEnabledJsonTypeInfo() throws IOException { ObjectMapper mapper = mapper(optSettings); String javaUtilValue = mapper.writeValueAsString(new A()); - Assertions.assertEquals("{\"f\":[\"defined\",{\"card\":{\"type\":\"hello\"}}]}", javaUtilValue); + assertEquals("{\"f\":[\"defined\",{\"card\":{\"type\":\"hello\"}}]}", javaUtilValue); A restored = mapper.readValue(javaUtilValue, A.class); - Assertions.assertEquals("hello", restored.f.get().type); + assertEquals("hello", restored.f.get().type); } } diff --git a/src/test/java/io/vavr/jackson/datatype/PriorityQueueTest.java b/src/test/java/io/vavr/jackson/datatype/PriorityQueueTest.java index 35512a5..97d9930 100644 --- a/src/test/java/io/vavr/jackson/datatype/PriorityQueueTest.java +++ b/src/test/java/io/vavr/jackson/datatype/PriorityQueueTest.java @@ -10,7 +10,6 @@ import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement; import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; import io.vavr.collection.PriorityQueue; -import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import javax.xml.bind.annotation.XmlElement; @@ -20,43 +19,38 @@ import java.util.Arrays; import java.util.Date; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; public class PriorityQueueTest extends BaseTest { @Test - void testGeneric1() { + void shouldThrowExceptionForGenericDeserializationWithIntegers() { assertThrows(JsonMappingException.class, () -> mapper().readValue("[1, 2]", PriorityQueue.class)); } @Test - void testGeneric2() throws IOException { + void shouldThrowExceptionForGenericDeserializationWithTypeReference() throws IOException { assertThrows(JsonMappingException.class, () -> { mapper().readValue("[1, 2]", new TypeReference>() {}); }); } - static class Incomparable { - private int i = 0; - int getI() { return i; } - void setI(int i) { this.i = i; } - } - @Test - void testGeneric3() { + void shouldThrowExceptionForIncomparableDeserialization() { assertThrows(JsonMappingException.class, () -> { mapper().readValue("[{\"i\":1}, {\"i\":2}]", new TypeReference>() {}); }); } @Test - void test1() throws IOException { + void shouldSerializeAndDeserializePriorityQueueOfIntegers() throws IOException { ObjectWriter writer = mapper().writer(); PriorityQueue src = PriorityQueue.of(1, 5, 8); String json = writer.writeValueAsString(src); - Assertions.assertEquals(genJsonList(1, 5, 8), json); + assertEquals(genJsonList(1, 5, 8), json); PriorityQueue dst = mapper().readValue(json, new TypeReference>() {}); - Assertions.assertEquals(src, dst); + assertEquals(src, dst); } @Test @@ -67,6 +61,12 @@ void testSerializable() throws IOException { checkSerialization(restored); } + static class Incomparable { + private int i = 0; + int getI() { return i; } + void setI(int i) { this.i = i; } + } + @XmlRootElement(name = "xmlSerialize") private static class JaxbXmlSerializeVavr { @XmlElementWrapper(name = "transitTypes") @@ -96,9 +96,9 @@ public JaxbXmlSerializeJavaUtil init(java.util.Collection javaSet) { void testJaxbXmlSerialization() throws IOException { ObjectMapper mapper = xmlMapperJaxb(); String javaUtilValue = mapper.writeValueAsString(new JaxbXmlSerializeVavr().init(PriorityQueue.of(1, 2, 3))); - Assertions.assertEquals(mapper.writeValueAsString(new JaxbXmlSerializeJavaUtil().init(Arrays.asList(1, 2, 3))), javaUtilValue); + assertEquals(mapper.writeValueAsString(new JaxbXmlSerializeJavaUtil().init(Arrays.asList(1, 2, 3))), javaUtilValue); JaxbXmlSerializeVavr restored = mapper.readValue(javaUtilValue, JaxbXmlSerializeVavr.class); - Assertions.assertEquals(restored.transitTypes.size(), 3); + assertEquals(restored.transitTypes.size(), 3); } @JacksonXmlRootElement(localName = "xmlSerialize") @@ -130,9 +130,9 @@ public XmlSerializeJavaUtil init(java.util.Collection javaSet) { void testXmlSerialization() throws IOException { ObjectMapper mapper = xmlMapper(); String javaUtilValue = mapper.writeValueAsString(new XmlSerializeVavr().init(PriorityQueue.of(1, 2, 3))); - Assertions.assertEquals(mapper.writeValueAsString(new XmlSerializeJavaUtil().init(Arrays.asList(1, 2, 3))), javaUtilValue); + assertEquals(mapper.writeValueAsString(new XmlSerializeJavaUtil().init(Arrays.asList(1, 2, 3))), javaUtilValue); XmlSerializeVavr restored = mapper.readValue(javaUtilValue, XmlSerializeVavr.class); - Assertions.assertEquals(restored.transitTypes.size(), 3); + assertEquals(restored.transitTypes.size(), 3); } static class FrenchDates { @@ -152,10 +152,10 @@ void testSerializeWithContext() throws IOException { String json = mapper.writeValueAsString(src); // Then the serialization is successful - Assertions.assertEquals("{\"dates\":[\"05/06/2020\"]}", json); + assertEquals("{\"dates\":[\"05/06/2020\"]}", json); // And the deserialization is successful FrenchDates restored = mapper.readValue(json, FrenchDates.class); - Assertions.assertEquals(src.dates, restored.dates); + assertEquals(src.dates, restored.dates); } } diff --git a/src/test/java/io/vavr/jackson/datatype/ScalarTest.java b/src/test/java/io/vavr/jackson/datatype/ScalarTest.java index 48eb1b3..8d55fbc 100644 --- a/src/test/java/io/vavr/jackson/datatype/ScalarTest.java +++ b/src/test/java/io/vavr/jackson/datatype/ScalarTest.java @@ -3,46 +3,75 @@ import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.DeserializationFeature; import io.vavr.collection.List; -import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import java.io.IOException; import java.math.BigDecimal; import java.math.BigInteger; +import static org.junit.jupiter.api.Assertions.assertEquals; + public class ScalarTest extends BaseTest { @Test - void test1() throws IOException { + void shouldDeserializeIntegerList() throws IOException { List l1 = mapper().readValue("[1]", new TypeReference>() {}); - Assertions.assertEquals(l1, List.of(1)); + assertEquals(l1, List.of(1)); + } + + @Test + void shouldDeserializeBooleanList() throws IOException { List l2 = mapper().readValue("[true]", new TypeReference>() {}); - Assertions.assertEquals(l2, List.of(true)); + assertEquals(l2, List.of(true)); + List l3 = mapper().readValue("[false]", new TypeReference>() {}); - Assertions.assertEquals(l3, List.of(false)); + assertEquals(l3, List.of(false)); + } + + @Test + void shouldDeserializeFloatList() throws IOException { List l4 = mapper().readValue("[2.4]", new TypeReference>() {}); - Assertions.assertEquals(l4, List.of(2.4f)); + assertEquals(l4, List.of(2.4f)); + } + + @Test + void shouldDeserializeDoubleAndBigDecimalList() throws IOException { List l5 = mapper().readValue("[2.4]", new TypeReference>() {}); - Assertions.assertEquals(l5, List.of(2.4)); - List l5d = mapper().enable(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS).readValue("[2.4]", new TypeReference>() {}); - Assertions.assertEquals(l5d, List.of(BigDecimal.valueOf(2.4))); + assertEquals(l5, List.of(2.4)); + + List l5d = mapper().enable(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS) + .readValue("[2.4]", new TypeReference>() {}); + assertEquals(l5d, List.of(BigDecimal.valueOf(2.4))); + } + + @Test + void shouldDeserializeStringList() throws IOException { List l6 = mapper().readValue("[\"1\"]", new TypeReference>() {}); - Assertions.assertEquals(l6, List.of("1")); + assertEquals(l6, List.of("1")); + } + + @Test + void shouldDeserializeLongList() throws IOException { List l7 = mapper().readValue("[24]", new TypeReference>() {}); - Assertions.assertEquals(l7, List.of(24L)); + assertEquals(l7, List.of(24L)); + List l8 = mapper().readValue("[" + Long.MAX_VALUE + "]", new TypeReference>() {}); - Assertions.assertEquals(l8, List.of(Long.MAX_VALUE)); + assertEquals(l8, List.of(Long.MAX_VALUE)); + } + + @Test + void shouldDeserializeBigIntegerList() throws IOException { List l9 = mapper().readValue("[1234567890123456789012]", new TypeReference>() {}); - Assertions.assertEquals(l9, List.of(new BigInteger("1234567890123456789012"))); + assertEquals(l9, List.of(new BigInteger("1234567890123456789012"))); } @Test - void test2() throws IOException { - Assertions.assertEquals(mapper().readValue("[1.3]", new TypeReference>() {}), List.of(1)); + void shouldDeserializeFloatToIntegerList() throws IOException { + assertEquals(mapper().readValue("[1.3]", new TypeReference>() {}), List.of(1)); } @Test - void test3() throws IOException { - Assertions.assertEquals(mapper().readValue("[1]", new TypeReference>() {}), List.of("1")); + void shouldDeserializeIntegerToStringList() throws IOException { + assertEquals(mapper().readValue("[1]", new TypeReference>() {}), List.of("1")); } } diff --git a/src/test/java/io/vavr/jackson/datatype/SerializationFeatureTest.java b/src/test/java/io/vavr/jackson/datatype/SerializationFeatureTest.java index 14aed11..1ca800c 100644 --- a/src/test/java/io/vavr/jackson/datatype/SerializationFeatureTest.java +++ b/src/test/java/io/vavr/jackson/datatype/SerializationFeatureTest.java @@ -8,44 +8,35 @@ import io.vavr.collection.List; import org.joda.time.DateTime; import org.joda.time.DateTimeZone; -import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import java.io.IOException; import java.util.ArrayList; -public class SerializationFeatureTest { +import static org.junit.jupiter.api.Assertions.assertEquals; - private static CsvMapper getMapper() { - final CsvMapper csvMapper = new CsvMapper(); - csvMapper.registerModule(new JodaModule()); - csvMapper.registerModule(new VavrModule()); - return csvMapper; - } +public class SerializationFeatureTest { @Test - public void vavr_List_DateTime_serialization_should_use_SerializationFeature() throws IOException { - final DateTime dateTime = new DateTime(2016, 6, 6, 8, 0, DateTimeZone.forID("CET")); - final io.vavr.collection.List dateTimeList = List.of(dateTime); - final java.util.List dateTimeJavaList = new ArrayList<>(); + public void shouldApplySerializationFeatureListDateTime() throws IOException { + DateTime dateTime = new DateTime(2016, 6, 6, 8, 0, DateTimeZone.forID("CET")); + io.vavr.collection.List dateTimeList = List.of(dateTime); + java.util.List dateTimeJavaList = new ArrayList<>(); dateTimeJavaList.add(dateTime); - final CsvMapper mapper = getMapper(); - final ObjectWriter writer = mapper.writer() - .without(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); + CsvMapper mapper = new CsvMapper(); + mapper.registerModule(new JodaModule()); + mapper.registerModule(new VavrModule()); + ObjectWriter writer = mapper.writer().without(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); - final String serializedDateTime = writer - .writeValueAsString(dateTime); - final String serializedDateTimeJavaList = writer - .writeValueAsString(dateTimeJavaList); - final String serializedDateTimeList = writer - .writeValueAsString(dateTimeList); + String serializedDateTime = writer.writeValueAsString(dateTime); + String serializedDateTimeJavaList = writer.writeValueAsString(dateTimeJavaList); + String serializedDateTimeList = writer.writeValueAsString(dateTimeList); - Assertions.assertEquals(serializedDateTime, serializedDateTimeJavaList); - Assertions.assertEquals(serializedDateTimeJavaList, serializedDateTimeList); + assertEquals(serializedDateTime, serializedDateTimeJavaList); + assertEquals(serializedDateTimeJavaList, serializedDateTimeList); List restored = mapper.readValue(serializedDateTimeList, new TypeReference>() {}); - Assertions.assertEquals(restored.head().getMillis(), dateTime.getMillis()); - + assertEquals(restored.head().getMillis(), dateTime.getMillis()); } } diff --git a/src/test/java/io/vavr/jackson/datatype/ServiceLoaderTest.java b/src/test/java/io/vavr/jackson/datatype/ServiceLoaderTest.java index d6514d4..94572a7 100644 --- a/src/test/java/io/vavr/jackson/datatype/ServiceLoaderTest.java +++ b/src/test/java/io/vavr/jackson/datatype/ServiceLoaderTest.java @@ -1,6 +1,5 @@ package io.vavr.jackson.datatype; - import com.fasterxml.jackson.databind.ObjectMapper; import io.vavr.Lazy; import org.junit.jupiter.api.Assertions; @@ -8,20 +7,21 @@ public class ServiceLoaderTest { - /** - * Since there is no way (without reflection) to interrogate the mapper to see which modules - * are loaded, we can do so indirectly by simply asking it to handle any vavr type. - * Non-failure means success - * @throws Exception only if jackson fails to handle vavr types - meaning auto-register didn't work - */ - @Test - void testAutoDiscovery() throws Exception { - ObjectMapper mapper = new ObjectMapper().findAndRegisterModules(); + /** + * Since there is no way (without reflection) to interrogate the mapper to see which modules + * are loaded, we can do so indirectly by simply asking it to handle any vavr type. + * Non-failure means success + * + * @throws Exception only if jackson fails to handle vavr types - meaning auto-register didn't work + */ + @Test + void shouldAutoDiscover() throws Exception { + ObjectMapper mapper = new ObjectMapper().findAndRegisterModules(); - Lazy src = Lazy.of(() -> 1); - String json = mapper.writer().writeValueAsString(src); - Assertions.assertEquals("1", json); - Lazy restored = mapper.readValue(json, Lazy.class); - Assertions.assertEquals(src, restored); - } + Lazy src = Lazy.of(() -> 1); + String json = mapper.writer().writeValueAsString(src); + Assertions.assertEquals("1", json); + Lazy restored = mapper.readValue(json, Lazy.class); + Assertions.assertEquals(src, restored); + } }