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
AssertJ based JSON tester backed by Jackson. Usually instantiated via
initFields(Object, JsonMapper), for example:
public class ExampleObjectJsonTests {
private JacksonTester<ExampleObject> json;
@Before
public void setup() {
JsonMapper jsonMapper = new JsonMapper();
JacksonTester.initFields(this, jsonMapper);
}
@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, Madhura Bhave, Diego Berrueta
-
Nested Class Summary
Nested classes/interfaces inherited from class AbstractJsonMarshalTester
AbstractJsonMarshalTester.FieldInitializer<M> -
Constructor Summary
ConstructorsModifierConstructorDescriptionJacksonTester(Class<?> resourceLoadClass, ResolvableType type, tools.jackson.databind.json.JsonMapper jsonMapper) Create a newJacksonTesterinstance.JacksonTester(Class<?> resourceLoadClass, ResolvableType type, tools.jackson.databind.json.JsonMapper jsonMapper, @Nullable Class<?> view) Create a newJacksonTesterinstance.protectedJacksonTester(tools.jackson.databind.json.JsonMapper jsonMapper) Create a newJacksonTesterinstance. -
Method Summary
Modifier and TypeMethodDescriptionReturns a new instance ofJacksonTesterwith the view that should be used for json serialization/deserialization.protected JsonContent<T> getJsonContent(String json) Factory method used to get aJsonContentinstance from a source JSON string.static voidinitFields(Object testInstance, ObjectFactory<tools.jackson.databind.json.JsonMapper> jsonMapperFactory) Utility method to initializeJacksonTesterfields.static voidinitFields(Object testInstance, tools.jackson.databind.json.JsonMapper jsonMapper) Utility method to initializeJacksonTesterfields.protected TreadObject(InputStream inputStream, ResolvableType type) Read from the specified input stream to create an object of the specified type.protected TreadObject(Reader reader, ResolvableType type) Read from the specified reader to create an object of the specified type.protected StringwriteObject(T value, ResolvableType type) Write the specified object to a JSON string.Methods inherited from class AbstractJsonMarshalTester
getResourceLoadClass, getType, initialize, parse, parse, parseObject, parseObject, read, read, read, read, read, readObject, readObject, readObject, readObject, readObject, write
-
Constructor Details
-
JacksonTester
protected JacksonTester(tools.jackson.databind.json.JsonMapper jsonMapper) Create a newJacksonTesterinstance.- Parameters:
jsonMapper- the Jackson JSON mapper- Since:
- 4.0.0
-
JacksonTester
public JacksonTester(Class<?> resourceLoadClass, ResolvableType type, tools.jackson.databind.json.JsonMapper jsonMapper) Create a newJacksonTesterinstance.- Parameters:
resourceLoadClass- the source class used to load resourcestype- the type under testjsonMapper- the Jackson JSON mapper- Since:
- 4.0.0
-
JacksonTester
public JacksonTester(Class<?> resourceLoadClass, ResolvableType type, tools.jackson.databind.json.JsonMapper jsonMapper, @Nullable Class<?> view) Create a newJacksonTesterinstance.- Parameters:
resourceLoadClass- the source class used to load resourcestype- the type under testjsonMapper- the Jackson JSON mapperview- the JSON view- Since:
- 4.0.0
-
-
Method Details
-
getJsonContent
Description copied from class:AbstractJsonMarshalTesterFactory method used to get aJsonContentinstance from a source JSON string.- Overrides:
getJsonContentin classAbstractJsonMarshalTester<T>- Parameters:
json- the source JSON- Returns:
- a new
JsonContentinstance
-
readObject
Description copied from class:AbstractJsonMarshalTesterRead from the specified input stream to create an object of the specified type. The default implementation delegates toAbstractJsonMarshalTester.readObject(Reader, ResolvableType).- Overrides:
readObjectin classAbstractJsonMarshalTester<T>- Parameters:
inputStream- the source input stream (nevernull)type- the resulting type (nevernull)- Returns:
- the resulting object
- Throws:
IOException- on read error
-
readObject
Description copied from class:AbstractJsonMarshalTesterRead from the specified reader to create an object of the specified type.- Specified by:
readObjectin classAbstractJsonMarshalTester<T>- Parameters:
reader- the source reader (nevernull)type- the resulting type (nevernull)- Returns:
- the resulting object
- Throws:
IOException- on read error
-
writeObject
Description copied from class:AbstractJsonMarshalTesterWrite the specified object to a JSON string.- Specified by:
writeObjectin 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, tools.jackson.databind.json.JsonMapper jsonMapper) Utility method to initializeJacksonTesterfields. Seeclass-level documentationfor example usage.- Parameters:
testInstance- the test instancejsonMapper- the JSON mapper- Since:
- 4.0.0
- See Also:
-
initFields
public static void initFields(Object testInstance, ObjectFactory<tools.jackson.databind.json.JsonMapper> jsonMapperFactory) Utility method to initializeJacksonTesterfields. Seeclass-level documentationfor example usage.- Parameters:
testInstance- the test instancejsonMapperFactory- a factory to create the JSON mapper- Since:
- 4.0.0
- See Also:
-
forView
Returns a new instance ofJacksonTesterwith the view that should be used for json serialization/deserialization.- Parameters:
view- the view class- Returns:
- the new instance
-