Skip to content

Commit 066fd27

Browse files
committed
feat(test): Add unit tests for McpServers startServer method
1 parent 746f43b commit 066fd27

File tree

6 files changed

+78
-20
lines changed

6 files changed

+78
-20
lines changed

src/main/java/com/github/codeboyzhou/mcp/declarative/McpServers.java

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import io.modelcontextprotocol.server.transport.HttpServletSseServerTransportProvider;
1919
import io.modelcontextprotocol.server.transport.StdioServerTransportProvider;
2020
import io.modelcontextprotocol.spec.McpServerTransportProvider;
21+
import io.modelcontextprotocol.util.Assert;
2122
import org.reflections.Reflections;
2223
import org.slf4j.Logger;
2324
import org.slf4j.LoggerFactory;
@@ -83,32 +84,23 @@ public void startSyncSseServer(McpSseServerInfo serverInfo) {
8384
}
8485

8586
public void startServer(String configFileName) {
86-
McpServerConfiguration configuration = loadConfiguration(configFileName);
87-
if (configuration.enabled()) {
88-
startServerWith(configuration);
89-
} else {
90-
logger.info("MCP server is disabled.");
91-
}
92-
}
93-
94-
public void startServer() {
95-
// Load configuration from default file
96-
startServer(null);
97-
}
98-
99-
private McpServerConfiguration loadConfiguration(String configFileName) {
87+
Assert.notNull(configFileName, "configFileName must not be null");
10088
YamlConfigurationLoader configurationLoader = new YamlConfigurationLoader();
10189
McpServerConfiguration configuration;
10290
try {
103-
if (configFileName == null || configFileName.isBlank()) {
104-
configuration = configurationLoader.loadConfiguration();
91+
configuration = configurationLoader.load(configFileName);
92+
if (configuration.enabled()) {
93+
startServerWith(configuration);
10594
} else {
106-
configuration = configurationLoader.load(configFileName);
95+
logger.info("MCP server is disabled.");
10796
}
10897
} catch (IOException e) {
109-
throw new McpServerException("Error loading configuration file", e);
98+
throw new McpServerException("Error loading configuration file: " + e.getMessage(), e);
11099
}
111-
return configuration;
100+
}
101+
102+
public void startServer() {
103+
startServer("mcp-server.yml");
112104
}
113105

114106
private void startServerWith(McpServerConfiguration configuration) {

src/test/java/com/github/codeboyzhou/mcp/declarative/McpServersTest.java

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.github.codeboyzhou.mcp.declarative;
22

33
import com.github.codeboyzhou.mcp.declarative.annotation.McpTools;
4+
import com.github.codeboyzhou.mcp.declarative.exception.McpServerException;
45
import com.github.codeboyzhou.mcp.declarative.server.McpServerInfo;
56
import com.github.codeboyzhou.mcp.declarative.server.McpSseServerInfo;
67
import com.github.codeboyzhou.mcp.declarative.server.TestMcpComponentScanBasePackageClass;
@@ -9,6 +10,7 @@
910
import com.github.codeboyzhou.mcp.declarative.server.TestMcpComponentScanIsNull;
1011
import com.github.codeboyzhou.mcp.declarative.server.TestMcpTools;
1112
import org.junit.jupiter.api.AfterEach;
13+
import org.junit.jupiter.api.BeforeEach;
1214
import org.junit.jupiter.api.Test;
1315
import org.junit.jupiter.params.ParameterizedTest;
1416
import org.junit.jupiter.params.provider.ValueSource;
@@ -23,13 +25,19 @@
2325
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
2426
import static org.junit.jupiter.api.Assertions.assertEquals;
2527
import static org.junit.jupiter.api.Assertions.assertNotNull;
28+
import static org.junit.jupiter.api.Assertions.assertThrows;
2629

2730
class McpServersTest {
2831

2932
static final String[] EMPTY_ARGS = new String[]{};
3033

3134
Reflections reflections;
3235

36+
@BeforeEach
37+
void setUp() {
38+
System.setProperty("mcp.declarative.java.sdk.testing", "true");
39+
}
40+
3341
@AfterEach
3442
void tearDown() throws NoSuchFieldException, IllegalAccessException {
3543
reflections = getReflectionsField();
@@ -68,7 +76,6 @@ void testStartSyncStdioServer() {
6876

6977
@Test
7078
void testStartSyncSseServer() {
71-
System.setProperty("mcp.declarative.java.sdk.testing", "true");
7279
McpServers servers = McpServers.run(TestMcpComponentScanIsNull.class, EMPTY_ARGS);
7380
assertDoesNotThrow(() -> {
7481
McpSseServerInfo serverInfo = McpSseServerInfo.builder()
@@ -85,6 +92,37 @@ void testStartSyncSseServer() {
8592
});
8693
}
8794

95+
@Test
96+
void testStartServer() {
97+
assertDoesNotThrow(() -> {
98+
McpServers servers = McpServers.run(TestMcpComponentScanIsNull.class, EMPTY_ARGS);
99+
servers.startServer();
100+
});
101+
}
102+
103+
@ParameterizedTest
104+
@ValueSource(strings = {
105+
"mcp-server.yml",
106+
"mcp-server-async.yml",
107+
"mcp-server-sse-mode.yml",
108+
"mcp-server-not-enabled.yml"
109+
})
110+
void testStartServerWithConfigFileName(String configFileName) {
111+
assertDoesNotThrow(() -> {
112+
McpServers servers = McpServers.run(TestMcpComponentScanIsNull.class, EMPTY_ARGS);
113+
servers.startServer(configFileName);
114+
});
115+
}
116+
117+
@Test
118+
void testStartServerWithInvalidConfigFileName() {
119+
McpServerException e = assertThrows(McpServerException.class, () -> {
120+
McpServers servers = McpServers.run(TestMcpComponentScanIsNull.class, EMPTY_ARGS);
121+
servers.startServer("mcp-server-not-exist.yml");
122+
});
123+
assertEquals("Error loading configuration file: mcp-server-not-exist.yml", e.getMessage());
124+
}
125+
88126
private Reflections getReflectionsField() throws NoSuchFieldException, IllegalAccessException {
89127
Field reflectionsField = McpServers.class.getDeclaredField("reflections");
90128
reflectionsField.setAccessible(true);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
enabled: true
2+
stdio: true
3+
name: mcp-stdio-server
4+
version: 1.0.0
5+
instructions: A simple server that uses stdio to communicate with the client
6+
request-timeout: 30000
7+
type: ASYNC
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
enabled: false
2+
stdio: true
3+
name: mcp-stdio-server
4+
version: 1.0.0
5+
instructions: A simple server that uses stdio to communicate with the client
6+
request-timeout: 30000
7+
type: SYNC
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
enabled: true
2+
stdio: false
3+
name: mcp-stdio-server
4+
version: 1.0.0
5+
instructions: A simple server that uses stdio to communicate with the client
6+
request-timeout: 30000
7+
type: SYNC

src/test/resources/mcp-server.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
enabled: true
2+
stdio: true
3+
name: mcp-stdio-server
4+
version: 1.0.0
5+
instructions: A simple server that uses stdio to communicate with the client
6+
request-timeout: 30000
7+
type: SYNC

0 commit comments

Comments
 (0)