Testing

Serial tester

So we are testing json serialization/deserialization. How can Jumahuaca help you to do this? Giving you assertions!! Let's checkout AssertJackson.java

public class AssertJackson {

	public static void assertSerialization(ObjectMapper mapper, Object object, String string)
			throws JsonProcessingException {
		String result = mapper.writeValueAsString(object);
		assertThat(result).isEqualTo(string);
	}

	public static void assertDeserialization(ObjectMapper mapper, Object object, String string, Class<?> clazz)
			throws IOException {
		assertThat(mapper.readValue(string, clazz)).isEqualTo(object);
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static void assertListDeserialization(ObjectMapper mapper, List object, String string, TypeReference type)
			throws IOException {
		assertThat((List) mapper.readValue(string, type)).isEqualTo(object);
	}

}

Yes, we are giving you the entire assertion so you don't have to do much. You have to build the objects and jsons and invoke these assertions.

Let's review the test code. It's the same in both projects because Dropwizard and Spring use Jackson as the default json framework. So you can access the example here and here

public class JacksonJsonSerializationTests {


...
@BeforeAll
	public static void setup() {
		mapper = Jackson.newObjectMapper();
		exchangeToTest1 = new UVAExchange(LocalDate.of(EXCHANGE_1_YEAR, EXCHANGE_1_MONTH, EXCHANGE_1_DAY),
				EXCHANGE_1_RATE);
		exchangeToTest2 = new UVAExchange(LocalDate.of(EXCHANGE_2_YEAR, EXCHANGE_2_MONTH, EXCHANGE_2_DAY),
				EXCHANGE_2_RATE);

	}

	@Test
	public void testUvaExchangeSerializationShouldWork() throws JsonProcessingException {
		assertSerialization(mapper, exchangeToTest1, EXCHANGE_1_SERIALIZED_STRING);
	}

	@Test
	public void testUvaExchangeNullSerializationShouldWork() throws JsonProcessingException {
		assertSerialization(mapper, new UVAExchange(), EXCHANGE_NULL_SERIALIZED);
	}

	@Test
	public void testUvaExchangeDeserializationShouldWork() throws IOException {
		assertDeserialization(mapper, exchangeToTest1, EXCHANGE_1_SERIALIZED_STRING, UVAExchange.class);
	}

It's really simple, build your objects and jsons and call the assertions.

You are welcome :)

Last updated

Was this helpful?