From a6cff8f6a9b13e7c89894afa8936603059c18c10 Mon Sep 17 00:00:00 2001 From: ylqin Date: Sat, 26 Oct 2019 14:14:55 +0800 Subject: [PATCH] use junit test to replace main() for testing --- .../xxl/job/admin/core/util/JacksonUtil.java | 28 ------------- .../job/admin/core/util/JacksonUtilTest.java | 42 +++++++++++++++++++ 2 files changed, 42 insertions(+), 28 deletions(-) create mode 100644 xxl-job-admin/src/test/java/com/xxl/job/admin/core/util/JacksonUtilTest.java diff --git a/xxl-job-admin/src/main/java/com/xxl/job/admin/core/util/JacksonUtil.java b/xxl-job-admin/src/main/java/com/xxl/job/admin/core/util/JacksonUtil.java index 4cf5a189..4f4ea3cc 100644 --- a/xxl-job-admin/src/main/java/com/xxl/job/admin/core/util/JacksonUtil.java +++ b/xxl-job-admin/src/main/java/com/xxl/job/admin/core/util/JacksonUtil.java @@ -9,8 +9,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.IOException; -import java.util.HashMap; -import java.util.Map; /** * Jackson util @@ -91,30 +89,4 @@ public class JacksonUtil { } return null; } - - /*public static T readValueRefer(String jsonStr, Class clazz) { - try { - return getInstance().readValue(jsonStr, new TypeReference() { }); - } catch (JsonParseException e) { - logger.error(e.getMessage(), e); - } catch (JsonMappingException e) { - logger.error(e.getMessage(), e); - } catch (IOException e) { - logger.error(e.getMessage(), e); - } - return null; - }*/ - - public static void main(String[] args) { - try { - Map map = new HashMap(); - map.put("aaa", "111"); - map.put("bbb", "222"); - String json = writeValueAsString(map); - System.out.println(json); - System.out.println(readValue(json, Map.class)); - } catch (Exception e) { - logger.error(e.getMessage(), e); - } - } } diff --git a/xxl-job-admin/src/test/java/com/xxl/job/admin/core/util/JacksonUtilTest.java b/xxl-job-admin/src/test/java/com/xxl/job/admin/core/util/JacksonUtilTest.java new file mode 100644 index 00000000..52db4676 --- /dev/null +++ b/xxl-job-admin/src/test/java/com/xxl/job/admin/core/util/JacksonUtilTest.java @@ -0,0 +1,42 @@ +package com.xxl.job.admin.core.util; + +import org.hamcrest.core.Is; +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; + +import static com.xxl.job.admin.core.util.JacksonUtil.writeValueAsString; +import static org.hamcrest.core.Is.is; +import static org.junit.Assert.assertThat; + +public class JacksonUtilTest { + + @Test + public void shouldWriteValueAsString() { + //given + Map map = new HashMap<>(); + map.put("aaa", "111"); + map.put("bbb", "222"); + + //when + String json = writeValueAsString(map); + + //then + assertThat(json, is("{\"aaa\":\"111\",\"bbb\":\"222\"}")); + } + + @Test + public void shouldReadValueAsObject() { + //given + String jsonString = "{\"aaa\":\"111\",\"bbb\":\"222\"}"; + + //when + Map result = JacksonUtil.readValue(jsonString, Map.class); + + //then + assertThat(result.get("aaa"), Is.is("111")); + assertThat(result.get("bbb"), Is.is("222")); + + } +} \ No newline at end of file