T - the type under testpublic abstract class AbstractJsonMarshalTester<T>
extends java.lang.Object
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.
JsonContentAssert,
ObjectContentAssert| Modifier and Type | Class and Description |
|---|---|
protected static class |
AbstractJsonMarshalTester.FieldInitializer<M>
Utility class used to support field initialization.
|
| Modifier | Constructor and Description |
|---|---|
protected |
AbstractJsonMarshalTester()
Create a new uninitialized
AbstractJsonMarshalTester instance. |
|
AbstractJsonMarshalTester(java.lang.Class<?> resourceLoadClass,
org.springframework.core.ResolvableType type)
Create a new
AbstractJsonMarshalTester instance. |
| Modifier and Type | Method and Description |
|---|---|
protected JsonContent<T> |
getJsonContent(java.lang.String json)
Factory method used to get a
JsonContent instance from a source JSON
string. |
protected java.lang.Class<?> |
getResourceLoadClass()
Return class used to load relative resources.
|
protected org.springframework.core.ResolvableType |
getType()
Return the type under test.
|
protected void |
initialize(java.lang.Class<?> resourceLoadClass,
org.springframework.core.ResolvableType type)
Initialize the marshal tester for use.
|
ObjectContent<T> |
parse(byte[] jsonBytes)
Return
ObjectContent from parsing the specific JSON bytes. |
ObjectContent<T> |
parse(java.lang.String jsonString)
Return
ObjectContent from parsing the specific JSON String. |
T |
parseObject(byte[] jsonBytes)
Return the object created from parsing the specific JSON bytes.
|
T |
parseObject(java.lang.String jsonString)
Return the object created from parsing the specific JSON String.
|
ObjectContent<T> |
read(java.io.File file)
Return
ObjectContent from reading from the specified file. |
ObjectContent<T> |
read(java.io.InputStream inputStream)
Return
ObjectContent from reading from the specified input stream. |
ObjectContent<T> |
read(java.io.Reader reader)
Return
ObjectContent from reading from the specified reader. |
ObjectContent<T> |
read(org.springframework.core.io.Resource resource)
Return
ObjectContent from reading from the specified resource. |
ObjectContent<T> |
read(java.lang.String resourcePath)
Return
ObjectContent from reading from the specified classpath resource. |
T |
readObject(java.io.File file)
Return the object created from reading from the specified file.
|
T |
readObject(java.io.InputStream inputStream)
Return the object created from reading from the specified input stream.
|
protected T |
readObject(java.io.InputStream inputStream,
org.springframework.core.ResolvableType type)
Read from the specified input stream to create an object of the specified type.
|
T |
readObject(java.io.Reader reader)
Return the object created from reading from the specified reader.
|
protected abstract T |
readObject(java.io.Reader reader,
org.springframework.core.ResolvableType type)
Read from the specified reader to create an object of the specified type.
|
T |
readObject(org.springframework.core.io.Resource resource)
Return the object created from reading from the specified resource.
|
T |
readObject(java.lang.String resourcePath)
Return the object created from reading from the specified classpath resource.
|
JsonContent<T> |
write(T value)
Return
JsonContent from writing the specific value. |
protected abstract java.lang.String |
writeObject(T value,
org.springframework.core.ResolvableType type)
Write the specified object to a JSON string.
|
protected AbstractJsonMarshalTester()
AbstractJsonMarshalTester instance.public AbstractJsonMarshalTester(java.lang.Class<?> resourceLoadClass,
org.springframework.core.ResolvableType type)
AbstractJsonMarshalTester instance.resourceLoadClass - the source class used when loading relative classpath
resourcestype - the type under testprotected final void initialize(java.lang.Class<?> resourceLoadClass,
org.springframework.core.ResolvableType type)
resourceLoadClass - the source class used when loading relative classpath
resourcestype - the type under testprotected final org.springframework.core.ResolvableType getType()
protected final java.lang.Class<?> getResourceLoadClass()
public JsonContent<T> write(T value) throws java.io.IOException
JsonContent from writing the specific value.value - the value to writeJsonContentjava.io.IOException - on write errorprotected JsonContent<T> getJsonContent(java.lang.String json)
JsonContent instance from a source JSON
string.json - the source JSONJsonContent instancepublic T parseObject(byte[] jsonBytes) throws java.io.IOException
jsonBytes - the source JSON bytesjava.io.IOException - on parse errorpublic ObjectContent<T> parse(byte[] jsonBytes) throws java.io.IOException
ObjectContent from parsing the specific JSON bytes.jsonBytes - the source JSON bytesObjectContentjava.io.IOException - on parse errorpublic T parseObject(java.lang.String jsonString) throws java.io.IOException
jsonString - the source JSON stringjava.io.IOException - on parse errorpublic ObjectContent<T> parse(java.lang.String jsonString) throws java.io.IOException
ObjectContent from parsing the specific JSON String.jsonString - the source JSON stringObjectContentjava.io.IOException - on parse errorpublic T readObject(java.lang.String resourcePath) throws java.io.IOException
resourcePath - the source resource path. May be a full path or a path relative
to the resourceLoadClass passed to the constructorjava.io.IOException - on read errorpublic ObjectContent<T> read(java.lang.String resourcePath) throws java.io.IOException
ObjectContent from reading from the specified classpath resource.resourcePath - the source resource path. May be a full path or a path relative
to the resourceLoadClass passed to the constructorObjectContentjava.io.IOException - on read errorpublic T readObject(java.io.File file) throws java.io.IOException
file - the source filejava.io.IOException - on read errorpublic ObjectContent<T> read(java.io.File file) throws java.io.IOException
ObjectContent from reading from the specified file.file - the source fileObjectContentjava.io.IOException - on read errorpublic T readObject(java.io.InputStream inputStream) throws java.io.IOException
inputStream - the source input streamjava.io.IOException - on read errorpublic ObjectContent<T> read(java.io.InputStream inputStream) throws java.io.IOException
ObjectContent from reading from the specified input stream.inputStream - the source input streamObjectContentjava.io.IOException - on read errorpublic T readObject(org.springframework.core.io.Resource resource) throws java.io.IOException
resource - the source resourcejava.io.IOException - on read errorpublic ObjectContent<T> read(org.springframework.core.io.Resource resource) throws java.io.IOException
ObjectContent from reading from the specified resource.resource - the source resourceObjectContentjava.io.IOException - on read errorpublic T readObject(java.io.Reader reader) throws java.io.IOException
reader - the source readerjava.io.IOException - on read errorpublic ObjectContent<T> read(java.io.Reader reader) throws java.io.IOException
ObjectContent from reading from the specified reader.reader - the source readerObjectContentjava.io.IOException - on read errorprotected abstract java.lang.String writeObject(T value, org.springframework.core.ResolvableType type) throws java.io.IOException
value - the source value (never null)type - the resulting type (never null)java.io.IOException - on write errorprotected T readObject(java.io.InputStream inputStream, org.springframework.core.ResolvableType type) throws java.io.IOException
readObject(Reader, ResolvableType).inputStream - the source input stream (never null)type - the resulting type (never null)java.io.IOException - on read errorprotected abstract T readObject(java.io.Reader reader, org.springframework.core.ResolvableType type) throws java.io.IOException
reader - the source reader (never null)type - the resulting type (never null)java.io.IOException - on read error