Skip to content

Commit d29e3a9

Browse files
committed
test(common): Add unit tests for BufferQueue
1 parent 69e28d7 commit d29e3a9

File tree

3 files changed

+65
-4
lines changed

3 files changed

+65
-4
lines changed

pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
<junit5.version>5.10.2</junit5.version>
5959
<logback.version>1.5.18</logback.version>
6060
<mcp-sdk.version>0.10.0</mcp-sdk.version>
61+
<mockito.version>5.18.0</mockito.version>
6162
<reflections.version>0.10.2</reflections.version>
6263
</properties>
6364

@@ -112,6 +113,12 @@
112113
<version>${junit5.version}</version>
113114
<scope>test</scope>
114115
</dependency>
116+
<dependency>
117+
<groupId>org.mockito</groupId>
118+
<artifactId>mockito-core</artifactId>
119+
<version>${mockito.version}</version>
120+
<scope>test</scope>
121+
</dependency>
115122
<dependency>
116123
<groupId>org.reflections</groupId>
117124
<artifactId>reflections</artifactId>

src/main/java/com/github/codeboyzhou/mcp/declarative/exception/McpServerException.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22

33
public class McpServerException extends RuntimeException {
44

5-
public McpServerException(String message) {
6-
super(message);
7-
}
8-
95
public McpServerException(String message, Throwable cause) {
106
super(message, cause);
117
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.github.codeboyzhou.mcp.declarative.common;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.lang.reflect.Field;
6+
import java.util.concurrent.BlockingQueue;
7+
import java.util.concurrent.LinkedBlockingQueue;
8+
9+
import static org.junit.jupiter.api.Assertions.assertEquals;
10+
import static org.junit.jupiter.api.Assertions.assertThrows;
11+
import static org.mockito.ArgumentMatchers.anyString;
12+
import static org.mockito.Mockito.doThrow;
13+
import static org.mockito.Mockito.mock;
14+
import static org.mockito.Mockito.verify;
15+
16+
class BufferQueueTest {
17+
18+
@Test
19+
void testNewInstance() {
20+
IllegalArgumentException e = assertThrows(IllegalArgumentException.class, () -> new BufferQueue<>(0));
21+
assertEquals("delayMillis must be greater than 0", e.getMessage());
22+
}
23+
24+
@Test
25+
@SuppressWarnings("unchecked")
26+
void testSubmitCatchException() throws Exception {
27+
BlockingQueue<String> queueMock = mock(LinkedBlockingQueue.class);
28+
doThrow(InterruptedException.class).when(queueMock).put(anyString());
29+
30+
BufferQueue<String> bufferQueue = new BufferQueue<>();
31+
Field queue = BufferQueue.class.getDeclaredField("queue");
32+
queue.setAccessible(true);
33+
queue.set(bufferQueue, queueMock);
34+
35+
bufferQueue.submit("test");
36+
37+
verify(queueMock).put("test");
38+
}
39+
40+
@Test
41+
@SuppressWarnings("unchecked")
42+
void testConsumeCatchException() throws Exception {
43+
BlockingQueue<String> queueMock = mock(LinkedBlockingQueue.class);
44+
doThrow(InterruptedException.class).when(queueMock).take();
45+
46+
BufferQueue<String> bufferQueue = new BufferQueue<>();
47+
Field queue = BufferQueue.class.getDeclaredField("queue");
48+
queue.setAccessible(true);
49+
queue.set(bufferQueue, queueMock);
50+
51+
bufferQueue.consume(string -> {
52+
// do nothing
53+
});
54+
55+
verify(queueMock).take();
56+
}
57+
58+
}

0 commit comments

Comments
 (0)