|
| 1 | +package me.chanjar.weixin.common.util.json; |
| 2 | + |
| 3 | +import com.google.gson.JsonObject; |
| 4 | +import com.google.gson.stream.JsonReader; |
| 5 | +import org.testng.annotations.Test; |
| 6 | + |
| 7 | +import java.io.StringReader; |
| 8 | + |
| 9 | +import static org.testng.Assert.assertEquals; |
| 10 | +import static org.testng.Assert.assertNotNull; |
| 11 | + |
| 12 | +/** |
| 13 | + * GsonParser 测试类 |
| 14 | + * |
| 15 | + * @author <a href="https://github.com/binarywang">Binary Wang</a> |
| 16 | + */ |
| 17 | +public class GsonParserTest { |
| 18 | + |
| 19 | + @Test |
| 20 | + public void testParseString() { |
| 21 | + String json = "{\"code\":\"ALREADY_EXISTS\",\"message\":\"当前订单已关闭,可查询订单了解关闭原因\"}"; |
| 22 | + JsonObject jsonObject = GsonParser.parse(json); |
| 23 | + assertNotNull(jsonObject); |
| 24 | + assertEquals(jsonObject.get("code").getAsString(), "ALREADY_EXISTS"); |
| 25 | + assertEquals(jsonObject.get("message").getAsString(), "当前订单已关闭,可查询订单了解关闭原因"); |
| 26 | + } |
| 27 | + |
| 28 | + @Test |
| 29 | + public void testParseReader() { |
| 30 | + String json = "{\"code\":\"SUCCESS\",\"message\":\"处理成功\"}"; |
| 31 | + StringReader reader = new StringReader(json); |
| 32 | + JsonObject jsonObject = GsonParser.parse(reader); |
| 33 | + assertNotNull(jsonObject); |
| 34 | + assertEquals(jsonObject.get("code").getAsString(), "SUCCESS"); |
| 35 | + assertEquals(jsonObject.get("message").getAsString(), "处理成功"); |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + public void testParseJsonReader() { |
| 40 | + String json = "{\"errcode\":0,\"errmsg\":\"ok\"}"; |
| 41 | + JsonReader jsonReader = new JsonReader(new StringReader(json)); |
| 42 | + JsonObject jsonObject = GsonParser.parse(jsonReader); |
| 43 | + assertNotNull(jsonObject); |
| 44 | + assertEquals(jsonObject.get("errcode").getAsInt(), 0); |
| 45 | + assertEquals(jsonObject.get("errmsg").getAsString(), "ok"); |
| 46 | + } |
| 47 | +} |
0 commit comments