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
public abstract class AbstractJsonMarshalTester<T> extends Object
Base class for AssertJ based JSON marshal testers. Exposes specific Asserts following aread
,write
orparse
of JSON content. Typically used in combination with an AssertJassertThat
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 seeJsonContentAssert
andObjectContentAssert
.To use this library JSONAssert must be on the test classpath.
- Since:
- 1.4.0
- See Also:
JsonContentAssert
,ObjectContentAssert
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static class
AbstractJsonMarshalTester.FieldInitializer<M>
Utility class used to support field initialization.
Constructor Summary
Constructors Modifier Constructor Description protected
AbstractJsonMarshalTester()
Create a new uninitializedAbstractJsonMarshalTester
instance.AbstractJsonMarshalTester(Class<?> resourceLoadClass, org.springframework.core.ResolvableType type)
Create a newAbstractJsonMarshalTester
instance.
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected Class<?>
getResourceLoadClass()
Return class used to load relative resources.protected org.springframework.core.ResolvableType
getType()
Return the type under test.protected void
initialize(Class<?> resourceLoadClass, org.springframework.core.ResolvableType type)
Initialize the marshal tester for use.ObjectContent<T>
parse(byte[] jsonBytes)
ReturnObjectContent
from parsing the specific JSON bytes.ObjectContent<T>
parse(String jsonString)
ReturnObjectContent
from parsing the specific JSON String.T
parseObject(byte[] jsonBytes)
Return the object created from parsing the specific JSON bytes.T
parseObject(String jsonString)
Return the object created from parsing the specific JSON String.ObjectContent<T>
read(File file)
ReturnObjectContent
from reading from the specified file.ObjectContent<T>
read(InputStream inputStream)
ReturnObjectContent
from reading from the specified input stream.ObjectContent<T>
read(Reader reader)
ReturnObjectContent
from reading from the specified reader.ObjectContent<T>
read(String resourcePath)
ReturnObjectContent
from reading from the specified classpath resource.ObjectContent<T>
read(org.springframework.core.io.Resource resource)
ReturnObjectContent
from reading from the specified resource.T
readObject(File file)
Return the object created from reading from the specified file.T
readObject(InputStream inputStream)
Return the object created from reading from the specified input stream.protected T
readObject(InputStream inputStream, org.springframework.core.ResolvableType type)
Read from the specified input stream to create an object of the specified type.T
readObject(Reader reader)
Return the object created from reading from the specified reader.protected abstract T
readObject(Reader reader, org.springframework.core.ResolvableType type)
Read from the specified reader to create an object of the specified type.T
readObject(String resourcePath)
Return the object created from reading from the specified classpath resource.T
readObject(org.springframework.core.io.Resource resource)
Return the object created from reading from the specified resource.JsonContent<T>
write(T value)
ReturnJsonContent
from writing the specific value.protected abstract String
writeObject(T value, org.springframework.core.ResolvableType type)
Write the specified object to a JSON string.
Constructor Detail
AbstractJsonMarshalTester
protected AbstractJsonMarshalTester()
Create a new uninitializedAbstractJsonMarshalTester
instance.
AbstractJsonMarshalTester
public AbstractJsonMarshalTester(Class<?> resourceLoadClass, org.springframework.core.ResolvableType type)
Create a newAbstractJsonMarshalTester
instance.- Parameters:
resourceLoadClass
- the source class used when loading relative classpath resourcestype
- the type under test
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 resourcestype
- 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
write
public JsonContent<T> write(T value) throws IOException
ReturnJsonContent
from writing the specific value.- Parameters:
value
- the value to write- Returns:
- the
JsonContent
- Throws:
IOException
- on write error
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
parse
public ObjectContent<T> parse(byte[] jsonBytes) throws IOException
ReturnObjectContent
from parsing the specific JSON bytes.- Parameters:
jsonBytes
- the source JSON bytes- Returns:
- the
ObjectContent
- 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
parse
public ObjectContent<T> parse(String jsonString) throws IOException
ReturnObjectContent
from parsing the specific JSON String.- Parameters:
jsonString
- the source JSON string- Returns:
- the
ObjectContent
- 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 theresourceLoadClass
passed to the constructor- Returns:
- the resulting object
- Throws:
IOException
- on read error
read
public ObjectContent<T> read(String resourcePath) throws IOException
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
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
read
public ObjectContent<T> read(File file) throws IOException
ReturnObjectContent
from reading from the specified file.- Parameters:
file
- the source file- Returns:
- the
ObjectContent
- 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
read
public ObjectContent<T> read(InputStream inputStream) throws IOException
ReturnObjectContent
from reading from the specified input stream.- Parameters:
inputStream
- the source input stream- Returns:
- the
ObjectContent
- 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
read
public ObjectContent<T> read(org.springframework.core.io.Resource resource) throws IOException
ReturnObjectContent
from reading from the specified resource.- Parameters:
resource
- the source resource- Returns:
- the
ObjectContent
- 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
read
public ObjectContent<T> read(Reader reader) throws IOException
ReturnObjectContent
from reading from the specified reader.- Parameters:
reader
- the source reader- Returns:
- the
ObjectContent
- 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 (nevernull
)type
- the resulting type (nevernull
)- 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 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
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 (nevernull
)type
- the resulting type (nevernull
)- Returns:
- the resulting object
- Throws:
IOException
- on read error