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 bd717a6b7e test: add UT for RemotingFactoryBeanParser class (#7420)
bd717a6b7e is described below

commit bd717a6b7e77813c72d8fcf9dc43aaa9217b7517
Author: maple <gsk525...@163.com>
AuthorDate: Wed Jun 11 00:19:58 2025 +0800

    test: add UT for RemotingFactoryBeanParser class (#7420)
---
 changes/en-us/2.x.md                               |   5 +-
 changes/zh-cn/2.x.md                               |   6 +-
 spring/pom.xml                                     |  10 +
 .../parser/RemotingFactoryBeanParserTest.java      | 215 +++++++++++++++++++++
 4 files changed, 234 insertions(+), 2 deletions(-)

diff --git a/changes/en-us/2.x.md b/changes/en-us/2.x.md
index cd1ebce5c1..d56ea842a1 100644
--- a/changes/en-us/2.x.md
+++ b/changes/en-us/2.x.md
@@ -83,7 +83,8 @@ Add changes here for all PR submitted to the 2.x branch.
 - [[#7295](https://github.com/apache/incubator-seata/pull/7295)] updated 3 
tests in StringUtilsTest to use parameterized unit testing
 - [[#7205](https://github.com/apache/incubator-seata/issues/7205)] add UT for 
namingserver module
 - [[#7359](https://github.com/apache/incubator-seata/issues/7359)] merge 
submodule test reports
-- [[#7377](https://github.com/apache/incubator-seata/issues/7377)] add UT for 
org.apache.seata.spring.annotation.scannercheckers
+- [[#7423](https://github.com/apache/incubator-seata/pull/7423)] add UT for 
org.apache.seata.spring.annotation.scannercheckers
+- [[#7420](https://github.com/apache/incubator-seata/pull/7420)] add UT for 
RemotingFactoryBeanParser class
 - [[#7379](https://github.com/apache/incubator-seata/issues/7379)] add UT for 
TccAnnotationProcessor class
 - [[#7422](https://github.com/apache/incubator-seata/pull/7422)] add UT for 
seata-spring-boot-starter module
 
@@ -114,5 +115,7 @@ Thanks to these contributors for their code commits. Please 
report an unintended
 - [xucq07](https://github.com/xucq07)
 - [PengningYang](https://github.com/PengningYang)
 - [WangzJi](https://github.com/WangzJi)
+- [maple525866](https://github.com/maple525866)
+
 
 Also, we receive many valuable issues, questions and advices from our 
community. Thanks for you all.
diff --git a/changes/zh-cn/2.x.md b/changes/zh-cn/2.x.md
index 9355c8a229..c7ef41db1b 100644
--- a/changes/zh-cn/2.x.md
+++ b/changes/zh-cn/2.x.md
@@ -84,7 +84,8 @@
 - [[#7295](https://github.com/apache/incubator-seata/pull/7295)] 重构了 
StringUtilsTest 中的 3 个测试,改为使用参数化单元测试
 - [[#7205](https://github.com/apache/incubator-seata/issues/7205)] 为 
namingserver module 添加单元测试
 - [[#7359](https://github.com/apache/incubator-seata/issues/7359)] 
合并所有模块的单测报告,准确显示单测覆盖度
-- [[#7377](https://github.com/apache/incubator-seata/issues/7377)] 为 
org.apache.seata.spring.annotation.scannercheckers 添加单元测试
+- [[#7423](https://github.com/apache/incubator-seata/pull/7423)] 为 
org.apache.seata.spring.annotation.scannercheckers 添加单元测试
+- [[#7420](https://github.com/apache/incubator-seata/pull/7420)] 为 
RemotingFactoryBeanParser 类添加了单元测试
 - [[#7379](https://github.com/apache/incubator-seata/issues/7379)] 为 
TccAnnotationProcessor 添加了单元测试 
 - [[#7422](https://github.com/apache/incubator-seata/pull/7422)] 为 
seata-spring-boot-starter 添加了测试
 
@@ -115,6 +116,9 @@
 - [xucq07](https://github.com/xucq07)
 - [PengningYang](https://github.com/PengningYang)
 - [WangzJi](https://github.com/WangzJi)
+- [maple525866](https://github.com/maple525866)
+
+
 
 
 同时,我们收到了社区反馈的很多有价值的issue和建议,非常感谢大家。
diff --git a/spring/pom.xml b/spring/pom.xml
index f6613638ad..a6bf4022dc 100644
--- a/spring/pom.xml
+++ b/spring/pom.xml
@@ -62,6 +62,11 @@
             <artifactId>seata-tcc</artifactId>
             <version>${project.version}</version>
         </dependency>
+        <dependency>
+            <groupId>${project.groupId}</groupId>
+            <artifactId>seata-sqlparser-druid</artifactId>
+            <version>${project.version}</version>
+        </dependency>
 
         <!-- spring  -->
         <dependency>
@@ -86,6 +91,11 @@
             <artifactId>logback-classic</artifactId>
             <scope>test</scope>
         </dependency>
+        <dependency>
+            <groupId>org.mockito</groupId>
+            <artifactId>mockito-inline</artifactId>
+            <scope>test</scope>
+        </dependency>
         <dependency>
             <groupId>org.slf4j</groupId>
             <artifactId>slf4j-api</artifactId>
diff --git 
a/spring/src/test/java/org/apache/seata/spring/remoting/parser/RemotingFactoryBeanParserTest.java
 
b/spring/src/test/java/org/apache/seata/spring/remoting/parser/RemotingFactoryBeanParserTest.java
new file mode 100644
index 0000000000..fbffdbc178
--- /dev/null
+++ 
b/spring/src/test/java/org/apache/seata/spring/remoting/parser/RemotingFactoryBeanParserTest.java
@@ -0,0 +1,215 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.seata.spring.remoting.parser;
+
+import org.apache.seata.common.exception.FrameworkException;
+import org.apache.seata.integration.tx.api.remoting.RemotingDesc;
+import 
org.apache.seata.integration.tx.api.remoting.parser.DefaultRemotingParser;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.mockito.MockedStatic;
+import org.mockito.Mockito;
+import org.springframework.aop.framework.ProxyFactory;
+import org.springframework.context.ApplicationContext;
+
+/**
+ * Test for {@link RemotingFactoryBeanParser} class
+ */
+public class RemotingFactoryBeanParserTest {
+
+    private ApplicationContext applicationContext;
+    private RemotingFactoryBeanParser remotingFactoryBeanParser;
+    private DefaultRemotingParser defaultRemotingParser;
+    private MockedStatic<DefaultRemotingParser> mockedDefaultRemotingParser;
+
+    private interface TestService {
+        void doSomething();
+    }
+
+    private static class TestServiceImpl implements TestService {
+        @Override
+        public void doSomething() {
+            // Empty implementation
+        }
+    }
+
+    @BeforeEach
+    public void setUp() {
+        applicationContext = Mockito.mock(ApplicationContext.class);
+        remotingFactoryBeanParser = new 
RemotingFactoryBeanParser(applicationContext);
+
+        // Use Mockito.mockStatic to mock the static method of 
DefaultRemotingParser
+        defaultRemotingParser = Mockito.mock(DefaultRemotingParser.class);
+        mockedDefaultRemotingParser = 
Mockito.mockStatic(DefaultRemotingParser.class);
+        
mockedDefaultRemotingParser.when(DefaultRemotingParser::get).thenReturn(defaultRemotingParser);
+    }
+
+    @AfterEach
+    public void tearDown() {
+        if (mockedDefaultRemotingParser != null) {
+            mockedDefaultRemotingParser.close();
+        }
+    }
+    
+    /**
+     * Create a proxy test service
+     *
+     * @return the proxied test service
+     */
+    private TestService createProxyTestService() {
+        TestService testService = new TestServiceImpl();
+        ProxyFactory proxyFactory = new ProxyFactory(testService);
+        return (TestService) proxyFactory.getProxy();
+    }
+
+    @Test
+    public void testConstructorWithNullApplicationContext() {
+        Assertions.assertThrows(IllegalArgumentException.class, () -> {
+            new RemotingFactoryBeanParser(null);
+        });
+    }
+    
+    @Test
+    public void testGetRemotingFactoryBeanWithNonProxyBean() {
+        TestService testService = new TestServiceImpl();
+        Object result = 
remotingFactoryBeanParser.getRemotingFactoryBean(testService, "testService");
+        Assertions.assertNull(result);
+    }
+    
+    @Test
+    public void testGetRemotingFactoryBeanWithProxyBeanButNoFactoryBean() {
+        // Create proxy object
+        TestService proxyTestService = createProxyTestService();
+        
+        // Mock applicationContext behavior
+        
Mockito.when(applicationContext.containsBean("&testService")).thenReturn(false);
+        
+        // Test
+        Object result = 
remotingFactoryBeanParser.getRemotingFactoryBean(proxyTestService, 
"testService");
+        Assertions.assertNull(result);
+    }
+    
+    @Test
+    public void testGetRemotingFactoryBeanWithProxyBeanAndFactoryBean() {
+        // Create proxy object
+        TestService proxyTestService = createProxyTestService();
+        
+        // Mock applicationContext behavior
+        Object expectedFactoryBean = new Object();
+        
Mockito.when(applicationContext.containsBean("&testService")).thenReturn(true);
+        
Mockito.when(applicationContext.getBean("&testService")).thenReturn(expectedFactoryBean);
+        
+        // Test
+        Object result = 
remotingFactoryBeanParser.getRemotingFactoryBean(proxyTestService, 
"testService");
+        
+        // Alternative to assertNotNull - check that result is the same as our 
expected object
+        Assertions.assertSame(expectedFactoryBean, result);
+    }
+    
+    @Test
+    public void testIsReferenceWithNullFactoryBean() {
+        TestService testService = new TestServiceImpl();
+        boolean result = remotingFactoryBeanParser.isReference(testService, 
"testService");
+        Assertions.assertFalse(result);
+    }
+    
+    @Test
+    public void testIsReferenceWithFactoryBean() {
+        // Create proxy object
+        TestService proxyTestService = createProxyTestService();
+        
+        Object factoryBean = new Object();
+        
+        // Mock applicationContext behavior
+        
Mockito.when(applicationContext.containsBean("&testService")).thenReturn(true);
+        
Mockito.when(applicationContext.getBean("&testService")).thenReturn(factoryBean);
+        
+        // Mock DefaultRemotingParser behavior
+        Mockito.when(defaultRemotingParser.isReference(factoryBean, 
"&testService")).thenReturn(true);
+        
+        // Test
+        boolean result = 
remotingFactoryBeanParser.isReference(proxyTestService, "testService");
+        Assertions.assertTrue(result);
+    }
+    
+    @Test
+    public void testIsServiceWithNullFactoryBean() {
+        TestService testService = new TestServiceImpl();
+        boolean result = remotingFactoryBeanParser.isService(testService, 
"testService");
+        Assertions.assertFalse(result);
+    }
+    
+    @Test
+    public void testIsServiceWithFactoryBean() {
+        // Create proxy object
+        TestService proxyTestService = createProxyTestService();
+        
+        Object factoryBean = new Object();
+        
+        // Mock applicationContext behavior
+        
Mockito.when(applicationContext.containsBean("&testService")).thenReturn(true);
+        
Mockito.when(applicationContext.getBean("&testService")).thenReturn(factoryBean);
+        
+        // Mock DefaultRemotingParser behavior
+        Mockito.when(defaultRemotingParser.isService(factoryBean, 
"&testService")).thenReturn(true);
+        
+        // Test
+        boolean result = remotingFactoryBeanParser.isService(proxyTestService, 
"testService");
+        Assertions.assertTrue(result);
+    }
+    
+    @Test
+    public void testIsServiceWithClass() {
+        boolean result = 
remotingFactoryBeanParser.isService(TestServiceImpl.class);
+        Assertions.assertFalse(result);
+    }
+    
+    @Test
+    public void testGetServiceDescWithNullFactoryBean() {
+        TestService testService = new TestServiceImpl();
+        RemotingDesc result = 
remotingFactoryBeanParser.getServiceDesc(testService, "testService");
+        Assertions.assertNull(result);
+    }
+    
+    @Test
+    public void testGetServiceDescWithFactoryBean() throws FrameworkException {
+        // Create proxy object
+        TestService proxyTestService = createProxyTestService();
+        
+        Object factoryBean = new Object();
+        RemotingDesc expectedDesc = new RemotingDesc();
+        
+        // Mock applicationContext behavior
+        
Mockito.when(applicationContext.containsBean("&testService")).thenReturn(true);
+        
Mockito.when(applicationContext.getBean("&testService")).thenReturn(factoryBean);
+        
+        // Mock DefaultRemotingParser behavior
+        Mockito.when(defaultRemotingParser.getServiceDesc(factoryBean, 
"&testService")).thenReturn(expectedDesc);
+        
+        // Test
+        RemotingDesc result = 
remotingFactoryBeanParser.getServiceDesc(proxyTestService, "testService");
+        Assertions.assertEquals(expectedDesc, result);
+    }
+    
+    @Test
+    public void testGetProtocol() {
+        short result = remotingFactoryBeanParser.getProtocol();
+        Assertions.assertEquals(0, result);
+    }
+} 
\ No newline at end of file


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

Reply via email to