From 1e6e45b345239538f2b5ef58f5ff206b4b716ea1 Mon Sep 17 00:00:00 2001 From: cheese8 Date: Fri, 1 Jul 2022 17:41:28 +0800 Subject: [PATCH] Refator JacksonUtils and JacksonUtilsTest in 2020.0 (#366) --- CHANGELOG.md | 1 + .../cloud/common/util/JacksonUtils.java | 5 ++- .../cloud/common/util/JacksonUtilsTest.java | 34 ++++++++----------- 3 files changed, 18 insertions(+), 22 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1036cdbbe..8cddf799d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,3 +24,4 @@ - [rm code: Condition 'null != interceptors' is always 'true' ](https://github.com/Tencent/spring-cloud-tencent/pull/356) - [docs:update logo in README.](https://github.com/Tencent/spring-cloud-tencent/pull/359) - [Use jdk constants instead of magic variables](https://github.com/Tencent/spring-cloud-tencent/pull/362) +- [Refator JacksonUtils and JacksonUtilsTest](https://github.com/Tencent/spring-cloud-tencent/pull/366) diff --git a/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/util/JacksonUtils.java b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/util/JacksonUtils.java index a30fc8bc1..0bb96bae2 100644 --- a/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/util/JacksonUtils.java +++ b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/util/JacksonUtils.java @@ -30,7 +30,7 @@ import org.springframework.util.StringUtils; /** * Utils for Jackson. * - * @author Haotian Zhang + * @author Haotian Zhang, cheese8 */ public final class JacksonUtils { @@ -80,8 +80,7 @@ public final class JacksonUtils { } catch (JsonProcessingException e) { LOG.error( - "Json to map failed. check if the format of the json string[{}] is correct.", - jsonStr, e); + "Json to map failed. check if the format of the json string[{}] is correct.", jsonStr, e); throw new RuntimeException("Json to map failed.", e); } } diff --git a/spring-cloud-tencent-commons/src/test/java/com/tencent/cloud/common/util/JacksonUtilsTest.java b/spring-cloud-tencent-commons/src/test/java/com/tencent/cloud/common/util/JacksonUtilsTest.java index db0868dff..e07dcceb2 100644 --- a/spring-cloud-tencent-commons/src/test/java/com/tencent/cloud/common/util/JacksonUtilsTest.java +++ b/spring-cloud-tencent-commons/src/test/java/com/tencent/cloud/common/util/JacksonUtilsTest.java @@ -26,12 +26,12 @@ import org.junit.runner.RunWith; import org.mockito.junit.MockitoJUnitRunner; import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.fail; +import static org.assertj.core.api.Assertions.assertThatThrownBy; /** * Test for {@link JacksonUtils}. * - * @author lepdou, Haotian Zhang + * @author lepdou, Haotian Zhang, cheese8 */ @RunWith(MockitoJUnitRunner.class) public class JacksonUtilsTest { @@ -42,10 +42,7 @@ public class JacksonUtilsTest { sourceMap.put("k1", "v1"); sourceMap.put("k2", "v2"); sourceMap.put("k3", "v3"); - - String jsonStr = JacksonUtils.serialize2Json(sourceMap); - - assertThat(jsonStr).isEqualTo("{\"k1\":\"v1\",\"k2\":\"v2\",\"k3\":\"v3\"}"); + assertThat(JacksonUtils.serialize2Json(sourceMap)).isEqualTo("{\"k1\":\"v1\",\"k2\":\"v2\",\"k3\":\"v3\"}"); } @Test @@ -56,20 +53,19 @@ public class JacksonUtilsTest { assertThat(map.get("k1")).isEqualTo("v1"); assertThat(map.get("k2")).isEqualTo("v2"); assertThat(map.get("k3")).isEqualTo("v3"); + } - assertThat(JacksonUtils.deserialize2Map("")).isNotNull(); - assertThat(JacksonUtils.deserialize2Map("")).isEmpty(); + @Test + public void testDeserializeBlankIntoEmptyMap() { + Map map = JacksonUtils.deserialize2Map(""); + assertThat(map).isNotNull(); + assertThat(map).isEmpty(); + } - jsonStr = "{\"k1\":\"v1\",\"k2\":\"v2\",\"k3\":\"v3\""; - try { - JacksonUtils.deserialize2Map(jsonStr); - fail("RuntimeException should be thrown."); - } - catch (RuntimeException exception) { - assertThat(exception.getMessage()).isEqualTo("Json to map failed."); - } - catch (Throwable throwable) { - fail("RuntimeException should be thrown."); - } + @Test + public void testDeserializeThrowsRuntimeException() { + String jsonStr = "{\"k1\":\"v1\",\"k2\":\"v2\",\"k3\":\"v3\""; + assertThatThrownBy(() -> JacksonUtils.deserialize2Map(jsonStr)) + .isExactlyInstanceOf(RuntimeException.class).hasMessage("Json to map failed."); } }