You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@seata.apache.org by "Copilot (via GitHub)" <gi...@apache.org> on 2025/06/07 13:47:22 UTC

Re: [PR] test: add UT for RemotingFactoryBeanParser class [incubator-seata]

Copilot commented on code in PR #7420:
URL: https://github.com/apache/incubator-seata/pull/7420#discussion_r2133842854


##########
spring/src/test/java/org/apache/seata/spring/remoting/parser/RemotingFactoryBeanParserTest.java:
##########
@@ -0,0 +1,211 @@
+/*
+ * 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();
+        }
+    }
+
+    @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 testService = new TestServiceImpl();
+        ProxyFactory proxyFactory = new ProxyFactory(testService);
+        TestService proxyTestService = (TestService) proxyFactory.getProxy();
+        
+        // Mock applicationContext behavior
+        Mockito.when(applicationContext.containsBean("&testService")).thenReturn(false);
+        

Review Comment:
   [nitpick] Proxy creation and context stubbing are repeated across tests; consider extracting a helper method or moving common setup to `@BeforeEach` to reduce duplication.
   ```suggestion
           // Use helper method to set up proxy and mock context
           TestService proxyTestService = (TestService) setUpProxyAndContext("testService", false);
           
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@seata.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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