1- package org .baeldung .jackson ;
1+ package org .baeldung .jackson . test ;
22
33import static org .hamcrest .Matchers .containsString ;
44import static org .hamcrest .Matchers .not ;
77import java .io .IOException ;
88import java .util .List ;
99
10+ import org .baeldung .jackson .MyBooleanMixIn ;
11+ import org .baeldung .jackson .MyDto ;
12+ import org .baeldung .jackson .MyDtoIgnoreFieldByName ;
13+ import org .baeldung .jackson .MyDtoIncludeNonDefault ;
1014import org .junit .Test ;
1115
1216import com .fasterxml .jackson .core .JsonParseException ;
@@ -20,7 +24,7 @@ public class JacksonSerializationUnitTest {
2024 @ Test
2125 public final void givenOnlyNonDefaultValuesAreSerialized_whenDtoHasOnlyDefaultValues_thenCorrect () throws JsonParseException , IOException {
2226 final ObjectMapper mapper = new ObjectMapper ();
23- final String dtoAsString = mapper .writeValueAsString (new FooDto ());
27+ final String dtoAsString = mapper .writeValueAsString (new MyDtoIncludeNonDefault ());
2428
2529 assertThat (dtoAsString , not (containsString ("intValue" )));
2630 System .out .println (dtoAsString );
@@ -29,7 +33,7 @@ public final void givenOnlyNonDefaultValuesAreSerialized_whenDtoHasOnlyDefaultVa
2933 @ Test
3034 public final void givenOnlyNonDefaultValuesAreSerialized_whenDtoHasNonDefaultValue_thenCorrect () throws JsonParseException , IOException {
3135 final ObjectMapper mapper = new ObjectMapper ();
32- final FooDto dtoObject = new FooDto ();
36+ final MyDtoIncludeNonDefault dtoObject = new MyDtoIncludeNonDefault ();
3337 dtoObject .setBooleanValue (true );
3438
3539 final String dtoAsString = mapper .writeValueAsString (dtoObject );
@@ -41,7 +45,7 @@ public final void givenOnlyNonDefaultValuesAreSerialized_whenDtoHasNonDefaultVal
4145 @ Test
4246 public final void givenFieldIsIgnored_whenDtoIsSerialized_thenCorrect () throws JsonParseException , IOException {
4347 final ObjectMapper mapper = new ObjectMapper ();
44- final BarDto dtoObject = new BarDto ();
48+ final MyDtoIgnoreFieldByName dtoObject = new MyDtoIgnoreFieldByName ();
4549 dtoObject .setBooleanValue (true );
4650
4751 final String dtoAsString = mapper .writeValueAsString (dtoObject );
@@ -51,6 +55,21 @@ public final void givenFieldIsIgnored_whenDtoIsSerialized_thenCorrect() throws J
5155 System .out .println (dtoAsString );
5256 }
5357
58+ @ Test
59+ public final void givenFieldTypeIsIgnored_whenDtoIsSerialized_thenCorrect () throws JsonParseException , IOException {
60+ final ObjectMapper mapper = new ObjectMapper ();
61+ mapper .addMixInAnnotations (String .class , MyBooleanMixIn .class );
62+ final MyDto dtoObject = new MyDto ();
63+ dtoObject .setBooleanValue (true );
64+
65+ final String dtoAsString = mapper .writeValueAsString (dtoObject );
66+
67+ assertThat (dtoAsString , containsString ("intValue" ));
68+ assertThat (dtoAsString , containsString ("booleanValue" ));
69+ assertThat (dtoAsString , not (containsString ("stringValue" )));
70+ System .out .println (dtoAsString );
71+ }
72+
5473 // tests - multiple entities to json
5574
5675 @ Test
0 commit comments