Procházet zdrojové kódy

Merge pull request #1212 from qinnnyul/replacing-main-with-junit-for-tesing

许雪里 před 5 roky
rodič
revize
ac70dea46d
No account linked to committer's email

+ 0 - 28
xxl-job-admin/src/main/java/com/xxl/job/admin/core/util/JacksonUtil.java Zobrazit soubor

@@ -9,8 +9,6 @@ import org.slf4j.Logger;
9 9
 import org.slf4j.LoggerFactory;
10 10
 
11 11
 import java.io.IOException;
12
-import java.util.HashMap;
13
-import java.util.Map;
14 12
 
15 13
 /**
16 14
  * Jackson util
@@ -91,30 +89,4 @@ public class JacksonUtil {
91 89
 		}
92 90
 		return null;
93 91
 	}
94
-
95
-    /*public static <T> T readValueRefer(String jsonStr, Class<T> clazz) {
96
-    	try {
97
-			return getInstance().readValue(jsonStr, new TypeReference<T>() { });
98
-		} catch (JsonParseException e) {
99
-			logger.error(e.getMessage(), e);
100
-		} catch (JsonMappingException e) {
101
-			logger.error(e.getMessage(), e);
102
-		} catch (IOException e) {
103
-			logger.error(e.getMessage(), e);
104
-		}
105
-    	return null;
106
-    }*/
107
-
108
-    public static void main(String[] args) {
109
-		try {
110
-			Map<String, String> map = new HashMap<String, String>();
111
-			map.put("aaa", "111");
112
-			map.put("bbb", "222");
113
-			String json = writeValueAsString(map);
114
-			System.out.println(json);
115
-			System.out.println(readValue(json, Map.class));
116
-		} catch (Exception e) {
117
-			logger.error(e.getMessage(), e);
118
-		}
119
-	}
120 92
 }

+ 42 - 0
xxl-job-admin/src/test/java/com/xxl/job/admin/core/util/JacksonUtilTest.java Zobrazit soubor

@@ -0,0 +1,42 @@
1
+package com.xxl.job.admin.core.util;
2
+
3
+import org.hamcrest.core.Is;
4
+import org.junit.Test;
5
+
6
+import java.util.HashMap;
7
+import java.util.Map;
8
+
9
+import static com.xxl.job.admin.core.util.JacksonUtil.writeValueAsString;
10
+import static org.hamcrest.core.Is.is;
11
+import static org.junit.Assert.assertThat;
12
+
13
+public class JacksonUtilTest {
14
+
15
+    @Test
16
+    public void shouldWriteValueAsString() {
17
+        //given
18
+        Map<String, String> map = new HashMap<>();
19
+        map.put("aaa", "111");
20
+        map.put("bbb", "222");
21
+
22
+        //when
23
+        String json = writeValueAsString(map);
24
+
25
+        //then
26
+        assertThat(json, is("{\"aaa\":\"111\",\"bbb\":\"222\"}"));
27
+    }
28
+
29
+    @Test
30
+    public void shouldReadValueAsObject() {
31
+        //given
32
+        String jsonString = "{\"aaa\":\"111\",\"bbb\":\"222\"}";
33
+
34
+        //when
35
+        Map result = JacksonUtil.readValue(jsonString, Map.class);
36
+
37
+        //then
38
+        assertThat(result.get("aaa"), Is.<Object>is("111"));
39
+        assertThat(result.get("bbb"), Is.<Object>is("222"));
40
+
41
+    }
42
+}