Skip to content

Commit 7bc8726

Browse files
committed
test: add mock verification to McpAsyncServerExchangeTests
- Verify sendNotification calls in logging notification tests - Verify sendRequest is never called when capabilities are missing - Improve test assertions to ensure correct method invocation behavior Signed-off-by: Christian Tzolov <christian.tzolov@broadcom.com>
1 parent 4aeeb1f commit 7bc8726

1 file changed

Lines changed: 46 additions & 0 deletions

File tree

mcp/src/test/java/io/modelcontextprotocol/server/McpAsyncServerExchangeTests.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
import static org.assertj.core.api.Assertions.assertThatThrownBy;
2424
import static org.mockito.ArgumentMatchers.any;
2525
import static org.mockito.ArgumentMatchers.eq;
26+
import static org.mockito.Mockito.never;
27+
import static org.mockito.Mockito.reset;
28+
import static org.mockito.Mockito.times;
29+
import static org.mockito.Mockito.verify;
2630
import static org.mockito.Mockito.when;
2731

2832
/**
@@ -226,6 +230,9 @@ void testLoggingNotificationWithAllowedLevel() {
226230
.thenReturn(Mono.empty());
227231

228232
StepVerifier.create(exchange.loggingNotification(notification)).verifyComplete();
233+
234+
// Verify that sendNotification was called exactly once
235+
verify(mockSession, times(1)).sendNotification(eq(McpSchema.METHOD_NOTIFICATION_MESSAGE), eq(notification));
229236
}
230237

231238
@Test
@@ -241,6 +248,9 @@ void testLoggingNotificationWithFilteredLevel() {
241248

242249
// When & Then - Should complete without sending notification
243250
StepVerifier.create(exchange.loggingNotification(debugNotification)).verifyComplete();
251+
252+
// Verify that sendNotification was never called for filtered DEBUG level
253+
verify(mockSession, never()).sendNotification(eq(McpSchema.METHOD_NOTIFICATION_MESSAGE), eq(debugNotification));
244254
}
245255

246256
@Test
@@ -257,6 +267,9 @@ void testLoggingNotificationLevelFiltering() {
257267

258268
StepVerifier.create(exchange.loggingNotification(debugNotification)).verifyComplete();
259269

270+
// Verify that sendNotification was never called for DEBUG level
271+
verify(mockSession, never()).sendNotification(eq(McpSchema.METHOD_NOTIFICATION_MESSAGE), eq(debugNotification));
272+
260273
// Test INFO (should be filtered)
261274
McpSchema.LoggingMessageNotification infoNotification = McpSchema.LoggingMessageNotification.builder()
262275
.level(McpSchema.LoggingLevel.INFO)
@@ -266,6 +279,11 @@ void testLoggingNotificationLevelFiltering() {
266279

267280
StepVerifier.create(exchange.loggingNotification(infoNotification)).verifyComplete();
268281

282+
// Verify that sendNotification was never called for INFO level
283+
verify(mockSession, never()).sendNotification(eq(McpSchema.METHOD_NOTIFICATION_MESSAGE), eq(infoNotification));
284+
285+
reset(mockSession);
286+
269287
// Test WARNING (should be sent)
270288
McpSchema.LoggingMessageNotification warningNotification = McpSchema.LoggingMessageNotification.builder()
271289
.level(McpSchema.LoggingLevel.WARNING)
@@ -278,6 +296,10 @@ void testLoggingNotificationLevelFiltering() {
278296

279297
StepVerifier.create(exchange.loggingNotification(warningNotification)).verifyComplete();
280298

299+
// Verify that sendNotification was called exactly once for WARNING level
300+
verify(mockSession, times(1)).sendNotification(eq(McpSchema.METHOD_NOTIFICATION_MESSAGE),
301+
eq(warningNotification));
302+
281303
// Test ERROR (should be sent)
282304
McpSchema.LoggingMessageNotification errorNotification = McpSchema.LoggingMessageNotification.builder()
283305
.level(McpSchema.LoggingLevel.ERROR)
@@ -289,6 +311,10 @@ void testLoggingNotificationLevelFiltering() {
289311
.thenReturn(Mono.empty());
290312

291313
StepVerifier.create(exchange.loggingNotification(errorNotification)).verifyComplete();
314+
315+
// Verify that sendNotification was called exactly once for ERROR level
316+
verify(mockSession, times(1)).sendNotification(eq(McpSchema.METHOD_NOTIFICATION_MESSAGE),
317+
eq(errorNotification));
292318
}
293319

294320
@Test
@@ -304,6 +330,9 @@ void testLoggingNotificationWithDefaultLevel() {
304330
.thenReturn(Mono.empty());
305331

306332
StepVerifier.create(exchange.loggingNotification(infoNotification)).verifyComplete();
333+
334+
// Verify that sendNotification was called exactly once for default level
335+
verify(mockSession, times(1)).sendNotification(eq(McpSchema.METHOD_NOTIFICATION_MESSAGE), eq(infoNotification));
307336
}
308337

309338
@Test
@@ -379,6 +408,10 @@ void testCreateElicitationWithNullCapabilities() {
379408
assertThat(error).isInstanceOf(McpError.class)
380409
.hasMessage("Client must be initialized. Call the initialize method first!");
381410
});
411+
412+
// Verify that sendRequest was never called due to null capabilities
413+
verify(mockSession, never()).sendRequest(eq(McpSchema.METHOD_ELICITATION_CREATE), any(),
414+
any(TypeReference.class));
382415
}
383416

384417
@Test
@@ -399,6 +432,11 @@ void testCreateElicitationWithoutElicitationCapabilities() {
399432
assertThat(error).isInstanceOf(McpError.class)
400433
.hasMessage("Client must be configured with elicitation capabilities");
401434
});
435+
436+
// Verify that sendRequest was never called due to missing elicitation
437+
// capabilities
438+
verify(mockSession, never()).sendRequest(eq(McpSchema.METHOD_ELICITATION_CREATE), any(),
439+
any(TypeReference.class));
402440
}
403441

404442
@Test
@@ -543,6 +581,10 @@ void testCreateMessageWithNullCapabilities() {
543581
assertThat(error).isInstanceOf(McpError.class)
544582
.hasMessage("Client must be initialized. Call the initialize method first!");
545583
});
584+
585+
// Verify that sendRequest was never called due to null capabilities
586+
verify(mockSession, never()).sendRequest(eq(McpSchema.METHOD_SAMPLING_CREATE_MESSAGE), any(),
587+
any(TypeReference.class));
546588
}
547589

548590
@Test
@@ -564,6 +606,10 @@ void testCreateMessageWithoutSamplingCapabilities() {
564606
assertThat(error).isInstanceOf(McpError.class)
565607
.hasMessage("Client must be configured with sampling capabilities");
566608
});
609+
610+
// Verify that sendRequest was never called due to missing sampling capabilities
611+
verify(mockSession, never()).sendRequest(eq(McpSchema.METHOD_SAMPLING_CREATE_MESSAGE), any(),
612+
any(TypeReference.class));
567613
}
568614

569615
@Test

0 commit comments

Comments
 (0)