Package com.codeheadsystems.test.model
Class BaseJacksonTest<T>
java.lang.Object
com.codeheadsystems.test.model.BaseJacksonTest<T>
- Type Parameters:
T-
@ExtendWith(org.mockito.junit.jupiter.MockitoExtension.class)
public abstract class BaseJacksonTest<T>
extends Object
Extend your test with this class and provide the immutable class as a type.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected com.fasterxml.jackson.databind.ObjectMapperprotected String -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidstatic voidDefine the model interface/class that we are testing.protected abstract TReturn a standard instance of the class that should go back and forth to json.Override this if you class has a base class.protected com.fasterxml.jackson.databind.ObjectMapperOverride this if you have a custom object mapper to use.voidTest verifies expected methods that are lists,sets,etc will not fail the JSON conversion if they are missing.voidTest verifies expected methods that are maps will not fail the JSON conversion if they are missing.voidTest verifies expected methods that are required by the model will fail the JSON conversion if they are missing.voidTest verifies expected methods that are not required by the model will not fail the JSON conversion if they are missing.voidTest verifies expected methods that are optional by the model will not fail the JSON conversion if they are missing.voidvoidTest verifies that we can write json from the model, and convert that json back to the object.voidTest verifies that we can write json from the model, and convert that json back to the object, even if the json has extra fields added.
-
Field Details
-
methodsToIgnore
-
objectMapper
protected com.fasterxml.jackson.databind.ObjectMapper objectMapper -
simpleName
-
-
Constructor Details
-
BaseJacksonTest
public BaseJacksonTest()
-
-
Method Details
-
findObjectMethods
@BeforeAll public static void findObjectMethods() -
getBaseClass
Define the model interface/class that we are testing. We use this query for properties we want.- Returns:
- Class that we are testing.
-
getInstance
Return a standard instance of the class that should go back and forth to json. Note, we will use the equals method of this class to validate.- Returns:
- an instance of the class.
-
getPolymorphicBaseClass
Override this if you class has a base class. Used to test polymorphic behavior is setup correctly with Jackson.- Returns:
- optional of base class.
-
objectMapper
protected com.fasterxml.jackson.databind.ObjectMapper objectMapper()Override this if you have a custom object mapper to use. This will be retrieved once per test.- Returns:
- object mapper.
-
baseJacksonTestSetup
@BeforeEach public void baseJacksonTestSetup() -
testRoundTrip
@Test public void testRoundTrip() throws com.fasterxml.jackson.core.JsonProcessingExceptionTest verifies that we can write json from the model, and convert that json back to the object.- Throws:
com.fasterxml.jackson.core.JsonProcessingException- if object mapper fails.
-
testRoundTripWithExtraCharacters
@Test public void testRoundTripWithExtraCharacters() throws com.fasterxml.jackson.core.JsonProcessingExceptionTest verifies that we can write json from the model, and convert that json back to the object, even if the json has extra fields added.- Throws:
com.fasterxml.jackson.core.JsonProcessingException- if object mapper fails.
-
testNotNullMethods
@Test public void testNotNullMethods() throws com.fasterxml.jackson.core.JsonProcessingExceptionTest verifies expected methods that are required by the model will fail the JSON conversion if they are missing.- Throws:
com.fasterxml.jackson.core.JsonProcessingException- if object mapper fails.
-
testNullableMethods
@Test public void testNullableMethods() throws com.fasterxml.jackson.core.JsonProcessingException, InvocationTargetException, IllegalAccessExceptionTest verifies expected methods that are not required by the model will not fail the JSON conversion if they are missing.- Throws:
com.fasterxml.jackson.core.JsonProcessingException- if object mapper fails.InvocationTargetExceptionIllegalAccessException
-
testCollectionMethods
@Test public void testCollectionMethods() throws com.fasterxml.jackson.core.JsonProcessingException, InvocationTargetException, IllegalAccessExceptionTest verifies expected methods that are lists,sets,etc will not fail the JSON conversion if they are missing. Note, this does not handle min quantites.- Throws:
com.fasterxml.jackson.core.JsonProcessingException- if object mapper fails.InvocationTargetExceptionIllegalAccessException
-
testMapMethods
@Test public void testMapMethods() throws com.fasterxml.jackson.core.JsonProcessingException, InvocationTargetException, IllegalAccessExceptionTest verifies expected methods that are maps will not fail the JSON conversion if they are missing.- Throws:
com.fasterxml.jackson.core.JsonProcessingException- if object mapper fails.InvocationTargetExceptionIllegalAccessException
-
testOptionalMethods
@Test public void testOptionalMethods() throws com.fasterxml.jackson.core.JsonProcessingException, InvocationTargetException, IllegalAccessExceptionTest verifies expected methods that are optional by the model will not fail the JSON conversion if they are missing.- Throws:
com.fasterxml.jackson.core.JsonProcessingException- if object mapper fails.InvocationTargetExceptionIllegalAccessException
-
testPolymorphicBaseClass
@Test public void testPolymorphicBaseClass() throws com.fasterxml.jackson.core.JsonProcessingException- Throws:
com.fasterxml.jackson.core.JsonProcessingException
-