From 66c3899ecb7d2736448dca94d2a5648c0b036145 Mon Sep 17 00:00:00 2001 From: Bharati Kulkarni <31247303+bmk15897@users.noreply.github.com> Date: Wed, 16 Nov 2022 21:40:36 -0600 Subject: [PATCH] [ISSUE #9643] Fixed Possible JSON Ordering Permutations Problem in Tests (#9505) * Fixed Flaky Test * Fix Updated --- .../nacos/common/utils/JacksonUtilsTest.java | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/common/src/test/java/com/alibaba/nacos/common/utils/JacksonUtilsTest.java b/common/src/test/java/com/alibaba/nacos/common/utils/JacksonUtilsTest.java index 44e0f29ed..2aff441aa 100644 --- a/common/src/test/java/com/alibaba/nacos/common/utils/JacksonUtilsTest.java +++ b/common/src/test/java/com/alibaba/nacos/common/utils/JacksonUtilsTest.java @@ -37,7 +37,7 @@ import java.math.BigDecimal; import java.nio.charset.Charset; import java.util.Collections; import java.util.Date; -import java.util.HashMap; +import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.concurrent.atomic.AtomicBoolean; @@ -435,7 +435,7 @@ public class JacksonUtilsTest { @Test public void testToJsonBytes() { - Map map = new HashMap(); + Map map = new LinkedHashMap(); map.put("string", "你好,中国!"); map.put("integer", 999); RestResult> restResult = new RestResult(); @@ -443,9 +443,8 @@ public class JacksonUtilsTest { byte[] bytes = JacksonUtils.toJsonBytes(restResult); String jsonFromBytes = ByteUtils.toString(bytes); - String expectedJson = "{\"code\":0,\"data\":{\"string\":\"你好,中国!\",\"integer\":999}}"; - Assert.assertEquals(expectedJson, jsonFromBytes); - + Assert.assertTrue(jsonFromBytes.contains("\"code\":0")); + Assert.assertTrue(jsonFromBytes.contains("\"data\":{\"string\":\"你好,中国!\",\"integer\":999}")); // old `toJsonBytes` method implementation: // public static byte[] toJsonBytes(Object obj) { // try { @@ -457,7 +456,9 @@ public class JacksonUtilsTest { // here is a verification to compare with the old implementation byte[] bytesFromOldImplementation = ByteUtils.toBytes(JacksonUtils.toJson(restResult)); - Assert.assertEquals(expectedJson, new String(bytesFromOldImplementation, Charset.forName(Constants.ENCODE))); + String jsonFromBytesOldImplementation = new String(bytesFromOldImplementation, Charset.forName(Constants.ENCODE)); + Assert.assertTrue(jsonFromBytesOldImplementation.contains("\"code\":0")); + Assert.assertTrue(jsonFromBytesOldImplementation.contains("\"data\":{\"string\":\"你好,中国!\",\"integer\":999}")); } @Test