-
Notifications
You must be signed in to change notification settings - Fork 72
/
JsonFormattingTest.java
60 lines (57 loc) · 1.56 KB
/
JsonFormattingTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package org.approvaltests;
import com.google.gson.GsonBuilder;
import org.junit.jupiter.api.Test;
import java.time.Instant;
public class JsonFormattingTest
{
@Test
public void testGsonCircular()
{
Approvals.verifyException(() -> {
JsonApprovals.verifyAsJson(Circular.getIndirectCircularReference());
});
}
@Test
public void testBasicFormatting()
{
String json = "{\"infos\":{\"address\":\"my address\",\"phone\":\"my phone\"},\"insurance\":{\"forks\":[14,53,123],\"prices\":[5,8,\"3%\"]}}";
JsonApprovals.verifyJson(json);
}
@Test
public void testIncorrectFormatting()
{
String json = "{\"infos\":{address:my address,\"phone\":\"my phone\"},\"insurance\":{\"forks\":[14,53,123],\"prices\":[5,8,\"3%\"]}}";
JsonApprovals.verifyJson(json);
}
@Test
void demonstrateNullIssueInVerifyJson()
{
// begin-snippet: CustomGsonBuilderShowingNull
Person person = new Person("Max", null, 1);
JsonApprovals.verifyAsJson(person, GsonBuilder::serializeNulls);
// end-snippet
}
static class Person
{
public String firstName;
public String lastName;
public int age;
public Person(String firstName, String lastName, int age)
{
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
}
@Test
public void testInstantAndOtherDateObjects()
{
DateStuff dateStuff = new DateStuff();
dateStuff.instant = Instant.ofEpochSecond(1651708441);
JsonApprovals.verifyAsJson(dateStuff);
}
private static class DateStuff
{
public Instant instant;
}
}