This is an automated email from the ASF dual-hosted git repository.
jianbin pushed a commit to branch 2.x
in repository https://gitbox.apache.org/repos/asf/incubator-seata.git
The following commit(s) were added to refs/heads/2.x by this push:
new 9b2158b58b test: Enhance NacosRegistryServiceImplTest with additional
mocks for service name group and cluster (#8003)
9b2158b58b is described below
commit 9b2158b58b78ee8ad8d900d0ceaf818e0f175194
Author: funkye <[email protected]>
AuthorDate: Sun Mar 1 15:05:06 2026 +0800
test: Enhance NacosRegistryServiceImplTest with additional mocks for
service name group and cluster (#8003)
---
.mvn/wrapper/maven-wrapper.properties | 2 +-
changes/en-us/2.x.md | 2 ++
changes/zh-cn/2.x.md | 3 +++
.../registry/nacos/NacosRegistryServiceImpl.java | 30 ++++++++++++----------
.../nacos/NacosRegistryServiceImplTest.java | 9 +++++++
.../seata/server/coordinator/AbstractCoreTest.java | 3 ++-
.../server/coordinator/RaftCoordinatorTest.java | 3 ++-
7 files changed, 36 insertions(+), 16 deletions(-)
diff --git a/.mvn/wrapper/maven-wrapper.properties
b/.mvn/wrapper/maven-wrapper.properties
index 6246813786..659ff0ca4c 100755
--- a/.mvn/wrapper/maven-wrapper.properties
+++ b/.mvn/wrapper/maven-wrapper.properties
@@ -14,5 +14,5 @@
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
-distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.6.3/apache-maven-3.6.3-bin.zip
+distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.9.5/apache-maven-3.9.5-bin.zip
wrapperUrl=https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.1.1/maven-wrapper-3.1.1.jar
diff --git a/changes/en-us/2.x.md b/changes/en-us/2.x.md
index 975cac02a7..192d97de1e 100644
--- a/changes/en-us/2.x.md
+++ b/changes/en-us/2.x.md
@@ -49,7 +49,9 @@ Add changes here for all PR submitted to the 2.x branch.
### security:
### test:
+
- [[#7962](https://github.com/apache/incubator-seata/pull/7962)] add unit
tests for NacosRegistryProvider and NacosRegistryServiceImpl
+- [[#8003](https://github.com/apache/incubator-seata/pull/8003)] Enhance
NacosRegistryServiceImplTest with additional mocks for service name group and
cluster
### refactor:
diff --git a/changes/zh-cn/2.x.md b/changes/zh-cn/2.x.md
index 0f404b2b2c..3da705d6b3 100644
--- a/changes/zh-cn/2.x.md
+++ b/changes/zh-cn/2.x.md
@@ -54,7 +54,10 @@
### test:
+
- [[#7962](https://github.com/apache/incubator-seata/pull/7962)] 为
NacosRegistryProvider 和 NacosRegistryServiceImpl 添加单元测试用例
+- [[#8003](https://github.com/apache/incubator-seata/pull/8003)] 为
NacosRegistryServiceImplTest 增加服务名称、分组和集群的额外模拟
+
### refactor:
diff --git
a/discovery/seata-discovery-nacos/src/main/java/org/apache/seata/discovery/registry/nacos/NacosRegistryServiceImpl.java
b/discovery/seata-discovery-nacos/src/main/java/org/apache/seata/discovery/registry/nacos/NacosRegistryServiceImpl.java
index 3edbd120c7..d8649de50c 100644
---
a/discovery/seata-discovery-nacos/src/main/java/org/apache/seata/discovery/registry/nacos/NacosRegistryServiceImpl.java
+++
b/discovery/seata-discovery-nacos/src/main/java/org/apache/seata/discovery/registry/nacos/NacosRegistryServiceImpl.java
@@ -72,7 +72,11 @@ public class NacosRegistryServiceImpl implements
RegistryService<EventListener>
private static final String PUBLIC_NAMING_ADDRESS_PREFIX = "public_";
private static final String PUBLIC_NAMING_SERVICE_META_IP_KEY = "publicIp";
private static final String PUBLIC_NAMING_SERVICE_META_PORT_KEY =
"publicPort";
- private static final Configuration FILE_CONFIG =
ConfigurationFactory.CURRENT_FILE_INSTANCE;
+ // Use a method to retrieve file config dynamically to support test
mocking on Java 17+
+ private static Configuration getFileConfig() {
+ return ConfigurationFactory.CURRENT_FILE_INSTANCE;
+ }
+
private static volatile NamingService naming;
private static final ConcurrentMap<String, List<EventListener>>
LISTENER_SERVICE_MAP = new ConcurrentHashMap<>();
private static final ConcurrentMap<String, List<InetSocketAddress>>
CLUSTER_ADDRESS_MAP = new ConcurrentHashMap<>();
@@ -86,7 +90,7 @@ public class NacosRegistryServiceImpl implements
RegistryService<EventListener>
private String transactionServiceGroup;
private NacosRegistryServiceImpl() {
- String configForNacosSLB =
FILE_CONFIG.getConfig(getNacosUrlPatternOfSLB());
+ String configForNacosSLB =
getFileConfig().getConfig(getNacosUrlPatternOfSLB());
Pattern patternOfNacosRegistryForSLB =
StringUtils.isBlank(configForNacosSLB)
? DEFAULT_SLB_REGISTRY_PATTERN
: Pattern.compile(configForNacosSLB);
@@ -278,7 +282,7 @@ public class NacosRegistryServiceImpl implements
RegistryService<EventListener>
if (System.getProperty(PRO_SERVER_ADDR_KEY) != null) {
properties.setProperty(PRO_SERVER_ADDR_KEY,
System.getProperty(PRO_SERVER_ADDR_KEY));
} else {
- String address = FILE_CONFIG.getConfig(getNacosAddrFileKey());
+ String address = getFileConfig().getConfig(getNacosAddrFileKey());
if (address != null) {
properties.setProperty(PRO_SERVER_ADDR_KEY, address);
}
@@ -286,7 +290,7 @@ public class NacosRegistryServiceImpl implements
RegistryService<EventListener>
if (System.getProperty(PRO_NAMESPACE_KEY) != null) {
properties.setProperty(PRO_NAMESPACE_KEY,
System.getProperty(PRO_NAMESPACE_KEY));
} else {
- String namespace =
FILE_CONFIG.getConfig(getNacosNameSpaceFileKey());
+ String namespace =
getFileConfig().getConfig(getNacosNameSpaceFileKey());
if (namespace == null) {
namespace = DEFAULT_NAMESPACE;
}
@@ -297,7 +301,7 @@ public class NacosRegistryServiceImpl implements
RegistryService<EventListener>
}
String contextPath =
StringUtils.isNotBlank(System.getProperty(CONTEXT_PATH))
? System.getProperty(CONTEXT_PATH)
- : FILE_CONFIG.getConfig(getNacosContextPathKey());
+ : getFileConfig().getConfig(getNacosContextPathKey());
if (StringUtils.isNotBlank(contextPath)) {
properties.setProperty(CONTEXT_PATH, contextPath);
}
@@ -315,11 +319,11 @@ public class NacosRegistryServiceImpl implements
RegistryService<EventListener>
private static boolean initNacosAuthProperties(Properties
sourceProperties) {
String userName = StringUtils.isNotBlank(System.getProperty(USER_NAME))
? System.getProperty(USER_NAME)
- : FILE_CONFIG.getConfig(getNacosUserName());
+ : getFileConfig().getConfig(getNacosUserName());
if (StringUtils.isNotBlank(userName)) {
String password =
StringUtils.isNotBlank(System.getProperty(PASSWORD))
? System.getProperty(PASSWORD)
- : FILE_CONFIG.getConfig(getNacosPassword());
+ : getFileConfig().getConfig(getNacosPassword());
if (StringUtils.isNotBlank(password)) {
sourceProperties.setProperty(USER_NAME, userName);
sourceProperties.setProperty(PASSWORD, password);
@@ -329,14 +333,14 @@ public class NacosRegistryServiceImpl implements
RegistryService<EventListener>
} else {
String accessKey =
StringUtils.isNotBlank(System.getProperty(ACCESS_KEY))
? System.getProperty(ACCESS_KEY)
- : FILE_CONFIG.getConfig(getNacosAccessKey());
+ : getFileConfig().getConfig(getNacosAccessKey());
String ramRoleName =
StringUtils.isNotBlank(System.getProperty(RAM_ROLE_NAME_KEY))
? System.getProperty(RAM_ROLE_NAME_KEY)
- : FILE_CONFIG.getConfig(getNacosRamRoleNameKey());
+ : getFileConfig().getConfig(getNacosRamRoleNameKey());
if (StringUtils.isNotBlank(accessKey)) {
String secretKey =
StringUtils.isNotBlank(System.getProperty(SECRET_KEY))
? System.getProperty(SECRET_KEY)
- : FILE_CONFIG.getConfig(getNacosSecretKey());
+ : getFileConfig().getConfig(getNacosSecretKey());
if (StringUtils.isNotBlank(secretKey)) {
sourceProperties.put(ACCESS_KEY, accessKey);
sourceProperties.put(SECRET_KEY, secretKey);
@@ -353,15 +357,15 @@ public class NacosRegistryServiceImpl implements
RegistryService<EventListener>
}
private static String getClusterName() {
- return FILE_CONFIG.getConfig(getNacosClusterFileKey(),
DEFAULT_CLUSTER);
+ return getFileConfig().getConfig(getNacosClusterFileKey(),
DEFAULT_CLUSTER);
}
private static String getServiceName() {
- return FILE_CONFIG.getConfig(getNacosApplicationFileKey(),
DEFAULT_APPLICATION);
+ return getFileConfig().getConfig(getNacosApplicationFileKey(),
DEFAULT_APPLICATION);
}
private static String getServiceGroup() {
- return FILE_CONFIG.getConfig(getNacosApplicationGroupKey(),
DEFAULT_GROUP);
+ return getFileConfig().getConfig(getNacosApplicationGroupKey(),
DEFAULT_GROUP);
}
private static String getNacosAddrFileKey() {
diff --git
a/discovery/seata-discovery-nacos/src/test/java/org/apache/seata/discovery/registry/nacos/NacosRegistryServiceImplTest.java
b/discovery/seata-discovery-nacos/src/test/java/org/apache/seata/discovery/registry/nacos/NacosRegistryServiceImplTest.java
index 51d703a1f6..fcef4e3c9d 100644
---
a/discovery/seata-discovery-nacos/src/test/java/org/apache/seata/discovery/registry/nacos/NacosRegistryServiceImplTest.java
+++
b/discovery/seata-discovery-nacos/src/test/java/org/apache/seata/discovery/registry/nacos/NacosRegistryServiceImplTest.java
@@ -88,11 +88,20 @@ public class NacosRegistryServiceImplTest {
when(mockedCurrentNacosConfiguration.getConfig(SLB_PATTERN_KEY)).thenReturn("");
when(mockedCurrentNacosConfiguration.getConfig(SERVER_ADDR_KEY)).thenReturn("127.0.0.1");
+ // Mock for getServiceName() -> registry.nacos.application
+
when(mockedCurrentNacosConfiguration.getConfig(APPLICATION_KEY)).thenReturn(NACOS_MOCKED_APPLICATION);
when(mockedCurrentNacosConfiguration.getConfig(APPLICATION_KEY,
"seata-server"))
.thenReturn(NACOS_MOCKED_APPLICATION);
+
+ // Mock for getServiceGroup() -> registry.nacos.group
+
when(mockedCurrentNacosConfiguration.getConfig(GROUP_KEY)).thenReturn(NACOS_MOCKED_GROUP);
when(mockedCurrentNacosConfiguration.getConfig(GROUP_KEY,
"DEFAULT_GROUP"))
.thenReturn(NACOS_MOCKED_GROUP);
+
+ // Mock for getClusterName() -> registry.nacos.cluster
+
when(mockedCurrentNacosConfiguration.getConfig(CLUSTER_KEY)).thenReturn(NACOS_MOCKED_CLUSTER);
when(mockedCurrentNacosConfiguration.getConfig(CLUSTER_KEY,
"default")).thenReturn(NACOS_MOCKED_CLUSTER);
+
when(mockedCurrentNacosConfiguration.getConfig(CONTEXT_PATH_KEY)).thenReturn("/foo");
nacosRegistryService = NacosRegistryServiceImpl.getInstance();
diff --git
a/server/src/test/java/org/apache/seata/server/coordinator/AbstractCoreTest.java
b/server/src/test/java/org/apache/seata/server/coordinator/AbstractCoreTest.java
index 6462ffcb57..23ae0aafb3 100644
---
a/server/src/test/java/org/apache/seata/server/coordinator/AbstractCoreTest.java
+++
b/server/src/test/java/org/apache/seata/server/coordinator/AbstractCoreTest.java
@@ -32,6 +32,7 @@ import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
+import org.springframework.context.ApplicationContext;
import java.util.Collection;
@@ -53,7 +54,7 @@ public class AbstractCoreTest extends BaseSpringBootTest {
private static final String applicationData = "{\"data\":\"test\"}";
@BeforeAll
- public static void initSessionManager() throws Exception {
+ public static void initSessionManager(ApplicationContext context) throws
Exception {
SessionHolder.init(SessionMode.FILE);
remotingServer = new DefaultCoordinatorTest.MockServerMessageSender();
abstractCore = new TestableAbstractCore(remotingServer);
diff --git
a/server/src/test/java/org/apache/seata/server/coordinator/RaftCoordinatorTest.java
b/server/src/test/java/org/apache/seata/server/coordinator/RaftCoordinatorTest.java
index a7a95d3500..6f8f9a990b 100644
---
a/server/src/test/java/org/apache/seata/server/coordinator/RaftCoordinatorTest.java
+++
b/server/src/test/java/org/apache/seata/server/coordinator/RaftCoordinatorTest.java
@@ -28,6 +28,7 @@ import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
+import org.springframework.context.ApplicationContext;
/**
* The type Raft coordinator test.
@@ -41,7 +42,7 @@ public class RaftCoordinatorTest extends BaseSpringBootTest {
private static final String ANOTHER_GROUP = "another_group";
@BeforeAll
- public static void setup() throws Exception {
+ public static void setup(ApplicationContext context) throws Exception {
SessionHolder.init(SessionMode.FILE);
remotingServer = new DefaultCoordinatorTest.MockServerMessageSender();
raftCoordinator = new RaftCoordinator(remotingServer);
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]