From 6de92e40c5a0b4503414e105dda26eef683534b1 Mon Sep 17 00:00:00 2001 From: onewe Date: Mon, 16 May 2022 10:12:04 +0800 Subject: [PATCH] Fix some failing unit tests in nacos config module (#8379) * [ISSUE #8348] fix mockito's mockStatic method error * [ISSUE #8348] fix all the unit test's error Close #8348 --- .../ConditionDistributedEmbedStorageTest.java | 28 +++--- .../ConditionOnEmbeddedStorageTest.java | 11 ++- .../ConditionOnExternalStorageTest.java | 12 ++- .../ConditionStandaloneEmbedStorageTest.java | 31 ++++--- .../controller/ConfigControllerTest.java | 9 +- .../controller/ConfigOpsControllerTest.java | 25 +++--- .../controller/ConfigServletInnerTest.java | 88 +++++++++---------- .../controller/HealthControllerTest.java | 1 + .../controller/HistoryControllerTest.java | 2 + ...igChangeBatchListenRequestHandlerTest.java | 16 ++-- .../remote/ConfigQueryRequestHandlerTest.java | 44 ++++++---- .../GroupCapacityPersistServiceTest.java | 7 +- .../TenantCapacityPersistServiceTest.java | 8 +- .../datasource/DynamicDataSourceTest.java | 12 +-- .../server/service/dump/DumpServiceTest.java | 10 +++ .../config/server/utils/DiskUtilsTest.java | 19 ++-- .../config/server/utils/MD5UtilTest.java | 35 ++++---- .../config/server/utils/ParamUtilsTest.java | 3 - .../config/server/utils/PropertyUtilTest.java | 14 +-- .../config/server/utils/ResponseUtilTest.java | 5 +- 20 files changed, 219 insertions(+), 161 deletions(-) diff --git a/config/src/test/java/com/alibaba/nacos/config/server/configuration/ConditionDistributedEmbedStorageTest.java b/config/src/test/java/com/alibaba/nacos/config/server/configuration/ConditionDistributedEmbedStorageTest.java index 11a24ad20c9..c77430bf6a4 100644 --- a/config/src/test/java/com/alibaba/nacos/config/server/configuration/ConditionDistributedEmbedStorageTest.java +++ b/config/src/test/java/com/alibaba/nacos/config/server/configuration/ConditionDistributedEmbedStorageTest.java @@ -22,6 +22,7 @@ import org.junit.Before; import org.junit.Test; import org.mockito.Mock; +import org.mockito.MockedStatic; import org.mockito.Mockito; import org.springframework.context.annotation.ConditionContext; import org.springframework.core.type.AnnotatedTypeMetadata; @@ -39,27 +40,34 @@ public class ConditionDistributedEmbedStorageTest { @Before public void init() { conditionDistributedEmbedStorage = new ConditionDistributedEmbedStorage(); - Mockito.mockStatic(PropertyUtil.class); - Mockito.mockStatic(EnvUtil.class); + } @Test public void testMatches() { - Mockito.when(PropertyUtil.isEmbeddedStorage()).thenReturn(true); - Mockito.when(EnvUtil.getStandaloneMode()).thenReturn(true); + MockedStatic propertyUtilMockedStatic = Mockito.mockStatic(PropertyUtil.class); + MockedStatic envUtilMockedStatic = Mockito.mockStatic(EnvUtil.class); + + propertyUtilMockedStatic.when(PropertyUtil::isEmbeddedStorage).thenReturn(true); + envUtilMockedStatic.when(EnvUtil::getStandaloneMode).thenReturn(true); Assert.assertFalse(conditionDistributedEmbedStorage.matches(context, metadata)); Mockito.when(PropertyUtil.isEmbeddedStorage()).thenReturn(true); Mockito.when(EnvUtil.getStandaloneMode()).thenReturn(false); + propertyUtilMockedStatic.when(PropertyUtil::isEmbeddedStorage).thenReturn(true); + envUtilMockedStatic.when(EnvUtil::getStandaloneMode).thenReturn(false); Assert.assertTrue(conditionDistributedEmbedStorage.matches(context, metadata)); - - Mockito.when(PropertyUtil.isEmbeddedStorage()).thenReturn(false); - Mockito.when(EnvUtil.getStandaloneMode()).thenReturn(true); + + propertyUtilMockedStatic.when(PropertyUtil::isEmbeddedStorage).thenReturn(false); + envUtilMockedStatic.when(EnvUtil::getStandaloneMode).thenReturn(true); Assert.assertFalse(conditionDistributedEmbedStorage.matches(context, metadata)); - - Mockito.when(PropertyUtil.isEmbeddedStorage()).thenReturn(false); - Mockito.when(EnvUtil.getStandaloneMode()).thenReturn(false); + + propertyUtilMockedStatic.when(PropertyUtil::isEmbeddedStorage).thenReturn(false); + envUtilMockedStatic.when(EnvUtil::getStandaloneMode).thenReturn(false); Assert.assertFalse(conditionDistributedEmbedStorage.matches(context, metadata)); + + propertyUtilMockedStatic.close(); + envUtilMockedStatic.close(); } } diff --git a/config/src/test/java/com/alibaba/nacos/config/server/configuration/ConditionOnEmbeddedStorageTest.java b/config/src/test/java/com/alibaba/nacos/config/server/configuration/ConditionOnEmbeddedStorageTest.java index 97342c36986..36ec69a000c 100644 --- a/config/src/test/java/com/alibaba/nacos/config/server/configuration/ConditionOnEmbeddedStorageTest.java +++ b/config/src/test/java/com/alibaba/nacos/config/server/configuration/ConditionOnEmbeddedStorageTest.java @@ -21,6 +21,7 @@ import org.junit.Before; import org.junit.Test; import org.mockito.Mock; +import org.mockito.MockedStatic; import org.mockito.Mockito; import org.springframework.context.annotation.ConditionContext; import org.springframework.core.type.AnnotatedTypeMetadata; @@ -38,16 +39,18 @@ public class ConditionOnEmbeddedStorageTest { @Before public void init() { conditionOnEmbeddedStorage = new ConditionOnEmbeddedStorage(); - Mockito.mockStatic(PropertyUtil.class); } @Test public void testMatches() { - Mockito.when(PropertyUtil.isEmbeddedStorage()).thenReturn(true); + MockedStatic mockedStatic = Mockito.mockStatic(PropertyUtil.class); + mockedStatic.when(PropertyUtil::isEmbeddedStorage).thenReturn(true); Assert.assertTrue(conditionOnEmbeddedStorage.matches(context, metadata)); - - Mockito.when(PropertyUtil.isEmbeddedStorage()).thenReturn(false); + + mockedStatic.when(PropertyUtil::isEmbeddedStorage).thenReturn(false); Assert.assertFalse(conditionOnEmbeddedStorage.matches(context, metadata)); + + mockedStatic.close(); } } diff --git a/config/src/test/java/com/alibaba/nacos/config/server/configuration/ConditionOnExternalStorageTest.java b/config/src/test/java/com/alibaba/nacos/config/server/configuration/ConditionOnExternalStorageTest.java index 8b3c456d6f8..8698ccc8061 100644 --- a/config/src/test/java/com/alibaba/nacos/config/server/configuration/ConditionOnExternalStorageTest.java +++ b/config/src/test/java/com/alibaba/nacos/config/server/configuration/ConditionOnExternalStorageTest.java @@ -21,6 +21,7 @@ import org.junit.Before; import org.junit.Test; import org.mockito.Mock; +import org.mockito.MockedStatic; import org.mockito.Mockito; import org.springframework.context.annotation.ConditionContext; import org.springframework.core.type.AnnotatedTypeMetadata; @@ -38,16 +39,19 @@ public class ConditionOnExternalStorageTest { @Before public void init() { conditionOnExternalStorage = new ConditionOnExternalStorage(); - Mockito.mockStatic(PropertyUtil.class); } @Test public void testMatches() { - Mockito.when(PropertyUtil.isEmbeddedStorage()).thenReturn(true); - Assert.assertFalse(conditionOnExternalStorage.matches(context, metadata)); + MockedStatic mockedStatic = Mockito.mockStatic(PropertyUtil.class); - Mockito.when(PropertyUtil.isEmbeddedStorage()).thenReturn(false); + mockedStatic.when(PropertyUtil::isEmbeddedStorage).thenReturn(true); + Assert.assertFalse(conditionOnExternalStorage.matches(context, metadata)); + + mockedStatic.when(PropertyUtil::isEmbeddedStorage).thenReturn(false); Assert.assertTrue(conditionOnExternalStorage.matches(context, metadata)); + + mockedStatic.close(); } } diff --git a/config/src/test/java/com/alibaba/nacos/config/server/configuration/ConditionStandaloneEmbedStorageTest.java b/config/src/test/java/com/alibaba/nacos/config/server/configuration/ConditionStandaloneEmbedStorageTest.java index bd634f6b1f2..29222df3ea0 100644 --- a/config/src/test/java/com/alibaba/nacos/config/server/configuration/ConditionStandaloneEmbedStorageTest.java +++ b/config/src/test/java/com/alibaba/nacos/config/server/configuration/ConditionStandaloneEmbedStorageTest.java @@ -22,6 +22,7 @@ import org.junit.Before; import org.junit.Test; import org.mockito.Mock; +import org.mockito.MockedStatic; import org.mockito.Mockito; import org.springframework.context.annotation.ConditionContext; import org.springframework.core.type.AnnotatedTypeMetadata; @@ -39,27 +40,31 @@ public class ConditionStandaloneEmbedStorageTest { @Before public void init() { conditionStandaloneEmbedStorage = new ConditionStandaloneEmbedStorage(); - Mockito.mockStatic(PropertyUtil.class); - Mockito.mockStatic(EnvUtil.class); } @Test public void testMatches() { - Mockito.when(PropertyUtil.isEmbeddedStorage()).thenReturn(true); - Mockito.when(EnvUtil.getStandaloneMode()).thenReturn(true); - Assert.assertTrue(conditionStandaloneEmbedStorage.matches(context, metadata)); + MockedStatic propertyUtilMockedStatic = Mockito.mockStatic(PropertyUtil.class); + MockedStatic envUtilMockedStatic = Mockito.mockStatic(EnvUtil.class); - Mockito.when(PropertyUtil.isEmbeddedStorage()).thenReturn(true); - Mockito.when(EnvUtil.getStandaloneMode()).thenReturn(false); + propertyUtilMockedStatic.when(PropertyUtil::isEmbeddedStorage).thenReturn(true); + envUtilMockedStatic.when(EnvUtil::getStandaloneMode).thenReturn(true); + Assert.assertTrue(conditionStandaloneEmbedStorage.matches(context, metadata)); + + propertyUtilMockedStatic.when(PropertyUtil::isEmbeddedStorage).thenReturn(true); + envUtilMockedStatic.when(EnvUtil::getStandaloneMode).thenReturn(false); Assert.assertFalse(conditionStandaloneEmbedStorage.matches(context, metadata)); - - Mockito.when(PropertyUtil.isEmbeddedStorage()).thenReturn(false); - Mockito.when(EnvUtil.getStandaloneMode()).thenReturn(true); + + propertyUtilMockedStatic.when(PropertyUtil::isEmbeddedStorage).thenReturn(false); + envUtilMockedStatic.when(EnvUtil::getStandaloneMode).thenReturn(true); Assert.assertFalse(conditionStandaloneEmbedStorage.matches(context, metadata)); - - Mockito.when(PropertyUtil.isEmbeddedStorage()).thenReturn(false); - Mockito.when(EnvUtil.getStandaloneMode()).thenReturn(false); + + propertyUtilMockedStatic.when(PropertyUtil::isEmbeddedStorage).thenReturn(false); + envUtilMockedStatic.when(EnvUtil::getStandaloneMode).thenReturn(false); Assert.assertFalse(conditionStandaloneEmbedStorage.matches(context, metadata)); + + propertyUtilMockedStatic.close(); + envUtilMockedStatic.close(); } } diff --git a/config/src/test/java/com/alibaba/nacos/config/server/controller/ConfigControllerTest.java b/config/src/test/java/com/alibaba/nacos/config/server/controller/ConfigControllerTest.java index 3de08f98509..e8e537c888d 100644 --- a/config/src/test/java/com/alibaba/nacos/config/server/controller/ConfigControllerTest.java +++ b/config/src/test/java/com/alibaba/nacos/config/server/controller/ConfigControllerTest.java @@ -38,6 +38,7 @@ import org.junit.runner.RunWith; import org.mockito.InjectMocks; import org.mockito.Mock; +import org.mockito.MockedStatic; import org.mockito.Mockito; import org.springframework.core.env.StandardEnvironment; import org.springframework.mock.web.MockMultipartFile; @@ -389,15 +390,15 @@ public void testExportConfigV2() throws Exception { @Test public void testImportAndPublishConfig() throws Exception { - - Mockito.mockStatic(ZipUtils.class); + + MockedStatic zipUtilsMockedStatic = Mockito.mockStatic(ZipUtils.class); List zipItems = new ArrayList<>(); ZipUtils.ZipItem zipItem = new ZipUtils.ZipItem("test/test", "test"); zipItems.add(zipItem); ZipUtils.UnZipResult unziped = new ZipUtils.UnZipResult(zipItems, null); MockMultipartFile file = new MockMultipartFile("file", "test.zip", "application/zip", "test".getBytes()); - when(ZipUtils.unzip(file.getBytes())).thenReturn(unziped); + zipUtilsMockedStatic.when(() -> ZipUtils.unzip(file.getBytes())).thenReturn(unziped); when(persistService.tenantInfoCountByTenantId("public")).thenReturn(1); Map map = new HashMap<>(); map.put("test", "test"); @@ -416,6 +417,8 @@ public void testImportAndPublishConfig() throws Exception { Assert.assertEquals("200", code); Map resultMap = JacksonUtils.toObj(JacksonUtils.toObj(actualValue).get("data").toString(), Map.class); Assert.assertEquals(map.get("test"), resultMap.get("test").toString()); + + zipUtilsMockedStatic.close(); } diff --git a/config/src/test/java/com/alibaba/nacos/config/server/controller/ConfigOpsControllerTest.java b/config/src/test/java/com/alibaba/nacos/config/server/controller/ConfigOpsControllerTest.java index 047a6ad7328..a368db61af0 100644 --- a/config/src/test/java/com/alibaba/nacos/config/server/controller/ConfigOpsControllerTest.java +++ b/config/src/test/java/com/alibaba/nacos/config/server/controller/ConfigOpsControllerTest.java @@ -100,13 +100,12 @@ public void testSetLogLevel() throws Exception { @Test public void testDerbyOps() throws Exception { - - MockedStatic propertyUtil = Mockito.mockStatic(PropertyUtil.class); - when(PropertyUtil.isEmbeddedStorage()).thenReturn(true); - propertyUtil.close(); - Mockito.mockStatic(DynamicDataSource.class); + MockedStatic propertyUtilMockedStatic = Mockito.mockStatic(PropertyUtil.class); + MockedStatic dynamicDataSourceMockedStatic = Mockito.mockStatic(DynamicDataSource.class); + + propertyUtilMockedStatic.when(PropertyUtil::isEmbeddedStorage).thenReturn(true); DynamicDataSource dataSource = Mockito.mock(DynamicDataSource.class); - when(DynamicDataSource.getInstance()).thenReturn(dataSource); + dynamicDataSourceMockedStatic.when(DynamicDataSource::getInstance).thenReturn(dataSource); LocalDataSourceServiceImpl dataSourceService = Mockito.mock(LocalDataSourceServiceImpl.class); when(dataSource.getDataSource()).thenReturn(dataSourceService); JdbcTemplate template = Mockito.mock(JdbcTemplate.class); @@ -118,23 +117,27 @@ public void testDerbyOps() throws Exception { .param("sql", "SELECT * FROM TEST"); String actualValue = mockMvc.perform(builder).andReturn().getResponse().getContentAsString(); Assert.assertEquals("200", JacksonUtils.toObj(actualValue).get("code").toString()); + propertyUtilMockedStatic.close(); + dynamicDataSourceMockedStatic.close(); } @Test public void testImportDerby() throws Exception { + MockedStatic propertyUtilMockedStatic = Mockito.mockStatic(PropertyUtil.class); + MockedStatic applicationUtilsMockedStatic = Mockito.mockStatic(ApplicationUtils.class); - MockedStatic propertyUtil = Mockito.mockStatic(PropertyUtil.class); - when(PropertyUtil.isEmbeddedStorage()).thenReturn(true); - propertyUtil.close(); - Mockito.mockStatic(ApplicationUtils.class); - when(ApplicationUtils.getBean(DatabaseOperate.class)).thenReturn(Mockito.mock(DatabaseOperate.class)); + propertyUtilMockedStatic.when(PropertyUtil::isEmbeddedStorage).thenReturn(true); + applicationUtilsMockedStatic.when(() -> ApplicationUtils.getBean(DatabaseOperate.class)) + .thenReturn(Mockito.mock(DatabaseOperate.class)); MockMultipartFile file = new MockMultipartFile("file", "test.zip", "application/zip", "test".getBytes()); MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.multipart(Constants.OPS_CONTROLLER_PATH + "/data/removal").file(file); int actualValue = mockMvc.perform(builder).andReturn().getResponse().getStatus(); Assert.assertEquals(200, actualValue); + propertyUtilMockedStatic.close(); + applicationUtilsMockedStatic.close(); } } diff --git a/config/src/test/java/com/alibaba/nacos/config/server/controller/ConfigServletInnerTest.java b/config/src/test/java/com/alibaba/nacos/config/server/controller/ConfigServletInnerTest.java index d60beabe2b7..d1df6f5931a 100644 --- a/config/src/test/java/com/alibaba/nacos/config/server/controller/ConfigServletInnerTest.java +++ b/config/src/test/java/com/alibaba/nacos/config/server/controller/ConfigServletInnerTest.java @@ -82,8 +82,8 @@ public void setUp() { @Test public void testDoPollingConfig() throws Exception { + MockedStatic md5UtilMockedStatic = Mockito.mockStatic(MD5Util.class); - final MockedStatic md5Util = Mockito.mockStatic(MD5Util.class); Map clientMd5Map = new HashMap<>(); MockHttpServletRequest request = new MockHttpServletRequest(); MockHttpServletResponse response = new MockHttpServletResponse(); @@ -91,9 +91,9 @@ public void testDoPollingConfig() throws Exception { changedGroups.add("1"); changedGroups.add("2"); - when(MD5Util.compareMd5(request, response, clientMd5Map)).thenReturn(changedGroups); - when(MD5Util.compareMd5OldResult(changedGroups)).thenReturn("test-old"); - when(MD5Util.compareMd5ResultString(changedGroups)).thenReturn("test-new"); + md5UtilMockedStatic.when(() -> MD5Util.compareMd5(request, response, clientMd5Map)).thenReturn(changedGroups); + md5UtilMockedStatic.when(() -> MD5Util.compareMd5OldResult(changedGroups)).thenReturn("test-old"); + md5UtilMockedStatic.when(() -> MD5Util.compareMd5ResultString(changedGroups)).thenReturn("test-new"); String actualValue = configServletInner.doPollingConfig(request, response, clientMd5Map, 1); @@ -101,19 +101,18 @@ public void testDoPollingConfig() throws Exception { Assert.assertEquals("test-old", response.getHeader(Constants.PROBE_MODIFY_RESPONSE)); Assert.assertEquals("test-new", response.getHeader(Constants.PROBE_MODIFY_RESPONSE_NEW)); Assert.assertEquals("no-cache,no-store", response.getHeader("Cache-Control")); - - md5Util.close(); + + md5UtilMockedStatic.close(); } @Test public void testDoGetConfigV1() throws Exception { + final MockedStatic configCacheServiceMockedStatic = Mockito.mockStatic(ConfigCacheService.class); + final MockedStatic diskUtilMockedStatic = Mockito.mockStatic(DiskUtil.class); + final MockedStatic propertyUtilMockedStatic = Mockito.mockStatic(PropertyUtil.class); - final MockedStatic diskUtil = Mockito.mockStatic(DiskUtil.class); - final MockedStatic configCacheService = Mockito.mockStatic(ConfigCacheService.class); - final MockedStatic propertyUtil = Mockito.mockStatic(PropertyUtil.class); - - when(ConfigCacheService.tryReadLock(anyString())).thenReturn(1); + configCacheServiceMockedStatic.when(() -> ConfigCacheService.tryReadLock(anyString())).thenReturn(1); // isBeta: true CacheItem cacheItem = new CacheItem("test"); @@ -121,10 +120,10 @@ public void testDoGetConfigV1() throws Exception { List ips4Beta = new ArrayList<>(); ips4Beta.add("localhost"); cacheItem.setIps4Beta(ips4Beta); - when(ConfigCacheService.getContentCache(anyString())).thenReturn(cacheItem); + configCacheServiceMockedStatic.when(() -> ConfigCacheService.getContentCache(anyString())).thenReturn(cacheItem); // if direct read is true - when(PropertyUtil.isDirectRead()).thenReturn(true); + propertyUtilMockedStatic.when(PropertyUtil::isDirectRead).thenReturn(true); ConfigInfoBetaWrapper configInfoBetaWrapper = new ConfigInfoBetaWrapper(); configInfoBetaWrapper.setDataId("test"); configInfoBetaWrapper.setGroup("test"); @@ -141,29 +140,28 @@ public void testDoGetConfigV1() throws Exception { Assert.assertEquals("isBeta:true, direct read: true", response.getContentAsString()); // if direct read is false - when(PropertyUtil.isDirectRead()).thenReturn(false); + propertyUtilMockedStatic.when(PropertyUtil::isDirectRead).thenReturn(false); File file = tempFolder.newFile("test.txt"); - when(DiskUtil.targetBetaFile(anyString(), anyString(), anyString())).thenReturn(file); + diskUtilMockedStatic.when(() -> DiskUtil.targetBetaFile(anyString(), anyString(), anyString())).thenReturn(file); response = new MockHttpServletResponse(); actualValue = configServletInner.doGetConfig(request, response, "test", "test", "test", "", "true", "localhost"); Assert.assertEquals(HttpServletResponse.SC_OK + "", actualValue); Assert.assertEquals("true", response.getHeader("isBeta")); Assert.assertEquals("", response.getContentAsString()); - - diskUtil.close(); - configCacheService.close(); - propertyUtil.close(); - + + configCacheServiceMockedStatic.close(); + diskUtilMockedStatic.close(); + propertyUtilMockedStatic.close(); } @Test public void testDoGetConfigV2() throws Exception { + + final MockedStatic configCacheServiceMockedStatic = Mockito.mockStatic(ConfigCacheService.class); + final MockedStatic diskUtilMockedStatic = Mockito.mockStatic(DiskUtil.class); + final MockedStatic propertyUtilMockedStatic = Mockito.mockStatic(PropertyUtil.class); - final MockedStatic diskUtil = Mockito.mockStatic(DiskUtil.class); - final MockedStatic configCacheService = Mockito.mockStatic(ConfigCacheService.class); - final MockedStatic propertyUtil = Mockito.mockStatic(PropertyUtil.class); - - when(ConfigCacheService.tryReadLock(anyString())).thenReturn(1); + configCacheServiceMockedStatic.when(() -> ConfigCacheService.tryReadLock(anyString())).thenReturn(1); // isBeta: false CacheItem cacheItem = new CacheItem("test"); @@ -171,10 +169,10 @@ public void testDoGetConfigV2() throws Exception { List ips4Beta = new ArrayList<>(); ips4Beta.add("localhost"); cacheItem.setIps4Beta(ips4Beta); - when(ConfigCacheService.getContentCache(anyString())).thenReturn(cacheItem); + configCacheServiceMockedStatic.when(() -> ConfigCacheService.getContentCache(anyString())).thenReturn(cacheItem); // if tag is blank and direct read is true - when(PropertyUtil.isDirectRead()).thenReturn(true); + propertyUtilMockedStatic.when(PropertyUtil::isDirectRead).thenReturn(true); ConfigInfoWrapper configInfoWrapper = new ConfigInfoWrapper(); configInfoWrapper.setDataId("test"); configInfoWrapper.setGroup("test"); @@ -189,16 +187,16 @@ public void testDoGetConfigV2() throws Exception { Assert.assertEquals("tag is blank and direct read is true", response.getContentAsString()); // if tag is blank and direct read is false - when(PropertyUtil.isDirectRead()).thenReturn(false); + propertyUtilMockedStatic.when(PropertyUtil::isDirectRead).thenReturn(false); response = new MockHttpServletResponse(); File file = tempFolder.newFile("test.txt"); - when(DiskUtil.targetFile(anyString(), anyString(), anyString())).thenReturn(file); + diskUtilMockedStatic.when(() -> DiskUtil.targetFile(anyString(), anyString(), anyString())).thenReturn(file); actualValue = configServletInner.doGetConfig(request, response, "test", "test", "test", "", "true", "localhost"); Assert.assertEquals(HttpServletResponse.SC_OK + "", actualValue); Assert.assertEquals("", response.getContentAsString()); // if tag is not blank and direct read is true - when(PropertyUtil.isDirectRead()).thenReturn(true); + propertyUtilMockedStatic.when(PropertyUtil::isDirectRead).thenReturn(true); ConfigInfoTagWrapper configInfoTagWrapper = new ConfigInfoTagWrapper(); configInfoTagWrapper.setDataId("test"); configInfoTagWrapper.setGroup("test"); @@ -210,15 +208,15 @@ public void testDoGetConfigV2() throws Exception { Assert.assertEquals("tag is not blank and direct read is true", response.getContentAsString()); // if tag is not blank and direct read is false - when(PropertyUtil.isDirectRead()).thenReturn(false); + propertyUtilMockedStatic.when(PropertyUtil::isDirectRead).thenReturn(false); response = new MockHttpServletResponse(); - when(DiskUtil.targetTagFile(anyString(), anyString(), anyString(), anyString())).thenReturn(file); + diskUtilMockedStatic.when(() -> DiskUtil.targetTagFile(anyString(), anyString(), anyString(), anyString())).thenReturn(file); actualValue = configServletInner.doGetConfig(request, response, "test", "test", "test", "test", "true", "localhost"); Assert.assertEquals(HttpServletResponse.SC_OK + "", actualValue); Assert.assertEquals("", response.getContentAsString()); // if use auto tag and direct read is true - when(PropertyUtil.isDirectRead()).thenReturn(true); + propertyUtilMockedStatic.when(PropertyUtil::isDirectRead).thenReturn(true); Map tagMd5 = new HashMap<>(); tagMd5.put("auto-tag-test", "auto-tag-test"); cacheItem.setTagMd5(tagMd5); @@ -231,36 +229,34 @@ public void testDoGetConfigV2() throws Exception { Assert.assertEquals("auto tag mode and direct read is true", response.getContentAsString()); // if use auto tag and direct read is false - when(PropertyUtil.isDirectRead()).thenReturn(false); + propertyUtilMockedStatic.when(PropertyUtil::isDirectRead).thenReturn(false); response = new MockHttpServletResponse(); actualValue = configServletInner.doGetConfig(request, response, "test", "test", "test", "", "true", "localhost"); Assert.assertEquals(HttpServletResponse.SC_OK + "", actualValue); Assert.assertEquals("", response.getContentAsString()); - - diskUtil.close(); - configCacheService.close(); - propertyUtil.close(); - + + configCacheServiceMockedStatic.close(); + diskUtilMockedStatic.close(); + propertyUtilMockedStatic.close(); } @Test public void testDoGetConfigV3() throws Exception { + + final MockedStatic configCacheServiceMockedStatic = Mockito.mockStatic(ConfigCacheService.class); - final MockedStatic configCacheService = Mockito.mockStatic(ConfigCacheService.class); - // if lockResult equals 0 - when(ConfigCacheService.tryReadLock(anyString())).thenReturn(0); + configCacheServiceMockedStatic.when(() -> ConfigCacheService.tryReadLock(anyString())).thenReturn(0); MockHttpServletRequest request = new MockHttpServletRequest(); MockHttpServletResponse response = new MockHttpServletResponse(); String actualValue = configServletInner.doGetConfig(request, response, "test", "test", "test", "test", "true", "localhost"); Assert.assertEquals(HttpServletResponse.SC_NOT_FOUND + "", actualValue); // if lockResult less than 0 - when(ConfigCacheService.tryReadLock(anyString())).thenReturn(-1); + configCacheServiceMockedStatic.when(() -> ConfigCacheService.tryReadLock(anyString())).thenReturn(-1); actualValue = configServletInner.doGetConfig(request, response, "test", "test", "test", "test", "true", "localhost"); Assert.assertEquals(HttpServletResponse.SC_CONFLICT + "", actualValue); - - configCacheService.close(); - + + configCacheServiceMockedStatic.close(); } } \ No newline at end of file diff --git a/config/src/test/java/com/alibaba/nacos/config/server/controller/HealthControllerTest.java b/config/src/test/java/com/alibaba/nacos/config/server/controller/HealthControllerTest.java index 5fce7da1dbf..8b36886a75c 100644 --- a/config/src/test/java/com/alibaba/nacos/config/server/controller/HealthControllerTest.java +++ b/config/src/test/java/com/alibaba/nacos/config/server/controller/HealthControllerTest.java @@ -75,6 +75,7 @@ public void setUp() { when(memberManager.getLookup()).thenReturn(memberLookup); when(servletContext.getContextPath()).thenReturn("/nacos"); ReflectionTestUtils.setField(healthController, "memberManager", memberManager); + ReflectionTestUtils.setField(healthController, "dataSourceService", dataSourceService); mockmvc = MockMvcBuilders.standaloneSetup(healthController).build(); } diff --git a/config/src/test/java/com/alibaba/nacos/config/server/controller/HistoryControllerTest.java b/config/src/test/java/com/alibaba/nacos/config/server/controller/HistoryControllerTest.java index e2210c7a374..c4698e5aed1 100644 --- a/config/src/test/java/com/alibaba/nacos/config/server/controller/HistoryControllerTest.java +++ b/config/src/test/java/com/alibaba/nacos/config/server/controller/HistoryControllerTest.java @@ -122,6 +122,7 @@ public void testGetConfigHistoryInfo() throws Exception { configHistoryInfo.setDataId("test"); configHistoryInfo.setGroup("test"); configHistoryInfo.setContent("test"); + configHistoryInfo.setTenant(""); configHistoryInfo.setCreatedTime(new Timestamp(new Date().getTime())); configHistoryInfo.setLastModifiedTime(new Timestamp(new Date().getTime())); @@ -149,6 +150,7 @@ public void testGetPreviousConfigHistoryInfo() throws Exception { configHistoryInfo.setDataId("test"); configHistoryInfo.setGroup("test"); configHistoryInfo.setContent("test"); + configHistoryInfo.setTenant(""); configHistoryInfo.setCreatedTime(new Timestamp(new Date().getTime())); configHistoryInfo.setLastModifiedTime(new Timestamp(new Date().getTime())); diff --git a/config/src/test/java/com/alibaba/nacos/config/server/remote/ConfigChangeBatchListenRequestHandlerTest.java b/config/src/test/java/com/alibaba/nacos/config/server/remote/ConfigChangeBatchListenRequestHandlerTest.java index ac9e6373f6a..af522752cd3 100644 --- a/config/src/test/java/com/alibaba/nacos/config/server/remote/ConfigChangeBatchListenRequestHandlerTest.java +++ b/config/src/test/java/com/alibaba/nacos/config/server/remote/ConfigChangeBatchListenRequestHandlerTest.java @@ -28,12 +28,12 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.InjectMocks; +import org.mockito.MockedStatic; import org.mockito.Mockito; import org.mockito.junit.MockitoJUnitRunner; import org.springframework.test.util.ReflectionTestUtils; import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.Mockito.when; @RunWith(MockitoJUnitRunner.class) public class ConfigChangeBatchListenRequestHandlerTest extends TestCase { @@ -52,18 +52,22 @@ public void setUp() { ReflectionTestUtils.setField(configQueryRequestHandler, "configChangeListenContext", configChangeListenContext); requestMeta = new RequestMeta(); requestMeta.setClientIp("1.1.1.1"); - Mockito.mockStatic(ConfigCacheService.class); } @Test public void testHandle() { + MockedStatic configCacheServiceMockedStatic = Mockito.mockStatic(ConfigCacheService.class); + String dataId = "dataId"; String group = "group"; String tenant = "tenant"; - String groupKey = GroupKey2 - .getKey(dataId, group, tenant); + String groupKey = GroupKey2.getKey(dataId, group, tenant); groupKey = StringPool.get(groupKey); - when(ConfigCacheService.isUptodate(eq(groupKey), Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(false); + + final String groupKeyCopy = groupKey; + configCacheServiceMockedStatic.when( + () -> ConfigCacheService.isUptodate(eq(groupKeyCopy), Mockito.any(), Mockito.any(), Mockito.any())) + .thenReturn(false); ConfigBatchListenRequest configChangeListenRequest = new ConfigBatchListenRequest(); configChangeListenRequest.addConfigListenContext(group, dataId, tenant, " "); try { @@ -79,6 +83,8 @@ public void testHandle() { assertTrue(hasChange); } catch (NacosException e) { e.printStackTrace(); + } finally { + configCacheServiceMockedStatic.close(); } } diff --git a/config/src/test/java/com/alibaba/nacos/config/server/remote/ConfigQueryRequestHandlerTest.java b/config/src/test/java/com/alibaba/nacos/config/server/remote/ConfigQueryRequestHandlerTest.java index c4f13b9c6b8..85a5abd82d9 100644 --- a/config/src/test/java/com/alibaba/nacos/config/server/remote/ConfigQueryRequestHandlerTest.java +++ b/config/src/test/java/com/alibaba/nacos/config/server/remote/ConfigQueryRequestHandlerTest.java @@ -34,6 +34,7 @@ import org.junit.runner.RunWith; import org.mockito.InjectMocks; import org.mockito.Mock; +import org.mockito.MockedStatic; import org.mockito.Mockito; import org.mockito.junit.MockitoJUnitRunner; import org.springframework.core.env.StandardEnvironment; @@ -47,37 +48,43 @@ @RunWith(MockitoJUnitRunner.class) public class ConfigQueryRequestHandlerTest { - + @InjectMocks private ConfigQueryRequestHandler configQueryRequestHandler; - + @Mock private PersistService persistService; - + @Mock private File file; - + @Before public void setUp() throws IOException { EnvUtil.setEnvironment(new StandardEnvironment()); - Mockito.mockStatic(ConfigCacheService.class); - Mockito.mockStatic(PropertyUtil.class); - Mockito.mockStatic(FileUtils.class); - Mockito.mockStatic(DiskUtil.class); + } + + @Test + public void testHandle() throws NacosException { + final MockedStatic configCacheServiceMockedStatic = Mockito.mockStatic(ConfigCacheService.class); + final MockedStatic fileUtilsMockedStatic = Mockito.mockStatic(FileUtils.class); + final MockedStatic diskUtilMockedStatic = Mockito.mockStatic(DiskUtil.class); + MockedStatic propertyUtilMockedStatic = Mockito.mockStatic(PropertyUtil.class); + + propertyUtilMockedStatic.when(PropertyUtil::isDirectRead).thenReturn(false); + ReflectionTestUtils.setField(configQueryRequestHandler, "persistService", persistService); final String groupKey = GroupKey2.getKey("dataId", "group", ""); - when(ConfigCacheService.tryReadLock(groupKey)).thenReturn(1); - when(DiskUtil.targetFile(Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(file); - when(FileUtils.readFileToString(file, ENCODE)).thenReturn("content"); + configCacheServiceMockedStatic.when(() -> ConfigCacheService.tryReadLock(groupKey)).thenReturn(1); + diskUtilMockedStatic.when(() -> DiskUtil.targetFile(Mockito.any(), Mockito.any(), Mockito.any())) + .thenReturn(file); + fileUtilsMockedStatic.when(() -> FileUtils.readFileToString(file, ENCODE)).thenReturn("content"); when(file.exists()).thenReturn(true); CacheItem cacheItem = new CacheItem(groupKey); cacheItem.setMd5("1"); cacheItem.setLastModifiedTs(1L); - when(ConfigCacheService.getContentCache(Mockito.any())).thenReturn(cacheItem); - } - - @Test - public void testHandle() throws NacosException { + configCacheServiceMockedStatic.when(() -> ConfigCacheService.getContentCache(Mockito.any())) + .thenReturn(cacheItem); + ConfigQueryRequest configQueryRequest = new ConfigQueryRequest(); configQueryRequest.setDataId("dataId"); configQueryRequest.setGroup("group"); @@ -85,6 +92,11 @@ public void testHandle() throws NacosException { requestMeta.setClientIp("127.0.0.1"); ConfigQueryResponse response = configQueryRequestHandler.handle(configQueryRequest, requestMeta); Assert.assertEquals(response.getContent(), "content"); + + configCacheServiceMockedStatic.close(); + fileUtilsMockedStatic.close(); + diskUtilMockedStatic.close(); + propertyUtilMockedStatic.close(); } } \ No newline at end of file diff --git a/config/src/test/java/com/alibaba/nacos/config/server/service/capacity/GroupCapacityPersistServiceTest.java b/config/src/test/java/com/alibaba/nacos/config/server/service/capacity/GroupCapacityPersistServiceTest.java index b4098032b2c..018f8ce2bcb 100644 --- a/config/src/test/java/com/alibaba/nacos/config/server/service/capacity/GroupCapacityPersistServiceTest.java +++ b/config/src/test/java/com/alibaba/nacos/config/server/service/capacity/GroupCapacityPersistServiceTest.java @@ -28,6 +28,7 @@ import org.mockito.ArgumentMatcher; import org.mockito.InjectMocks; import org.mockito.Mock; +import org.mockito.MockedStatic; import org.mockito.Mockito; import org.mockito.stubbing.Answer; import org.springframework.jdbc.core.JdbcTemplate; @@ -194,6 +195,7 @@ public void testDecrementUsage() { @Test public void testUpdateGroupCapacity() { + final MockedStatic timeUtilsMockedStatic = Mockito.mockStatic(TimeUtils.class); List argList = CollectionUtils.list(); @@ -210,8 +212,8 @@ public void testUpdateGroupCapacity() { argList.add(maxAggrSize); Timestamp timestamp = new Timestamp(System.currentTimeMillis()); - Mockito.mockStatic(TimeUtils.class); - when(TimeUtils.getCurrentTime()).thenReturn(timestamp); + + timeUtilsMockedStatic.when(TimeUtils::getCurrentTime).thenReturn(timestamp); argList.add(timestamp); String group = "test"; @@ -230,6 +232,7 @@ public void testUpdateGroupCapacity() { return 0; }); Assert.assertTrue(service.updateGroupCapacity(group, quota, maxSize, maxAggrCount, maxAggrSize)); + timeUtilsMockedStatic.close(); } @Test diff --git a/config/src/test/java/com/alibaba/nacos/config/server/service/capacity/TenantCapacityPersistServiceTest.java b/config/src/test/java/com/alibaba/nacos/config/server/service/capacity/TenantCapacityPersistServiceTest.java index 793e81a2434..17cc4a6ed0c 100644 --- a/config/src/test/java/com/alibaba/nacos/config/server/service/capacity/TenantCapacityPersistServiceTest.java +++ b/config/src/test/java/com/alibaba/nacos/config/server/service/capacity/TenantCapacityPersistServiceTest.java @@ -27,6 +27,7 @@ import org.mockito.ArgumentMatcher; import org.mockito.InjectMocks; import org.mockito.Mock; +import org.mockito.MockedStatic; import org.mockito.Mockito; import org.mockito.stubbing.Answer; import org.springframework.jdbc.core.JdbcTemplate; @@ -157,6 +158,7 @@ public void testDecrementUsage() { @Test public void testUpdateTenantCapacity() { + final MockedStatic timeUtilsMockedStatic = Mockito.mockStatic(TimeUtils.class); List argList = CollectionUtils.list(); @@ -173,8 +175,7 @@ public void testUpdateTenantCapacity() { argList.add(maxAggrSize); Timestamp timestamp = new Timestamp(System.currentTimeMillis()); - Mockito.mockStatic(TimeUtils.class); - when(TimeUtils.getCurrentTime()).thenReturn(timestamp); + timeUtilsMockedStatic.when(TimeUtils::getCurrentTime).thenReturn(timestamp); argList.add(timestamp); String tenant = "test"; @@ -193,11 +194,12 @@ public void testUpdateTenantCapacity() { return 0; }); Assert.assertTrue(service.updateTenantCapacity(tenant, quota, maxSize, maxAggrCount, maxAggrSize)); + + timeUtilsMockedStatic.close(); } @Test public void testUpdateQuota() { - List argList = CollectionUtils.list(); Integer quota = 2; diff --git a/config/src/test/java/com/alibaba/nacos/config/server/service/datasource/DynamicDataSourceTest.java b/config/src/test/java/com/alibaba/nacos/config/server/service/datasource/DynamicDataSourceTest.java index 0899240e0bc..1893f00c529 100644 --- a/config/src/test/java/com/alibaba/nacos/config/server/service/datasource/DynamicDataSourceTest.java +++ b/config/src/test/java/com/alibaba/nacos/config/server/service/datasource/DynamicDataSourceTest.java @@ -23,14 +23,13 @@ import org.junit.runner.RunWith; import org.mockito.InjectMocks; import org.mockito.Mock; +import org.mockito.MockedStatic; import org.mockito.Mockito; import org.springframework.mock.web.MockServletContext; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.util.ReflectionTestUtils; -import static org.mockito.Mockito.when; - @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = MockServletContext.class) public class DynamicDataSourceTest { @@ -53,14 +52,15 @@ public void setUp() { @Test public void testGetDataSource() { + MockedStatic propertyUtilMockedStatic = Mockito.mockStatic(PropertyUtil.class); - Mockito.mockStatic(PropertyUtil.class); - - when(PropertyUtil.isEmbeddedStorage()).thenReturn(true); + propertyUtilMockedStatic.when(PropertyUtil::isEmbeddedStorage).thenReturn(true); Assert.assertTrue(dataSource.getDataSource() instanceof LocalDataSourceServiceImpl); - when(PropertyUtil.isEmbeddedStorage()).thenReturn(false); + propertyUtilMockedStatic.when(PropertyUtil::isEmbeddedStorage).thenReturn(false); Assert.assertTrue(dataSource.getDataSource() instanceof ExternalDataSourceServiceImpl); + + propertyUtilMockedStatic.close(); } } diff --git a/config/src/test/java/com/alibaba/nacos/config/server/service/dump/DumpServiceTest.java b/config/src/test/java/com/alibaba/nacos/config/server/service/dump/DumpServiceTest.java index fb4756b579a..fb476af286a 100644 --- a/config/src/test/java/com/alibaba/nacos/config/server/service/dump/DumpServiceTest.java +++ b/config/src/test/java/com/alibaba/nacos/config/server/service/dump/DumpServiceTest.java @@ -16,6 +16,8 @@ package com.alibaba.nacos.config.server.service.dump; +import com.alibaba.nacos.config.server.service.datasource.DynamicDataSource; +import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; @@ -23,6 +25,7 @@ import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.test.util.ReflectionTestUtils; @RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest @@ -33,6 +36,13 @@ public class DumpServiceTest { @Autowired DumpService service; + @BeforeClass + public static void setUp() { + + ReflectionTestUtils.setField(DynamicDataSource.getInstance(), "localDataSourceService", null); + ReflectionTestUtils.setField(DynamicDataSource.getInstance(), "basicDataSourceService", null); + } + @Test public void init() throws Throwable { service.init(); diff --git a/config/src/test/java/com/alibaba/nacos/config/server/utils/DiskUtilsTest.java b/config/src/test/java/com/alibaba/nacos/config/server/utils/DiskUtilsTest.java index 57c90c4dcff..cb61c9a8ac6 100644 --- a/config/src/test/java/com/alibaba/nacos/config/server/utils/DiskUtilsTest.java +++ b/config/src/test/java/com/alibaba/nacos/config/server/utils/DiskUtilsTest.java @@ -23,15 +23,12 @@ import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; -import org.junit.runner.RunWith; import org.mockito.MockedStatic; import org.mockito.Mockito; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import java.io.File; import java.io.IOException; -@RunWith(SpringJUnit4ClassRunner.class) public class DiskUtilsTest { static MockedStatic fileUtils; @@ -100,13 +97,13 @@ public void testRemoveHeartHeat() { @Test public void testTargetFile() { File file = DiskUtil.targetFile("test1", "test2", "test3"); - String[] arr = file.getPath().split("\\\\"); + String[] arr = file.getPath().split(File.separator); Assert.assertEquals("test1", arr[arr.length - 1]); Assert.assertEquals("test2", arr[arr.length - 2]); Assert.assertEquals("test3", arr[arr.length - 3]); File file2 = DiskUtil.targetFile("test1", "test2", ""); - String[] arr2 = file2.getPath().split("\\\\"); + String[] arr2 = file2.getPath().split(File.separator); Assert.assertEquals("test1", arr2[arr2.length - 1]); Assert.assertEquals("test2", arr2[arr2.length - 2]); Assert.assertEquals("config-data", arr2[arr2.length - 3]); @@ -115,13 +112,13 @@ public void testTargetFile() { @Test public void testTargetBetaFile() { File file = DiskUtil.targetBetaFile("test1", "test2", "test3"); - String[] arr = file.getPath().split("\\\\"); + String[] arr = file.getPath().split(File.separator); Assert.assertEquals("test1", arr[arr.length - 1]); Assert.assertEquals("test2", arr[arr.length - 2]); Assert.assertEquals("test3", arr[arr.length - 3]); File file2 = DiskUtil.targetBetaFile("test1", "test2", ""); - String[] arr2 = file2.getPath().split("\\\\"); + String[] arr2 = file2.getPath().split(File.separator); Assert.assertEquals("test1", arr2[arr2.length - 1]); Assert.assertEquals("test2", arr2[arr2.length - 2]); Assert.assertEquals("beta-data", arr2[arr2.length - 3]); @@ -130,14 +127,14 @@ public void testTargetBetaFile() { @Test public void testTargetTagFile() { File file = DiskUtil.targetTagFile("test1", "test2", "test3", "tag"); - String[] arr = file.getPath().split("\\\\"); + String[] arr = file.getPath().split(File.separator); Assert.assertEquals("tag", arr[arr.length - 1]); Assert.assertEquals("test1", arr[arr.length - 2]); Assert.assertEquals("test2", arr[arr.length - 3]); Assert.assertEquals("test3", arr[arr.length - 4]); File file2 = DiskUtil.targetTagFile("test1", "test2", "", "tag"); - String[] arr2 = file2.getPath().split("\\\\"); + String[] arr2 = file2.getPath().split(File.separator); Assert.assertEquals("tag", arr2[arr2.length - 1]); Assert.assertEquals("test1", arr2[arr2.length - 2]); Assert.assertEquals("test2", arr2[arr2.length - 3]); @@ -162,7 +159,7 @@ public void testGetLocalConfigMd5() throws IOException { @Test public void testHeartBeatFile() { File file = DiskUtil.heartBeatFile(); - String[] arr = file.getPath().split("\\\\"); + String[] arr = file.getPath().split(File.separator); Assert.assertEquals("heartBeat.txt", arr[arr.length - 1]); Assert.assertEquals("status", arr[arr.length - 2]); Assert.assertEquals("nacos", arr[arr.length - 3]); @@ -171,7 +168,7 @@ public void testHeartBeatFile() { @Test public void testRelativePath() { String relativePath = DiskUtil.relativePath("test1", "test2"); - String[] arr = relativePath.split("/"); + String[] arr = relativePath.split(File.separator); Assert.assertEquals("test2", arr[arr.length - 1]); Assert.assertEquals("test1", arr[arr.length - 2]); } diff --git a/config/src/test/java/com/alibaba/nacos/config/server/utils/MD5UtilTest.java b/config/src/test/java/com/alibaba/nacos/config/server/utils/MD5UtilTest.java index a66f4f3c691..72e628aef06 100644 --- a/config/src/test/java/com/alibaba/nacos/config/server/utils/MD5UtilTest.java +++ b/config/src/test/java/com/alibaba/nacos/config/server/utils/MD5UtilTest.java @@ -38,18 +38,17 @@ import java.util.Map; import static org.mockito.ArgumentMatchers.anyString; -import static org.mockito.Mockito.when; public class MD5UtilTest { @Test public void testCompareMd5() { - - final MockedStatic configCacheServiceMockedStatic = Mockito - .mockStatic(ConfigCacheService.class); - - when(ConfigCacheService.isUptodate(anyString(), anyString(), anyString(), anyString())).thenReturn(false); - + final MockedStatic configCacheServiceMockedStatic = Mockito.mockStatic(ConfigCacheService.class); + + configCacheServiceMockedStatic.when( + () -> ConfigCacheService.isUptodate(anyString(), anyString(), anyString(), anyString())) + .thenReturn(false); + Map clientMd5Map = new HashMap<>(); clientMd5Map.put("test", "test"); @@ -61,13 +60,13 @@ public void testCompareMd5() { Assert.assertEquals(1, changedGroupKeys.size()); Assert.assertEquals("test", changedGroupKeys.get(0)); - + configCacheServiceMockedStatic.close(); + } @Test public void testCompareMd5OldResult() { - final MockedStatic groupKey2MockedStatic = Mockito.mockStatic(GroupKey2.class); List changedGroupKeys = new ArrayList<>(); @@ -77,18 +76,18 @@ public void testCompareMd5OldResult() { arr[0] = "test0"; arr[1] = "test1"; arr[2] = "test2"; - when(GroupKey2.parseKey(anyString())).thenReturn(arr); + groupKey2MockedStatic.when(() -> GroupKey2.parseKey(anyString())).thenReturn(arr); String actualValue = MD5Util.compareMd5OldResult(changedGroupKeys); Assert.assertEquals("test0:test1;", actualValue); - + groupKey2MockedStatic.close(); + } @Test public void testCompareMd5ResultString() { - final MockedStatic groupKey2MockedStatic = Mockito.mockStatic(GroupKey2.class); List changedGroupKeys = new ArrayList<>(); @@ -98,15 +97,15 @@ public void testCompareMd5ResultString() { arr[0] = "test0"; arr[1] = "test1"; arr[2] = "test2"; - when(GroupKey2.parseKey(anyString())).thenReturn(arr); + groupKey2MockedStatic.when(() -> GroupKey2.parseKey(anyString())).thenReturn(arr); try { String actualValue = MD5Util.compareMd5ResultString(changedGroupKeys); Assert.assertEquals("test0%02test1%02test2%01", actualValue); } catch (IOException e) { - System.out.println(e.toString()); + System.out.println(e); } - + groupKey2MockedStatic.close(); } @@ -142,7 +141,7 @@ public void testToStringV1() { String actualValue = MD5Util.toString(input, "UTF-8"); Assert.assertEquals("test", actualValue); } catch (IOException e) { - System.out.println(e.toString()); + System.out.println(e); } } @@ -154,7 +153,7 @@ public void testToStringV2() { String actualValue = MD5Util.toString(reader); Assert.assertEquals("test", actualValue); } catch (IOException e) { - System.out.println(e.toString()); + System.out.println(e); } } @@ -171,7 +170,7 @@ public void testCopy() { Assert.assertEquals(content, output.toString()); } catch (IOException e) { - System.out.println(e.toString()); + System.out.println(e); } } diff --git a/config/src/test/java/com/alibaba/nacos/config/server/utils/ParamUtilsTest.java b/config/src/test/java/com/alibaba/nacos/config/server/utils/ParamUtilsTest.java index a20fd5c8249..7672dca5d53 100644 --- a/config/src/test/java/com/alibaba/nacos/config/server/utils/ParamUtilsTest.java +++ b/config/src/test/java/com/alibaba/nacos/config/server/utils/ParamUtilsTest.java @@ -19,13 +19,10 @@ import com.alibaba.nacos.api.exception.NacosException; import org.junit.Assert; import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import java.util.HashMap; import java.util.Map; -@RunWith(SpringJUnit4ClassRunner.class) public class ParamUtilsTest { @Test diff --git a/config/src/test/java/com/alibaba/nacos/config/server/utils/PropertyUtilTest.java b/config/src/test/java/com/alibaba/nacos/config/server/utils/PropertyUtilTest.java index 0a907383ec3..721c4ab18f6 100644 --- a/config/src/test/java/com/alibaba/nacos/config/server/utils/PropertyUtilTest.java +++ b/config/src/test/java/com/alibaba/nacos/config/server/utils/PropertyUtilTest.java @@ -27,7 +27,6 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.Mockito.when; @RunWith(SpringJUnit4ClassRunner.class) public class PropertyUtilTest { @@ -37,19 +36,24 @@ public class PropertyUtilTest { @Test public void testGetPropertyV1() { - final MockedStatic envUtilMockedStatic = Mockito.mockStatic(EnvUtil.class); + MockedStatic envUtilMockedStatic = Mockito.mockStatic(EnvUtil.class); + EnvUtil.setEnvironment(configurableEnvironment); - when(EnvUtil.getProperty(eq("test"))).thenReturn("test"); + envUtilMockedStatic.when(() -> EnvUtil.getProperty(eq("test"))).thenReturn("test"); Assert.assertEquals("test", new PropertyUtil().getProperty("test")); + envUtilMockedStatic.close(); } @Test public void testGetPropertyV2() { - final MockedStatic envUtilMockedStatic = Mockito.mockStatic(EnvUtil.class); + MockedStatic envUtilMockedStatic = Mockito.mockStatic(EnvUtil.class); + EnvUtil.setEnvironment(configurableEnvironment); - when(EnvUtil.getProperty(eq("test"), eq("default"))).thenReturn("default"); + envUtilMockedStatic.when(() -> EnvUtil.getProperty(eq("test"), eq("default"))).thenReturn("default"); Assert.assertEquals("default", new PropertyUtil().getProperty("test", "default")); + envUtilMockedStatic.close(); } + } diff --git a/config/src/test/java/com/alibaba/nacos/config/server/utils/ResponseUtilTest.java b/config/src/test/java/com/alibaba/nacos/config/server/utils/ResponseUtilTest.java index 0d00d62b34d..abb7e6efca3 100644 --- a/config/src/test/java/com/alibaba/nacos/config/server/utils/ResponseUtilTest.java +++ b/config/src/test/java/com/alibaba/nacos/config/server/utils/ResponseUtilTest.java @@ -24,13 +24,16 @@ public class ResponseUtilTest { + String lineSeparator = java.security.AccessController.doPrivileged( + new sun.security.action.GetPropertyAction("line.separator")); + @Test public void testWriteErrMsg() { MockHttpServletResponse response = new MockHttpServletResponse(); ResponseUtil.writeErrMsg(response, 404, "test"); Assert.assertEquals(404, response.getStatus()); try { - Assert.assertEquals("test\r\n", response.getContentAsString()); + Assert.assertEquals("test" + lineSeparator, response.getContentAsString()); } catch (UnsupportedEncodingException e) { System.out.println(e.toString()); }