From e4f41791abf42782781a20876fe4901550e7d8c2 Mon Sep 17 00:00:00 2001 From: lance Date: Thu, 6 Nov 2025 21:45:22 +0800 Subject: [PATCH 1/2] add tests for McpSyncClientConfigurer/McpAsyncClientConfigurer Signed-off-by: lance --- .../McpAsyncClientConfigurerTests.java | 66 +++++++++++++++++++ .../McpSyncClientConfigurerTests.java | 66 +++++++++++++++++++ 2 files changed, 132 insertions(+) create mode 100644 auto-configurations/mcp/spring-ai-autoconfigure-mcp-client-common/src/test/java/org/springframework/ai/mcp/client/common/autoconfigure/configurer/McpAsyncClientConfigurerTests.java create mode 100644 auto-configurations/mcp/spring-ai-autoconfigure-mcp-client-common/src/test/java/org/springframework/ai/mcp/client/common/autoconfigure/configurer/McpSyncClientConfigurerTests.java diff --git a/auto-configurations/mcp/spring-ai-autoconfigure-mcp-client-common/src/test/java/org/springframework/ai/mcp/client/common/autoconfigure/configurer/McpAsyncClientConfigurerTests.java b/auto-configurations/mcp/spring-ai-autoconfigure-mcp-client-common/src/test/java/org/springframework/ai/mcp/client/common/autoconfigure/configurer/McpAsyncClientConfigurerTests.java new file mode 100644 index 00000000000..4db6402b9f6 --- /dev/null +++ b/auto-configurations/mcp/spring-ai-autoconfigure-mcp-client-common/src/test/java/org/springframework/ai/mcp/client/common/autoconfigure/configurer/McpAsyncClientConfigurerTests.java @@ -0,0 +1,66 @@ +package org.springframework.ai.mcp.client.common.autoconfigure.configurer; + +import java.util.Collections; +import java.util.List; + +import io.modelcontextprotocol.client.McpClient; +import io.modelcontextprotocol.spec.McpClientTransport; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.InOrder; + +import org.springframework.ai.mcp.customizer.McpAsyncClientCustomizer; + +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.mockito.Mockito.inOrder; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verifyNoInteractions; +import static org.mockito.Mockito.verifyNoMoreInteractions; + +/** + * Unit tests for {@link McpAsyncClientConfigurer}. + * + * @author lance + */ +class McpAsyncClientConfigurerTests { + + private McpAsyncClientCustomizer customizer1; + + private McpAsyncClientCustomizer customizer2; + + private McpClient.AsyncSpec spec; + + private McpAsyncClientConfigurer configurer; + + @BeforeEach + void setUp() { + customizer1 = mock(McpAsyncClientCustomizer.class); + customizer2 = mock(McpAsyncClientCustomizer.class); + spec = McpClient.async(mock(McpClientTransport.class)); + } + + @Test + void testConfigureWithCustomizersInOrder() { + configurer = new McpAsyncClientConfigurer(List.of(customizer1, customizer2)); + + McpClient.AsyncSpec result = configurer.configure("asyncClientA", spec); + + InOrder inOrder = inOrder(customizer1, customizer2); + inOrder.verify(customizer1).customize("asyncClientA", spec); + inOrder.verify(customizer2).customize("asyncClientA", spec); + + verifyNoMoreInteractions(customizer1, customizer2); + assertSame(spec, result); + } + + @Test + void testConfigureWithoutCustomizers() { + configurer = new McpAsyncClientConfigurer(Collections.emptyList()); + + McpClient.AsyncSpec result = configurer.configure("asyncClientB", spec); + + assertSame(spec, result); + verifyNoInteractions(customizer1, customizer2); + } + +} diff --git a/auto-configurations/mcp/spring-ai-autoconfigure-mcp-client-common/src/test/java/org/springframework/ai/mcp/client/common/autoconfigure/configurer/McpSyncClientConfigurerTests.java b/auto-configurations/mcp/spring-ai-autoconfigure-mcp-client-common/src/test/java/org/springframework/ai/mcp/client/common/autoconfigure/configurer/McpSyncClientConfigurerTests.java new file mode 100644 index 00000000000..995867fb7df --- /dev/null +++ b/auto-configurations/mcp/spring-ai-autoconfigure-mcp-client-common/src/test/java/org/springframework/ai/mcp/client/common/autoconfigure/configurer/McpSyncClientConfigurerTests.java @@ -0,0 +1,66 @@ +package org.springframework.ai.mcp.client.common.autoconfigure.configurer; + +import java.util.List; + +import io.modelcontextprotocol.client.McpClient; +import io.modelcontextprotocol.spec.McpClientTransport; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import org.springframework.ai.mcp.customizer.McpSyncClientCustomizer; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.only; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoMoreInteractions; + +/** + * Unit tests for {@link McpSyncClientConfigurer}. + * + * @author lance + */ +class McpSyncClientConfigurerTests { + + private McpSyncClientCustomizer customizer1; + + private McpSyncClientCustomizer customizer2; + + private McpClient.SyncSpec spec; + + private McpSyncClientConfigurer configurer; + + @BeforeEach + void setUp() { + customizer1 = mock(McpSyncClientCustomizer.class); + customizer2 = mock(McpSyncClientCustomizer.class); + spec = McpClient.sync(mock(McpClientTransport.class)); + } + + @Test + void testConfigureWithCustomizers() { + configurer = new McpSyncClientConfigurer(List.of(customizer1, customizer2)); + McpClient.SyncSpec result = configurer.configure("clientA", spec); + + verify(customizer1, only()).customize("clientA", spec); + verify(customizer2, only()).customize("clientA", spec); + + verifyNoMoreInteractions(customizer1, customizer2); + Assertions.assertSame(spec, result); + } + + @Test + void testConfigureWithoutCustomizers() { + configurer = new McpSyncClientConfigurer(List.of()); + McpClient.SyncSpec result = configurer.configure("clientB", spec); + Assertions.assertSame(spec, result); + } + + @Test + void testConfigureWithNullCustomizers() { + configurer = new McpSyncClientConfigurer(null); + McpClient.SyncSpec result = configurer.configure("clientC", spec); + Assertions.assertSame(spec, result); + } + +} From 3497d7fa2f4a32b64e2aa1d9b1f021cf8a174c82 Mon Sep 17 00:00:00 2001 From: lance Date: Thu, 6 Nov 2025 21:54:40 +0800 Subject: [PATCH 2/2] add tests for McpSyncClientConfigurer/McpAsyncClientConfigurer Signed-off-by: lance --- .../McpAsyncClientConfigurerTests.java | 44 ++++++++++++------ .../McpSyncClientConfigurerTests.java | 46 +++++++++++++------ 2 files changed, 61 insertions(+), 29 deletions(-) diff --git a/auto-configurations/mcp/spring-ai-autoconfigure-mcp-client-common/src/test/java/org/springframework/ai/mcp/client/common/autoconfigure/configurer/McpAsyncClientConfigurerTests.java b/auto-configurations/mcp/spring-ai-autoconfigure-mcp-client-common/src/test/java/org/springframework/ai/mcp/client/common/autoconfigure/configurer/McpAsyncClientConfigurerTests.java index 4db6402b9f6..43329af8c34 100644 --- a/auto-configurations/mcp/spring-ai-autoconfigure-mcp-client-common/src/test/java/org/springframework/ai/mcp/client/common/autoconfigure/configurer/McpAsyncClientConfigurerTests.java +++ b/auto-configurations/mcp/spring-ai-autoconfigure-mcp-client-common/src/test/java/org/springframework/ai/mcp/client/common/autoconfigure/configurer/McpAsyncClientConfigurerTests.java @@ -1,3 +1,19 @@ +/* + * Copyright 2025-2025 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.ai.mcp.client.common.autoconfigure.configurer; import java.util.Collections; @@ -34,33 +50,33 @@ class McpAsyncClientConfigurerTests { @BeforeEach void setUp() { - customizer1 = mock(McpAsyncClientCustomizer.class); - customizer2 = mock(McpAsyncClientCustomizer.class); - spec = McpClient.async(mock(McpClientTransport.class)); + this.customizer1 = mock(McpAsyncClientCustomizer.class); + this.customizer2 = mock(McpAsyncClientCustomizer.class); + this.spec = McpClient.async(mock(McpClientTransport.class)); } @Test void testConfigureWithCustomizersInOrder() { - configurer = new McpAsyncClientConfigurer(List.of(customizer1, customizer2)); + this.configurer = new McpAsyncClientConfigurer(List.of(this.customizer1, this.customizer2)); - McpClient.AsyncSpec result = configurer.configure("asyncClientA", spec); + McpClient.AsyncSpec result = this.configurer.configure("asyncClientA", this.spec); - InOrder inOrder = inOrder(customizer1, customizer2); - inOrder.verify(customizer1).customize("asyncClientA", spec); - inOrder.verify(customizer2).customize("asyncClientA", spec); + InOrder inOrder = inOrder(this.customizer1, this.customizer2); + inOrder.verify(this.customizer1).customize("asyncClientA", this.spec); + inOrder.verify(this.customizer2).customize("asyncClientA", this.spec); - verifyNoMoreInteractions(customizer1, customizer2); - assertSame(spec, result); + verifyNoMoreInteractions(this.customizer1, this.customizer2); + assertSame(this.spec, result); } @Test void testConfigureWithoutCustomizers() { - configurer = new McpAsyncClientConfigurer(Collections.emptyList()); + this.configurer = new McpAsyncClientConfigurer(Collections.emptyList()); - McpClient.AsyncSpec result = configurer.configure("asyncClientB", spec); + McpClient.AsyncSpec result = this.configurer.configure("asyncClientB", this.spec); - assertSame(spec, result); - verifyNoInteractions(customizer1, customizer2); + assertSame(this.spec, result); + verifyNoInteractions(this.customizer1, this.customizer2); } } diff --git a/auto-configurations/mcp/spring-ai-autoconfigure-mcp-client-common/src/test/java/org/springframework/ai/mcp/client/common/autoconfigure/configurer/McpSyncClientConfigurerTests.java b/auto-configurations/mcp/spring-ai-autoconfigure-mcp-client-common/src/test/java/org/springframework/ai/mcp/client/common/autoconfigure/configurer/McpSyncClientConfigurerTests.java index 995867fb7df..d66a8a238c4 100644 --- a/auto-configurations/mcp/spring-ai-autoconfigure-mcp-client-common/src/test/java/org/springframework/ai/mcp/client/common/autoconfigure/configurer/McpSyncClientConfigurerTests.java +++ b/auto-configurations/mcp/spring-ai-autoconfigure-mcp-client-common/src/test/java/org/springframework/ai/mcp/client/common/autoconfigure/configurer/McpSyncClientConfigurerTests.java @@ -1,3 +1,19 @@ +/* + * Copyright 2025-2025 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.ai.mcp.client.common.autoconfigure.configurer; import java.util.List; @@ -32,35 +48,35 @@ class McpSyncClientConfigurerTests { @BeforeEach void setUp() { - customizer1 = mock(McpSyncClientCustomizer.class); - customizer2 = mock(McpSyncClientCustomizer.class); - spec = McpClient.sync(mock(McpClientTransport.class)); + this.customizer1 = mock(McpSyncClientCustomizer.class); + this.customizer2 = mock(McpSyncClientCustomizer.class); + this.spec = McpClient.sync(mock(McpClientTransport.class)); } @Test void testConfigureWithCustomizers() { - configurer = new McpSyncClientConfigurer(List.of(customizer1, customizer2)); - McpClient.SyncSpec result = configurer.configure("clientA", spec); + this.configurer = new McpSyncClientConfigurer(List.of(this.customizer1, this.customizer2)); + McpClient.SyncSpec result = this.configurer.configure("clientA", this.spec); - verify(customizer1, only()).customize("clientA", spec); - verify(customizer2, only()).customize("clientA", spec); + verify(this.customizer1, only()).customize("clientA", this.spec); + verify(this.customizer2, only()).customize("clientA", this.spec); - verifyNoMoreInteractions(customizer1, customizer2); - Assertions.assertSame(spec, result); + verifyNoMoreInteractions(this.customizer1, this.customizer2); + Assertions.assertSame(this.spec, result); } @Test void testConfigureWithoutCustomizers() { - configurer = new McpSyncClientConfigurer(List.of()); - McpClient.SyncSpec result = configurer.configure("clientB", spec); - Assertions.assertSame(spec, result); + this.configurer = new McpSyncClientConfigurer(List.of()); + McpClient.SyncSpec result = this.configurer.configure("clientB", this.spec); + Assertions.assertSame(this.spec, result); } @Test void testConfigureWithNullCustomizers() { - configurer = new McpSyncClientConfigurer(null); - McpClient.SyncSpec result = configurer.configure("clientC", spec); - Assertions.assertSame(spec, result); + this.configurer = new McpSyncClientConfigurer(null); + McpClient.SyncSpec result = this.configurer.configure("clientC", this.spec); + Assertions.assertSame(this.spec, result); } }