1414import org .junit .Test ;
1515
1616import com .fasterxml .jackson .core .JsonProcessingException ;
17+ import com .fasterxml .jackson .databind .MapperFeature ;
1718import com .fasterxml .jackson .databind .ObjectMapper ;
1819import com .fasterxml .jackson .databind .ser .BeanSerializerFactory ;
1920import com .fasterxml .jackson .databind .ser .SerializerFactory ;
@@ -25,16 +26,17 @@ public void whenUseJsonViewToSerialize_thenCorrect() throws JsonProcessingExcept
2526 final User user = new User (1 , "John" );
2627
2728 final ObjectMapper mapper = new ObjectMapper ();
29+ mapper .disable (MapperFeature .DEFAULT_VIEW_INCLUSION );
30+
2831 final String result = mapper .writerWithView (Views .Public .class ).writeValueAsString (user );
2932
3033 assertThat (result , containsString ("John" ));
31- assertThat (result , containsString ("1" ));
34+ assertThat (result , not ( containsString ("1" ) ));
3235 }
3336
3437 @ Test
3538 public void whenUsePublicView_thenOnlyPublicSerialized () throws JsonProcessingException {
36- final User owner = new User (1 , "John" );
37- final Item item = new Item (2 , "book" , owner );
39+ final Item item = new Item (2 , "book" , "John" );
3840
3941 final ObjectMapper mapper = new ObjectMapper ();
4042 final String result = mapper .writerWithView (Views .Public .class ).writeValueAsString (item );
@@ -43,13 +45,11 @@ public void whenUsePublicView_thenOnlyPublicSerialized() throws JsonProcessingEx
4345 assertThat (result , containsString ("2" ));
4446
4547 assertThat (result , not (containsString ("John" )));
46- assertThat (result , not (containsString ("1" )));
4748 }
4849
4950 @ Test
5051 public void whenUseInternalView_thenAllSerialized () throws JsonProcessingException {
51- final User owner = new User (1 , "John" );
52- final Item item = new Item (2 , "book" , owner );
52+ final Item item = new Item (2 , "book" , "John" );
5353
5454 final ObjectMapper mapper = new ObjectMapper ();
5555 final String result = mapper .writerWithView (Views .Internal .class ).writeValueAsString (item );
@@ -58,7 +58,6 @@ public void whenUseInternalView_thenAllSerialized() throws JsonProcessingExcepti
5858 assertThat (result , containsString ("2" ));
5959
6060 assertThat (result , containsString ("John" ));
61- assertThat (result , containsString ("1" ));
6261 }
6362
6463 @ Test
@@ -84,4 +83,4 @@ public void whenUseCustomJsonViewToSerialize_thenCorrect() throws JsonProcessing
8483 assertThat (result , containsString ("JOHN" ));
8584 assertThat (result , containsString ("1" ));
8685 }
87- }
86+ }
0 commit comments