T - the type under testpublic abstract class AbstractJsonMarshalTester<T> extends 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(Class<?> resourceLoadClass,
ResolvableType type)
Create a new
AbstractJsonMarshalTester instance. |
| Modifier and Type | Method and Description |
|---|---|
protected JsonContent<T> |
getJsonContent(String json)
Factory method used to get a
JsonContent instance from a source JSON
string. |
protected Class<?> |
getResourceLoadClass()
Return class used to load relative resources.
|
protected ResolvableType |
getType()
Return the type under test.
|
protected void |
initialize(Class<?> resourceLoadClass,
ResolvableType type)
Initialize the marshal tester for use.
|
ObjectContent<T> |
parse(byte[] jsonBytes)
Return
ObjectContent from parsing the specific JSON bytes. |
ObjectContent<T> |
parse(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(String jsonString)
Return the object created from parsing the specific JSON String.
|
ObjectContent<T> |
read(File file)
Return
ObjectContent from reading from the specified file. |
ObjectContent<T> |
read(InputStream inputStream)
Return
ObjectContent from reading from the specified input stream. |
ObjectContent<T> |
read(Reader reader)
Return
ObjectContent from reading from the specified reader. |
ObjectContent<T> |
read(Resource resource)
Return
ObjectContent from reading from the specified resource. |
ObjectContent<T> |
read(String resourcePath)
Return
ObjectContent from reading from the specified classpath 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,
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,
ResolvableType type)
Read from the specified reader to create an object of the specified type.
|
T |
readObject(Resource resource)
Return the object created from reading from the specified resource.
|
T |
readObject(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 String |
writeObject(T value,
ResolvableType type)
Write the specified object to a JSON string.
|
protected AbstractJsonMarshalTester()
AbstractJsonMarshalTester instance.public AbstractJsonMarshalTester(Class<?> resourceLoadClass, ResolvableType type)
AbstractJsonMarshalTester instance.resourceLoadClass - the source class used when loading relative classpath
resourcestype - the type under testprotected final void initialize(Class<?> resourceLoadClass, ResolvableType type)
resourceLoadClass - the source class used when loading relative classpath
resourcestype - the type under testprotected final ResolvableType getType()
protected final Class<?> getResourceLoadClass()
public JsonContent<T> write(T value) throws IOException
JsonContent from writing the specific value.value - the value to writeJsonContentIOException - on write errorprotected JsonContent<T> getJsonContent(String json)
JsonContent instance from a source JSON
string.json - the source JSONJsonContent instancepublic T parseObject(byte[] jsonBytes) throws IOException
jsonBytes - the source JSON bytesIOException - on parse errorpublic ObjectContent<T> parse(byte[] jsonBytes) throws IOException
ObjectContent from parsing the specific JSON bytes.jsonBytes - the source JSON bytesObjectContentIOException - on parse errorpublic T parseObject(String jsonString) throws IOException
jsonString - the source JSON stringIOException - on parse errorpublic ObjectContent<T> parse(String jsonString) throws IOException
ObjectContent from parsing the specific JSON String.jsonString - the source JSON stringObjectContentIOException - on parse errorpublic T readObject(String resourcePath) throws IOException
resourcePath - the source resource path. May be a full path or a path relative
to the resourceLoadClass passed to the constructorIOException - on read errorpublic ObjectContent<T> read(String resourcePath) throws 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 constructorObjectContentIOException - on read errorpublic T readObject(File file) throws IOException
file - the source fileIOException - on read errorpublic ObjectContent<T> read(File file) throws IOException
ObjectContent from reading from the specified file.file - the source fileObjectContentIOException - on read errorpublic T readObject(InputStream inputStream) throws IOException
inputStream - the source input streamIOException - on read errorpublic ObjectContent<T> read(InputStream inputStream) throws IOException
ObjectContent from reading from the specified input stream.inputStream - the source input streamObjectContentIOException - on read errorpublic T readObject(Resource resource) throws IOException
resource - the source resourceIOException - on read errorpublic ObjectContent<T> read(Resource resource) throws IOException
ObjectContent from reading from the specified resource.resource - the source resourceObjectContentIOException - on read errorpublic T readObject(Reader reader) throws IOException
reader - the source readerIOException - on read errorpublic ObjectContent<T> read(Reader reader) throws IOException
ObjectContent from reading from the specified reader.reader - the source readerObjectContentIOException - on read errorprotected abstract String writeObject(T value, ResolvableType type) throws IOException
value - the source value (never null)type - the resulting type (never null)IOException - on write errorprotected T readObject(InputStream inputStream, ResolvableType type) throws IOException
readObject(Reader, ResolvableType).inputStream - the source input stream (never null)type - the resulting type (never null)IOException - on read errorprotected abstract T readObject(Reader reader, ResolvableType type) throws IOException
reader - the source reader (never null)type - the resulting type (never null)IOException - on read errorCopyright © 2020 Pivotal Software, Inc.. All rights reserved.