Class BaseJacksonTest<T>

  • Type Parameters:
    T - the type parameter

    @ExtendWith(org.mockito.junit.jupiter.MockitoExtension.class)
    public abstract class BaseJacksonTest<T>
    extends java.lang.Object
    Extend your test with this class and provide the immutable class as a type.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected static java.util.Set<java.lang.String> methodsToIgnore
      The Methods to ignore.
      protected com.fasterxml.jackson.databind.ObjectMapper objectMapper
      The Object mapper.
      protected java.lang.String simpleName
      The Simple name.
    • Constructor Summary

      Constructors 
      Constructor Description
      BaseJacksonTest()
      Base constructor.
    • Method Summary

      All Methods Static Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      void baseJacksonTestSetup()
      Base jackson test setup.
      static void findObjectMethods()
      Find object methods.
      protected abstract java.lang.Class<T> getBaseClass()
      Define the model interface/class that we are testing.
      protected abstract T getInstance()
      Return a standard instance of the class that should go back and forth to json.
      protected java.util.Optional<java.lang.Class<?>> getPolymorphicBaseClass()
      Override this if you class has a base class.
      protected com.fasterxml.jackson.databind.ObjectMapper objectMapper()
      Override this if you have a custom object mapper to use.
      void testCollectionMethods()
      Test verifies expected methods that are lists,sets,etc will not fail the JSON conversion if they are missing.
      void testMapMethods()
      Test verifies expected methods that are maps will not fail the JSON conversion if they are missing.
      void testNotNullMethods()
      Test verifies expected methods that are required by the model will fail the JSON conversion if they are missing.
      void testNullableMethods()
      Test verifies expected methods that are not required by the model will not fail the JSON conversion if they are missing.
      void testOptionalMethods()
      Test verifies expected methods that are optional by the model will not fail the JSON conversion if they are missing.
      void testPolymorphicBaseClass()
      Test polymorphic base class.
      void testRoundTrip()
      Test verifies that we can write json from the model, and convert that json back to the object.
      void testRoundTripWithExtraCharacters()
      Test verifies that we can write json from the model, and convert that json back to the object, even if the json has extra fields added.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • methodsToIgnore

        protected static java.util.Set<java.lang.String> methodsToIgnore
        The Methods to ignore.
      • objectMapper

        protected com.fasterxml.jackson.databind.ObjectMapper objectMapper
        The Object mapper.
      • simpleName

        protected java.lang.String simpleName
        The Simple name.
    • Constructor Detail

      • BaseJacksonTest

        public BaseJacksonTest()
        Base constructor.
    • Method Detail

      • findObjectMethods

        @BeforeAll
        public static void findObjectMethods()
        Find object methods.
      • getBaseClass

        protected abstract java.lang.Class<T> getBaseClass()
        Define the model interface/class that we are testing. We use this query for properties we want.
        Returns:
        Class that we are testing.
      • getInstance

        protected abstract T getInstance()
        Return a standard instance of the class that should go back and forth to json. Note, we will use the equals method of this class to validate.
        Returns:
        an instance of the class.
      • getPolymorphicBaseClass

        protected java.util.Optional<java.lang.Class<?>> getPolymorphicBaseClass()
        Override this if you class has a base class. Used to test polymorphic behavior is setup correctly with Jackson.
        Returns:
        optional of base class.
      • objectMapper

        protected com.fasterxml.jackson.databind.ObjectMapper objectMapper()
        Override this if you have a custom object mapper to use. This will be retrieved once per test.
        Returns:
        object mapper.
      • baseJacksonTestSetup

        @BeforeEach
        public void baseJacksonTestSetup()
        Base jackson test setup.
      • testRoundTrip

        @Test
        public void testRoundTrip()
                           throws com.fasterxml.jackson.core.JsonProcessingException
        Test verifies that we can write json from the model, and convert that json back to the object.
        Throws:
        com.fasterxml.jackson.core.JsonProcessingException - if object mapper fails.
      • testRoundTripWithExtraCharacters

        @Test
        public void testRoundTripWithExtraCharacters()
                                              throws com.fasterxml.jackson.core.JsonProcessingException
        Test verifies that we can write json from the model, and convert that json back to the object, even if the json has extra fields added.
        Throws:
        com.fasterxml.jackson.core.JsonProcessingException - if object mapper fails.
      • testNotNullMethods

        @Test
        public void testNotNullMethods()
                                throws com.fasterxml.jackson.core.JsonProcessingException
        Test verifies expected methods that are required by the model will fail the JSON conversion if they are missing.
        Throws:
        com.fasterxml.jackson.core.JsonProcessingException - if object mapper fails.
      • testNullableMethods

        @Test
        public void testNullableMethods()
                                 throws com.fasterxml.jackson.core.JsonProcessingException,
                                        java.lang.reflect.InvocationTargetException,
                                        java.lang.IllegalAccessException
        Test verifies expected methods that are not required by the model will not fail the JSON conversion if they are missing.
        Throws:
        com.fasterxml.jackson.core.JsonProcessingException - if object mapper fails.
        java.lang.reflect.InvocationTargetException - the invocation target exception
        java.lang.IllegalAccessException - the illegal access exception
      • testCollectionMethods

        @Test
        public void testCollectionMethods()
                                   throws com.fasterxml.jackson.core.JsonProcessingException,
                                          java.lang.reflect.InvocationTargetException,
                                          java.lang.IllegalAccessException
        Test verifies expected methods that are lists,sets,etc will not fail the JSON conversion if they are missing. Note, this does not handle min quantites.
        Throws:
        com.fasterxml.jackson.core.JsonProcessingException - if object mapper fails.
        java.lang.reflect.InvocationTargetException - the invocation target exception
        java.lang.IllegalAccessException - the illegal access exception
      • testMapMethods

        @Test
        public void testMapMethods()
                            throws com.fasterxml.jackson.core.JsonProcessingException,
                                   java.lang.reflect.InvocationTargetException,
                                   java.lang.IllegalAccessException
        Test verifies expected methods that are maps will not fail the JSON conversion if they are missing.
        Throws:
        com.fasterxml.jackson.core.JsonProcessingException - if object mapper fails.
        java.lang.reflect.InvocationTargetException - the invocation target exception
        java.lang.IllegalAccessException - the illegal access exception
      • testOptionalMethods

        @Test
        public void testOptionalMethods()
                                 throws com.fasterxml.jackson.core.JsonProcessingException,
                                        java.lang.reflect.InvocationTargetException,
                                        java.lang.IllegalAccessException
        Test verifies expected methods that are optional by the model will not fail the JSON conversion if they are missing.
        Throws:
        com.fasterxml.jackson.core.JsonProcessingException - if object mapper fails.
        java.lang.reflect.InvocationTargetException - the invocation target exception
        java.lang.IllegalAccessException - the illegal access exception
      • testPolymorphicBaseClass

        @Test
        public void testPolymorphicBaseClass()
                                      throws com.fasterxml.jackson.core.JsonProcessingException
        Test polymorphic base class.
        Throws:
        com.fasterxml.jackson.core.JsonProcessingException - the json processing exception