1010import org .baeldung .jackson .dtos .withEnum .TypeEnum ;
1111import org .baeldung .jackson .dtos .withEnum .TypeEnumSimple ;
1212import org .baeldung .jackson .dtos .withEnum .TypeEnumWithCustomSerializer ;
13+ import org .baeldung .jackson .dtos .withEnum .TypeEnumWithValue ;
14+ import org .junit .Ignore ;
1315import org .junit .Test ;
1416
1517import com .fasterxml .jackson .core .JsonParseException ;
1618import com .fasterxml .jackson .databind .ObjectMapper ;
1719
1820public class JacksonSerializationEnumsUnitTest {
1921
20- // tests - enums
22+ // tests - simple enum
2123
2224 @ Test
2325 public final void whenSerializingASimpleEnum_thenCorrect () throws JsonParseException , IOException {
@@ -28,28 +30,33 @@ public final void whenSerializingASimpleEnum_thenCorrect() throws JsonParseExcep
2830 assertThat (dtoAsString , containsString ("TYPE1" ));
2931 }
3032
33+ // tests - enum with main value
34+
3135 @ Test
32- public final void whenSerializingAnEnum_thenCorrect () throws JsonParseException , IOException {
36+ @ Ignore ("https://github.com/FasterXML/jackson-databind/issues/47" )
37+ public final void whenSerializingAEnumWithValue_thenCorrect () throws JsonParseException , IOException {
3338 final ObjectMapper mapper = new ObjectMapper ();
34- final String dtoAsString = mapper .writeValueAsString (TypeEnum .TYPE1 .name ());
35-
39+ final String dtoAsString = mapper .writeValueAsString (TypeEnumWithValue .TYPE1 .name ());
3640 System .out .println (dtoAsString );
37- assertThat (dtoAsString , containsString ("\" name\" :\" Type A\" " ));
41+
42+ assertThat (dtoAsString , containsString ("Type A" ));
3843 }
3944
45+ // tests - enum
46+
4047 @ Test
41- public final void whenSerializingEntityWithEnum_thenCorrect () throws JsonParseException , IOException {
48+ public final void whenSerializingAnEnum_thenCorrect () throws JsonParseException , IOException {
4249 final ObjectMapper mapper = new ObjectMapper ();
43- final String dtoAsString = mapper .writeValueAsString (new MyDtoWithEnum ( "a" , 1 , true , TypeEnum .TYPE1 ));
50+ final String dtoAsString = mapper .writeValueAsString (TypeEnum .TYPE1 . name ( ));
4451
4552 System .out .println (dtoAsString );
4653 assertThat (dtoAsString , containsString ("\" name\" :\" Type A\" " ));
4754 }
4855
4956 @ Test
50- public final void givenCustomSerializer_whenSerializingEntityWithEnum_thenCorrect () throws JsonParseException , IOException {
57+ public final void whenSerializingEntityWithEnum_thenCorrect () throws JsonParseException , IOException {
5158 final ObjectMapper mapper = new ObjectMapper ();
52- final String dtoAsString = mapper .writeValueAsString (new MyDtoWithEnumCustom ("a" , 1 , true , TypeEnumWithCustomSerializer .TYPE1 ));
59+ final String dtoAsString = mapper .writeValueAsString (new MyDtoWithEnum ("a" , 1 , true , TypeEnum .TYPE1 ));
5360
5461 System .out .println (dtoAsString );
5562 assertThat (dtoAsString , containsString ("\" name\" :\" Type A\" " ));
@@ -64,4 +71,15 @@ public final void whenSerializingArrayOfEnums_thenCorrect() throws JsonParseExce
6471 assertThat (json , containsString ("\" name\" :\" Type A\" " ));
6572 }
6673
74+ // tests - enum with custom serializer
75+
76+ @ Test
77+ public final void givenCustomSerializer_whenSerializingEntityWithEnum_thenCorrect () throws JsonParseException , IOException {
78+ final ObjectMapper mapper = new ObjectMapper ();
79+ final String dtoAsString = mapper .writeValueAsString (new MyDtoWithEnumCustom ("a" , 1 , true , TypeEnumWithCustomSerializer .TYPE1 ));
80+
81+ System .out .println (dtoAsString );
82+ assertThat (dtoAsString , containsString ("\" name\" :\" Type A\" " ));
83+ }
84+
6785}
0 commit comments