Class GsonTester<T>
java.lang.Object
org.springframework.boot.test.json.AbstractJsonMarshalTester<T>
org.springframework.boot.test.json.GsonTester<T>
- Type Parameters:
T
- the type under test
AssertJ based JSON tester backed by Gson. Usually instantiated via
initFields(Object, Gson)
, for example: public class ExampleObjectJsonTests { private GsonTester<ExampleObject> json; @Before public void setup() { Gson gson = new GsonBuilder().create(); GsonTester.initFields(this, gson); } @Test public void testWriteJson() throws IOException { ExampleObject object = //... assertThat(json.write(object)).isEqualToJson("expected.json"); } }See
AbstractJsonMarshalTester
for more details.- Since:
- 1.4.0
- Author:
- Phillip Webb
-
Nested Class Summary
Nested classes/interfaces inherited from class org.springframework.boot.test.json.AbstractJsonMarshalTester
AbstractJsonMarshalTester.FieldInitializer<M>
-
Constructor Summary
ModifierConstructorDescriptionprotected
GsonTester
(com.google.gson.Gson gson) Create a new uninitializedGsonTester
instance.GsonTester
(Class<?> resourceLoadClass, ResolvableType type, com.google.gson.Gson gson) Create a newGsonTester
instance. -
Method Summary
Modifier and TypeMethodDescriptionstatic void
initFields
(Object testInstance, com.google.gson.Gson gson) Utility method to initializeGsonTester
fields.static void
initFields
(Object testInstance, ObjectFactory<com.google.gson.Gson> gson) Utility method to initializeGsonTester
fields.protected T
readObject
(Reader reader, ResolvableType type) Read from the specified reader to create an object of the specified type.protected String
writeObject
(T value, ResolvableType type) Write the specified object to a JSON string.Methods inherited from class org.springframework.boot.test.json.AbstractJsonMarshalTester
getJsonContent, getResourceLoadClass, getType, initialize, parse, parse, parseObject, parseObject, read, read, read, read, read, readObject, readObject, readObject, readObject, readObject, readObject, write
-
Constructor Details
-
GsonTester
protected GsonTester(com.google.gson.Gson gson) Create a new uninitializedGsonTester
instance.- Parameters:
gson
- the Gson instance
-
GsonTester
Create a newGsonTester
instance.- Parameters:
resourceLoadClass
- the source class used to load resourcestype
- the type under testgson
- the Gson instance- See Also:
-
-
Method Details
-
writeObject
Description copied from class:AbstractJsonMarshalTester
Write the specified object to a JSON string.- Specified by:
writeObject
in classAbstractJsonMarshalTester<T>
- Parameters:
value
- the source value (nevernull
)type
- the resulting type (nevernull
)- Returns:
- the JSON string
- Throws:
IOException
- on write error
-
readObject
Description copied from class:AbstractJsonMarshalTester
Read from the specified reader to create an object of the specified type.- Specified by:
readObject
in classAbstractJsonMarshalTester<T>
- Parameters:
reader
- the source reader (nevernull
)type
- the resulting type (nevernull
)- Returns:
- the resulting object
- Throws:
IOException
- on read error
-
initFields
Utility method to initializeGsonTester
fields. Seeclass-level documentation
for example usage.- Parameters:
testInstance
- the test instancegson
- the Gson instance
-
initFields
Utility method to initializeGsonTester
fields. Seeclass-level documentation
for example usage.- Parameters:
testInstance
- the test instancegson
- an object factory to create the Gson instance
-