Skip to content

Commit a498970

Browse files
authored
Merge pull request #68 from symborsk/JsonUtil-Unit-Tests
Json util unit tests
2 parents 46cc098 + 6b39a94 commit a498970

File tree

1 file changed

+33
-5
lines changed

1 file changed

+33
-5
lines changed

payments-api/src/test/java/com/intuit/payment/util/JsonUtilTest.java

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,24 +30,52 @@
3030
*
3131
*/
3232
public class JsonUtilTest {
33-
33+
34+
// Test class used to ensure exception is thrown inside of JsonUtil
35+
public class ClassWithPrivateFields {
36+
ClassWithPrivateFields(int id, String name){
37+
id = id;
38+
name = name;
39+
}
40+
int id;
41+
String name;
42+
}
3443

3544
@Test
36-
public void testSerialize() throws SerializationException{
45+
public void testSerialize() throws SerializationException {
3746
Card card = new Card.Builder().number("12345").build();
3847
String cardStr = JsonUtil.serialize(card);
3948
Assert.assertNotNull(cardStr);
4049
String result = "{\n \"number\" : \"12345\"\n}";
4150
Assert.assertEquals(cardStr, result);
4251
}
43-
52+
53+
@Test
54+
public void testNullSerialize() throws SerializationException {
55+
String cardStr = JsonUtil.serialize(null);
56+
Assert.assertNull(cardStr);
57+
}
58+
59+
@Test(expectedExceptions = SerializationException.class)
60+
public void testErrorSerialize() throws SerializationException {
61+
// Serializing this causes a InvalidDefinitionException to be thrown
62+
ClassWithPrivateFields classWithPrivateFields = new ClassWithPrivateFields(1, "John");
63+
JsonUtil.serialize(classWithPrivateFields);
64+
}
65+
4466
@Test
4567
public void testDeserialize() throws SerializationException {
4668
String cardStr = "{\"number\":\"12345\"}";
4769
Card card = (Card) JsonUtil.deserialize(cardStr, new TypeReference<Card>() {} );
4870
Assert.assertEquals(card.getNumber(), "12345");
4971
}
50-
72+
73+
@Test(expectedExceptions = SerializationException.class)
74+
public void testErrorDeserialize() throws SerializationException {
75+
String cardStr = "{\"number\":\"12345\"\"}";
76+
JsonUtil.deserialize(cardStr, new TypeReference<Card>() {} );
77+
}
78+
5179
@SuppressWarnings("unchecked")
5280
@Test
5381
public void testDeserializeList() throws SerializationException {
@@ -57,5 +85,5 @@ public void testDeserializeList() throws SerializationException {
5785
Assert.assertEquals(cards.get(0).getNumber(), "12345");
5886
}
5987

60-
88+
6189
}

0 commit comments

Comments
 (0)