Class AbstractJsonMarshalTester<T>
java.lang.Object
org.springframework.boot.test.json.AbstractJsonMarshalTester<T>
- Type Parameters:
T
- the type under test
- Direct Known Subclasses:
GsonTester
,JacksonTester
,JsonbTester
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
- Author:
- Phillip Webb
- See Also:
-
Nested Class Summary
Modifier and TypeClassDescriptionprotected static class
Utility class used to support field initialization. -
Constructor Summary
ModifierConstructorDescriptionprotected
Create a new uninitializedAbstractJsonMarshalTester
instance.AbstractJsonMarshalTester
(Class<?> resourceLoadClass, ResolvableType type) Create a newAbstractJsonMarshalTester
instance. -
Method Summary
Modifier and TypeMethodDescriptionprotected JsonContent<T>
getJsonContent
(String json) Factory method used to get aJsonContent
instance from a source JSON string.protected final Class<?>
Return class used to load relative resources.protected final ResolvableType
getType()
Return the type under test.protected final void
initialize
(Class<?> resourceLoadClass, ResolvableType type) Initialize the marshal tester for use.parse
(byte[] jsonBytes) ReturnObjectContent
from parsing the specific JSON bytes.ReturnObjectContent
from parsing the specific JSON String.parseObject
(byte[] jsonBytes) Return the object created from parsing the specific JSON bytes.parseObject
(String jsonString) Return the object created from parsing the specific JSON String.ReturnObjectContent
from reading from the specified file.read
(InputStream inputStream) ReturnObjectContent
from reading from the specified input stream.ReturnObjectContent
from reading from the specified reader.ReturnObjectContent
from reading from the specified classpath resource.ReturnObjectContent
from reading from the specified resource.readObject
(File file) Return the object created from reading from the specified file.readObject
(InputStream inputStream) Return the object created from reading from the specified input stream.protected T
readObject
(InputStream inputStream, ResolvableType type) Read from the specified input stream to create an object of the specified type.readObject
(Reader reader) Return the object created from reading from the specified reader.protected abstract T
readObject
(Reader reader, ResolvableType type) Read from the specified reader to create an object of the specified type.readObject
(String resourcePath) Return the object created from reading from the specified classpath resource.readObject
(Resource resource) Return the object created from reading from the specified resource.ReturnJsonContent
from writing the specific value.protected abstract String
writeObject
(T value, ResolvableType type) Write the specified object to a JSON string.
-
Constructor Details
-
AbstractJsonMarshalTester
protected AbstractJsonMarshalTester()Create a new uninitializedAbstractJsonMarshalTester
instance. -
AbstractJsonMarshalTester
Create a newAbstractJsonMarshalTester
instance.- Parameters:
resourceLoadClass
- the source class used when loading relative classpath resourcestype
- the type under test
-
-
Method Details
-
initialize
Initialize the marshal tester for use.- Parameters:
resourceLoadClass
- the source class used when loading relative classpath resourcestype
- the type under test
-
getType
Return the type under test.- Returns:
- the type under test
-
getResourceLoadClass
Return class used to load relative resources.- Returns:
- the resource load class
-
write
ReturnJsonContent
from writing the specific value.- Parameters:
value
- the value to write- Returns:
- the
JsonContent
- Throws:
IOException
- on write error
-
getJsonContent
Factory method used to get aJsonContent
instance from a source JSON string.- Parameters:
json
- the source JSON- Returns:
- a new
JsonContent
instance - Since:
- 2.1.5
-
parseObject
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
-
parse
ReturnObjectContent
from parsing the specific JSON bytes.- Parameters:
jsonBytes
- the source JSON bytes- Returns:
- the
ObjectContent
- Throws:
IOException
- on parse error
-
parseObject
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
-
parse
ReturnObjectContent
from parsing the specific JSON String.- Parameters:
jsonString
- the source JSON string- Returns:
- the
ObjectContent
- Throws:
IOException
- on parse error
-
readObject
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 theresourceLoadClass
passed to the constructor- Returns:
- the resulting object
- Throws:
IOException
- on read error
-
read
ReturnObjectContent
from reading from the specified classpath resource.- Parameters:
resourcePath
- the source resource path. May be a full path or a path relative to theresourceLoadClass
passed to the constructor- Returns:
- the
ObjectContent
- Throws:
IOException
- on read error
-
readObject
Return the object created from reading from the specified file.- Parameters:
file
- the source file- Returns:
- the resulting object
- Throws:
IOException
- on read error
-
read
ReturnObjectContent
from reading from the specified file.- Parameters:
file
- the source file- Returns:
- the
ObjectContent
- Throws:
IOException
- on read error
-
readObject
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
-
read
ReturnObjectContent
from reading from the specified input stream.- Parameters:
inputStream
- the source input stream- Returns:
- the
ObjectContent
- Throws:
IOException
- on read error
-
readObject
Return the object created from reading from the specified resource.- Parameters:
resource
- the source resource- Returns:
- the resulting object
- Throws:
IOException
- on read error
-
read
ReturnObjectContent
from reading from the specified resource.- Parameters:
resource
- the source resource- Returns:
- the
ObjectContent
- Throws:
IOException
- on read error
-
readObject
Return the object created from reading from the specified reader.- Parameters:
reader
- the source reader- Returns:
- the resulting object
- Throws:
IOException
- on read error
-
read
ReturnObjectContent
from reading from the specified reader.- Parameters:
reader
- the source reader- Returns:
- the
ObjectContent
- Throws:
IOException
- on read error
-
writeObject
Write the specified object to a JSON string.- Parameters:
value
- the source value (nevernull
)type
- the resulting type (nevernull
)- Returns:
- the JSON string
- Throws:
IOException
- on write error
-
readObject
Read from the specified input stream to create an object of the specified type. The default implementation delegates toreadObject(Reader, ResolvableType)
.- Parameters:
inputStream
- the source input stream (nevernull
)type
- the resulting type (nevernull
)- Returns:
- the resulting object
- Throws:
IOException
- on read error
-
readObject
Read from the specified reader to create an object of the specified type.- Parameters:
reader
- the source reader (nevernull
)type
- the resulting type (nevernull
)- Returns:
- the resulting object
- Throws:
IOException
- on read error
-