|
12 | 12 | import org.baeldung.jackson.dtos.MyDtoFieldNameChanged; |
13 | 13 | import org.baeldung.jackson.dtos.MyDtoNoAccessors; |
14 | 14 | import org.baeldung.jackson.dtos.MyDtoNoAccessorsAndFieldVisibility; |
| 15 | +import org.baeldung.jackson.dtos.MyDtoWithEnum; |
| 16 | +import org.baeldung.jackson.dtos.MyDtoWithEnumCustom; |
| 17 | +import org.baeldung.jackson.dtos.TypeEnum; |
| 18 | +import org.baeldung.jackson.dtos.TypeEnumWithCustomSerializer; |
15 | 19 | import org.junit.Test; |
16 | 20 |
|
17 | 21 | import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility; |
|
21 | 25 | import com.fasterxml.jackson.databind.ObjectMapper; |
22 | 26 | import com.google.common.collect.Lists; |
23 | 27 |
|
24 | | - |
25 | 28 | public class JacksonSerializationUnitTest { |
26 | 29 |
|
27 | 30 | // tests - single entity to json |
@@ -77,6 +80,44 @@ public final void givenObjectHasNoAccessorsButHasVisibleFields_whenSerializing_t |
77 | 80 | assertThat(dtoAsString, containsString("booleanValue")); |
78 | 81 | } |
79 | 82 |
|
| 83 | + // tests - enums |
| 84 | + |
| 85 | + @Test |
| 86 | + public final void whenSerializingSimpleEnum_thenCorrect() throws JsonParseException, IOException { |
| 87 | + final ObjectMapper mapper = new ObjectMapper(); |
| 88 | + final String dtoAsString = mapper.writeValueAsString(TypeEnum.TYPE1); |
| 89 | + |
| 90 | + System.out.println(dtoAsString); |
| 91 | + assertThat(dtoAsString, containsString("\"name\":\"Type A\"")); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + public final void whenSerializingEntityWithEnum_thenCorrect() throws JsonParseException, IOException { |
| 96 | + final ObjectMapper mapper = new ObjectMapper(); |
| 97 | + final String dtoAsString = mapper.writeValueAsString(new MyDtoWithEnum("a", 1, true, TypeEnum.TYPE1)); |
| 98 | + |
| 99 | + System.out.println(dtoAsString); |
| 100 | + assertThat(dtoAsString, containsString("\"name\":\"Type A\"")); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + public final void givenCustomSerializer_whenSerializingEntityWithEnum_thenCorrect() throws JsonParseException, IOException { |
| 105 | + final ObjectMapper mapper = new ObjectMapper(); |
| 106 | + final String dtoAsString = mapper.writeValueAsString(new MyDtoWithEnumCustom("a", 1, true, TypeEnumWithCustomSerializer.TYPE1)); |
| 107 | + |
| 108 | + System.out.println(dtoAsString); |
| 109 | + assertThat(dtoAsString, containsString("\"name\":\"Type A\"")); |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + public final void whenSerializingArrayOfEnums_thenCorrect() throws JsonParseException, IOException { |
| 114 | + final ObjectMapper mapper = new ObjectMapper(); |
| 115 | + final String json = mapper.writeValueAsString(new TypeEnum[] { TypeEnum.TYPE1, TypeEnum.TYPE2 }); |
| 116 | + |
| 117 | + System.out.println(json); |
| 118 | + assertThat(json, containsString("\"name\":\"Type A\"")); |
| 119 | + } |
| 120 | + |
80 | 121 | // tests - multiple entities to json |
81 | 122 |
|
82 | 123 | @Test |
|
0 commit comments