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
Nested ClassesModifier and TypeClassDescriptionprotected static classUtility class used to support field initialization. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedCreate a new uninitializedAbstractJsonMarshalTesterinstance.AbstractJsonMarshalTester(Class<?> resourceLoadClass, ResolvableType type) Create a newAbstractJsonMarshalTesterinstance. -
Method Summary
Modifier and TypeMethodDescriptionprotected JsonContent<T>getJsonContent(String json) Factory method used to get aJsonContentinstance from a source JSON string.protected final Class<?>Return class used to load relative resources.protected final ResolvableTypegetType()Return the type under test.protected final voidinitialize(Class<?> resourceLoadClass, ResolvableType type) Initialize the marshal tester for use.parse(byte[] jsonBytes) ReturnObjectContentfrom parsing the specific JSON bytes.ReturnObjectContentfrom 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.ReturnObjectContentfrom reading from the specified file.read(InputStream inputStream) ReturnObjectContentfrom reading from the specified input stream.ReturnObjectContentfrom reading from the specified reader.ReturnObjectContentfrom reading from the specified classpath resource.ReturnObjectContentfrom 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 TreadObject(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 TreadObject(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.ReturnJsonContentfrom writing the specific value.protected abstract StringwriteObject(T value, ResolvableType type) Write the specified object to a JSON string.
-
Constructor Details
-
AbstractJsonMarshalTester
protected AbstractJsonMarshalTester()Create a new uninitializedAbstractJsonMarshalTesterinstance. -
AbstractJsonMarshalTester
Create a newAbstractJsonMarshalTesterinstance.- 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
ReturnJsonContentfrom writing the specific value.- Parameters:
value- the value to write- Returns:
- the
JsonContent - Throws:
IOException- on write error
-
getJsonContent
Factory method used to get aJsonContentinstance from a source JSON string.- Parameters:
json- the source JSON- Returns:
- a new
JsonContentinstance - 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
ReturnObjectContentfrom 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
ReturnObjectContentfrom 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 theresourceLoadClasspassed to the constructor- Returns:
- the resulting object
- Throws:
IOException- on read error
-
read
ReturnObjectContentfrom reading from the specified classpath resource.- Parameters:
resourcePath- the source resource path. May be a full path or a path relative to theresourceLoadClasspassed 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
ReturnObjectContentfrom 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
ReturnObjectContentfrom 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
ReturnObjectContentfrom 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
ReturnObjectContentfrom 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
-