Skip to content

Commit 32147c7

Browse files
committed
feat(tests): Add unit tests for McpServers
1 parent 3cf3a49 commit 32147c7

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

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

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

33
import com.github.codeboyzhou.mcp.declarative.annotation.McpTools;
4-
import com.github.codeboyzhou.mcp.declarative.server.TestMcpTools;
54
import com.github.codeboyzhou.mcp.declarative.server.TestMcpComponentScanBasePackageClass;
65
import com.github.codeboyzhou.mcp.declarative.server.TestMcpComponentScanBasePackageString;
76
import com.github.codeboyzhou.mcp.declarative.server.TestMcpComponentScanDefault;
87
import com.github.codeboyzhou.mcp.declarative.server.TestMcpComponentScanIsNull;
8+
import com.github.codeboyzhou.mcp.declarative.server.TestMcpTools;
99
import org.junit.jupiter.api.AfterEach;
1010
import org.junit.jupiter.api.BeforeEach;
11+
import org.junit.jupiter.api.Test;
1112
import org.junit.jupiter.params.ParameterizedTest;
1213
import org.junit.jupiter.params.provider.ValueSource;
1314
import org.reflections.Reflections;
@@ -17,6 +18,7 @@
1718
import java.util.Map;
1819
import java.util.Set;
1920

21+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
2022
import static org.junit.jupiter.api.Assertions.assertEquals;
2123
import static org.junit.jupiter.api.Assertions.assertNotNull;
2224

@@ -52,6 +54,14 @@ void testRun(Class<?> applicationMainClass) {
5254
McpServers.run(applicationMainClass, EMPTY_ARGS);
5355
}
5456

57+
@Test
58+
void testStartSyncStdioServer() {
59+
assertDoesNotThrow(() -> {
60+
McpServers servers = McpServers.run(TestMcpComponentScanDefault.class, EMPTY_ARGS);
61+
servers.startSyncStdioServer("test-mcp-sync-stdio-server", "1.0.0");
62+
});
63+
}
64+
5565
private Reflections getReflectionsField() throws NoSuchFieldException, IllegalAccessException {
5666
Field reflectionsField = McpServers.class.getDeclaredField("reflections");
5767
reflectionsField.setAccessible(true);
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.github.codeboyzhou.mcp.declarative.server;
2+
3+
import com.github.codeboyzhou.mcp.declarative.annotation.McpResource;
4+
import com.github.codeboyzhou.mcp.declarative.annotation.McpResources;
5+
6+
@McpResources
7+
public class TestMcpResources {
8+
9+
@McpResource(uri = "test://resource1", name = "resource1", description = "resource1")
10+
public String resource1() {
11+
return System.lineSeparator();
12+
}
13+
14+
@McpResource(uri = "test://resource2", description = "resource2")
15+
public String resource2() {
16+
return System.lineSeparator();
17+
}
18+
19+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,26 @@
11
package com.github.codeboyzhou.mcp.declarative.server;
22

3+
import com.github.codeboyzhou.mcp.declarative.annotation.McpTool;
4+
import com.github.codeboyzhou.mcp.declarative.annotation.McpToolParam;
35
import com.github.codeboyzhou.mcp.declarative.annotation.McpTools;
46

57
@McpTools
68
public class TestMcpTools {
79

10+
@McpTool(name = "tool1", description = "tool1")
11+
public static String tool1(
12+
@McpToolParam(name = "name", description = "name", required = true) String name,
13+
@McpToolParam(name = "version", description = "version", required = true) String version
14+
) {
15+
return "Hello " + name + ", I am " + version;
16+
}
17+
18+
@McpTool(description = "tool2")
19+
public static String tool2(
20+
@McpToolParam(name = "name", description = "name") String name,
21+
@McpToolParam(name = "version", description = "version") String version
22+
) {
23+
return "Hello " + name + ", I am " + version;
24+
}
25+
826
}

0 commit comments

Comments
 (0)