Class AbstractJsonMarshalTester<T>

  • Type Parameters:
    T - the type under test
    Direct Known Subclasses:
    GsonTester, JacksonTester, JsonbTester

    public abstract class AbstractJsonMarshalTester<T>
    extends Object
    Base class for AssertJ based JSON marshal testers. Exposes specific Asserts following a read, write or parse of JSON content. Typically used in combination with an AssertJ assertThat call. For example:
     public class ExampleObjectJsonTests {
    
         private AbstractJsonTester<ExampleObject> json = //...
    
         @Test
         public void testWriteJson() {
             ExampleObject object = //...
             assertThat(json.write(object)).isEqualToJson("expected.json");
             assertThat(json.read("expected.json")).isEqualTo(object);
         }
    
     }
     
    For a complete list of supported assertions see JsonContentAssert and ObjectContentAssert.

    To use this library JSONAssert must be on the test classpath.

    Since:
    1.4.0
    See Also:
    JsonContentAssert, ObjectContentAssert
    • Method Detail

      • initialize

        protected final void initialize​(Class<?> resourceLoadClass,
                                        org.springframework.core.ResolvableType type)
        Initialize the marshal tester for use.
        Parameters:
        resourceLoadClass - the source class used when loading relative classpath resources
        type - the type under test
      • getType

        protected final org.springframework.core.ResolvableType getType()
        Return the type under test.
        Returns:
        the type under test
      • getResourceLoadClass

        protected final Class<?> getResourceLoadClass()
        Return class used to load relative resources.
        Returns:
        the resource load class
      • parseObject

        public T parseObject​(byte[] jsonBytes)
                      throws IOException
        Return the object created from parsing the specific JSON bytes.
        Parameters:
        jsonBytes - the source JSON bytes
        Returns:
        the resulting object
        Throws:
        IOException - on parse error
      • parseObject

        public T parseObject​(String jsonString)
                      throws IOException
        Return the object created from parsing the specific JSON String.
        Parameters:
        jsonString - the source JSON string
        Returns:
        the resulting object
        Throws:
        IOException - on parse error
      • readObject

        public T readObject​(String resourcePath)
                     throws IOException
        Return the object created from reading from the specified classpath resource.
        Parameters:
        resourcePath - the source resource path. May be a full path or a path relative to the resourceLoadClass passed to the constructor
        Returns:
        the resulting object
        Throws:
        IOException - on read error
      • readObject

        public T readObject​(File file)
                     throws IOException
        Return the object created from reading from the specified file.
        Parameters:
        file - the source file
        Returns:
        the resulting object
        Throws:
        IOException - on read error
      • readObject

        public T readObject​(InputStream inputStream)
                     throws IOException
        Return the object created from reading from the specified input stream.
        Parameters:
        inputStream - the source input stream
        Returns:
        the resulting object
        Throws:
        IOException - on read error
      • readObject

        public T readObject​(org.springframework.core.io.Resource resource)
                     throws IOException
        Return the object created from reading from the specified resource.
        Parameters:
        resource - the source resource
        Returns:
        the resulting object
        Throws:
        IOException - on read error
      • readObject

        public T readObject​(Reader reader)
                     throws IOException
        Return the object created from reading from the specified reader.
        Parameters:
        reader - the source reader
        Returns:
        the resulting object
        Throws:
        IOException - on read error
      • writeObject

        protected abstract String writeObject​(T value,
                                              org.springframework.core.ResolvableType type)
                                       throws IOException
        Write the specified object to a JSON string.
        Parameters:
        value - the source value (never null)
        type - the resulting type (never null)
        Returns:
        the JSON string
        Throws:
        IOException - on write error
      • readObject

        protected T readObject​(InputStream inputStream,
                               org.springframework.core.ResolvableType type)
                        throws IOException
        Read from the specified input stream to create an object of the specified type. The default implementation delegates to readObject(Reader, ResolvableType).
        Parameters:
        inputStream - the source input stream (never null)
        type - the resulting type (never null)
        Returns:
        the resulting object
        Throws:
        IOException - on read error
      • readObject

        protected abstract T readObject​(Reader reader,
                                        org.springframework.core.ResolvableType type)
                                 throws IOException
        Read from the specified reader to create an object of the specified type.
        Parameters:
        reader - the source reader (never null)
        type - the resulting type (never null)
        Returns:
        the resulting object
        Throws:
        IOException - on read error