You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@seata.apache.org by ji...@apache.org on 2025/11/20 02:09:11 UTC

(incubator-seata) branch 2.x updated: test: fix the NPE on ConsulConfigurationTest method (#7796)

This is an automated email from the ASF dual-hosted git repository.

jimin 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 1b63dd0f0c test: fix the NPE on ConsulConfigurationTest method (#7796)
1b63dd0f0c is described below

commit 1b63dd0f0c1734968e3ffe0f66a4a2405ab2571f
Author: lokidundun <lo...@qq.com>
AuthorDate: Thu Nov 20 10:09:07 2025 +0800

    test: fix the NPE on ConsulConfigurationTest method (#7796)
---
 changes/en-us/2.x.md                               |  1 +
 changes/zh-cn/2.x.md                               |  1 +
 .../config/consul/ConsulConfigurationTest.java     | 24 +++++++++++++++-------
 3 files changed, 19 insertions(+), 7 deletions(-)

diff --git a/changes/en-us/2.x.md b/changes/en-us/2.x.md
index 5e511e0a3a..9028c50d32 100644
--- a/changes/en-us/2.x.md
+++ b/changes/en-us/2.x.md
@@ -52,6 +52,7 @@ Add changes here for all PR submitted to the 2.x branch.
 - [[#7761](https://github.com/apache/incubator-seata/pull/7761)] special handling is applied to the Byte[] type to ensure the correct primary key value
 - [[#7771](https://github.com/apache/incubator-seata/pull/7771)] Shentongdata xa mode should be hold the same connection
 - [[#7785](https://github.com/apache/incubator-seata/pull/7785)] fix the failure test
+- [[#7796](https://github.com/apache/incubator-seata/pull/7796)] fix the NPE on ConsulConfigurationTest method
 
 
 ### optimize:
diff --git a/changes/zh-cn/2.x.md b/changes/zh-cn/2.x.md
index 112388d45e..adddd6f180 100644
--- a/changes/zh-cn/2.x.md
+++ b/changes/zh-cn/2.x.md
@@ -52,6 +52,7 @@
 - [[#7761](https://github.com/apache/incubator-seata/pull/7761)] 对 Byte[] 类型进行了特殊处理,以确保主键值正确
 - [[#7771](https://github.com/apache/incubator-seata/pull/7771)] 确保神通XA模式相同的事务使用相同的XAConnection
 - [[#7785](https://github.com/apache/incubator-seata/pull/7785)] 修复失败的测试方法
+- [[#7796](https://github.com/apache/incubator-seata/pull/7796)] 修复 Consul 监听器空值 NPE 问题
 
 
 ### optimize:
diff --git a/config/seata-config-consul/src/test/java/org/apache/seata/config/consul/ConsulConfigurationTest.java b/config/seata-config-consul/src/test/java/org/apache/seata/config/consul/ConsulConfigurationTest.java
index 6dc8e84933..650ec33f23 100644
--- a/config/seata-config-consul/src/test/java/org/apache/seata/config/consul/ConsulConfigurationTest.java
+++ b/config/seata-config-consul/src/test/java/org/apache/seata/config/consul/ConsulConfigurationTest.java
@@ -135,26 +135,36 @@ class ConsulConfigurationTest {
 
     @Test
     void testOnChangeEvent_skipWhenValueIsBlank() throws InterruptedException {
-
         String dataId = "seata.properties";
-        ConsulConfiguration.ConsulListener listener = new ConsulConfiguration.ConsulListener(dataId, null);
 
+        // Mock the initial call in ConsulListener constructor (2-arg version)
+        GetValue initValue = mock(GetValue.class);
+        when(initValue.getDecodedValue()).thenReturn("dummy");
+        Response<GetValue> initResponse = new Response<>(initValue, 1L, false, 1L);
+        when(mockConsulClient.getKVValue(eq(dataId), (String) isNull())).thenReturn(initResponse);
+
+        // Mock the watch call in onChangeEvent loop (3-arg version)
         GetValue blankValue = mock(GetValue.class);
         when(blankValue.getDecodedValue()).thenReturn("");
-
         Response<GetValue> blankResponse = new Response<>(blankValue, 2L, false, 2L);
-        when(mockConsulClient.getKVValue(eq(dataId), isNull(), any(QueryParams.class)))
+        when(mockConsulClient.getKVValue(eq(dataId), (String) isNull(), any(QueryParams.class)))
                 .thenReturn(blankResponse);
 
+        ConsulConfiguration.ConsulListener listener = new ConsulConfiguration.ConsulListener(dataId, null);
+
         // Run onChangeEvent in a separate thread since it loops indefinitely
-        Thread thread = new Thread(() -> listener.onChangeEvent(new ConfigurationChangeEvent()));
+        Thread thread = new Thread(() -> {
+            try {
+                listener.onChangeEvent(new ConfigurationChangeEvent());
+            } catch (Exception e) {
+                // ignore
+            }
+        });
         thread.start();
         Thread.sleep(100);
-        // Interrupt to break the loop
         thread.interrupt();
         thread.join(500);
 
-        // Assert: Test passes as long as no exceptions are thrown
         assertTrue(true);
     }
 


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@seata.apache.org
For additional commands, e-mail: notifications-help@seata.apache.org