-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestJSONObject.java
51 lines (39 loc) · 1.43 KB
/
TestJSONObject.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
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.File;
public class TestJSONObject {
private String jsonSource;
@Before
public void setUp() throws Exception {
String fixtName = "fixtures/post.json";
File file = new File(fixtName);
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
jsonSource = "";
String text = null;
while ((text = br.readLine()) != null)
jsonSource += text;
}
@Test
public void testEmptyJsonReturnsAnEmptyJSONObject() throws JSONException {
JSONObject h = new JSONObject("{}");
assertEquals(0, h.length());
}
@Test
public void testAssinaturaJSON() throws JSONException {
String expectedTitle = "My Test Post";
String expectedBody = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
JSONObject post = new JSONObject(jsonSource);
assertEquals(4, post.length());
assertEquals(123456789, post.getDouble("id"), 0);
assertEquals(expectedTitle, post.get("title"));
assertEquals(expectedBody, post.get("body"));
JSONObject author = post.getJSONObject("author");
assertEquals("John", author.get("name"));
}
}