Class JacksonTester<T>
- java.lang.Object
- org.springframework.boot.test.json.AbstractJsonMarshalTester<T>
- org.springframework.boot.test.json.JacksonTester<T>
- Type Parameters:
T
- the type under test
public class JacksonTester<T> extends AbstractJsonMarshalTester<T>
AssertJ based JSON tester backed by Jackson. Usually instantiated viainitFields(Object, ObjectMapper)
, for example:public class ExampleObjectJsonTests { private JacksonTester<ExampleObject> json; @Before public void setup() { ObjectMapper objectMapper = new ObjectMapper(); JacksonTester.initFields(this, objectMapper); } @Test public void testWriteJson() throws IOException { ExampleObject object = //... assertThat(json.write(object)).isEqualToJson("expected.json"); } }
SeeAbstractJsonMarshalTester
for more details.- Since:
- 1.4.0
Nested Class Summary
Nested classes/interfaces inherited from class org.springframework.boot.test.json.AbstractJsonMarshalTester
AbstractJsonMarshalTester.FieldInitializer<M>
Constructor Summary
Constructors Modifier Constructor Description protected
JacksonTester(com.fasterxml.jackson.databind.ObjectMapper objectMapper)
Create a newJacksonTester
instance.JacksonTester(Class<?> resourceLoadClass, org.springframework.core.ResolvableType type, com.fasterxml.jackson.databind.ObjectMapper objectMapper)
Create a newJacksonTester
instance.JacksonTester(Class<?> resourceLoadClass, org.springframework.core.ResolvableType type, com.fasterxml.jackson.databind.ObjectMapper objectMapper, Class<?> view)
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description JacksonTester<T>
forView(Class<?> view)
Returns a new instance ofJacksonTester
with the view that should be used for json serialization/deserialization.static void
initFields(Object testInstance, com.fasterxml.jackson.databind.ObjectMapper objectMapper)
Utility method to initializeJacksonTester
fields.static void
initFields(Object testInstance, org.springframework.beans.factory.ObjectFactory<com.fasterxml.jackson.databind.ObjectMapper> objectMapperFactory)
Utility method to initializeJacksonTester
fields.protected T
readObject(InputStream inputStream, org.springframework.core.ResolvableType type)
Read from the specified input stream to create an object of the specified type.protected T
readObject(Reader reader, org.springframework.core.ResolvableType type)
Read from the specified reader to create an object of the specified type.protected String
writeObject(T value, org.springframework.core.ResolvableType type)
Write the specified object to a JSON string.Methods inherited from class org.springframework.boot.test.json.AbstractJsonMarshalTester
getResourceLoadClass, getType, initialize, parse, parse, parseObject, parseObject, read, read, read, read, read, readObject, readObject, readObject, readObject, readObject, write
Constructor Detail
JacksonTester
protected JacksonTester(com.fasterxml.jackson.databind.ObjectMapper objectMapper)
Create a newJacksonTester
instance.- Parameters:
objectMapper
- the Jackson object mapper
JacksonTester
public JacksonTester(Class<?> resourceLoadClass, org.springframework.core.ResolvableType type, com.fasterxml.jackson.databind.ObjectMapper objectMapper)
Create a newJacksonTester
instance.- Parameters:
resourceLoadClass
- the source class used to load resourcestype
- the type under testobjectMapper
- the Jackson object mapper
JacksonTester
public JacksonTester(Class<?> resourceLoadClass, org.springframework.core.ResolvableType type, com.fasterxml.jackson.databind.ObjectMapper objectMapper, Class<?> view)
Method Detail
readObject
protected T readObject(InputStream inputStream, org.springframework.core.ResolvableType type) throws IOException
Description copied from class:AbstractJsonMarshalTester
Read from the specified input stream to create an object of the specified type. The default implementation delegates toAbstractJsonMarshalTester.readObject(Reader, ResolvableType)
.- Overrides:
readObject
in classAbstractJsonMarshalTester<T>
- Parameters:
inputStream
- the source input stream (nevernull
)type
- the resulting type (nevernull
)- Returns:
- the resulting object
- Throws:
IOException
- on read error
readObject
protected T readObject(Reader reader, org.springframework.core.ResolvableType type) throws IOException
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
writeObject
protected String writeObject(T value, org.springframework.core.ResolvableType type) throws IOException
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
initFields
public static void initFields(Object testInstance, com.fasterxml.jackson.databind.ObjectMapper objectMapper)
Utility method to initializeJacksonTester
fields. Seeclass-level documentation
for example usage.- Parameters:
testInstance
- the test instanceobjectMapper
- the object mapper- See Also:
initFields(Object, ObjectMapper)
initFields
public static void initFields(Object testInstance, org.springframework.beans.factory.ObjectFactory<com.fasterxml.jackson.databind.ObjectMapper> objectMapperFactory)
Utility method to initializeJacksonTester
fields. Seeclass-level documentation
for example usage.- Parameters:
testInstance
- the test instanceobjectMapperFactory
- a factory to create the object mapper- See Also:
initFields(Object, ObjectMapper)
forView
public JacksonTester<T> forView(Class<?> view)
Returns a new instance ofJacksonTester
with the view that should be used for json serialization/deserialization.- Parameters:
view
- the view class- Returns:
- the new instance