@@ -109,6 +109,17 @@ void tearDown() {
109
109
onClose ();
110
110
}
111
111
112
+ <T > void verifyInitializationTimeout (Function <McpAsyncClient , Mono <T >> operation , String action ) {
113
+ withClient (createMcpTransport (), mcpAsyncClient -> {
114
+ StepVerifier .withVirtualTime (() -> operation .apply (mcpAsyncClient ))
115
+ .expectSubscription ()
116
+ .thenAwait (getInitializationTimeout ())
117
+ .consumeErrorWith (e -> assertThat (e ).isInstanceOf (McpError .class )
118
+ .hasMessage ("Client must be initialized before " + action ))
119
+ .verify ();
120
+ });
121
+ }
122
+
112
123
@ Test
113
124
void testConstructorWithInvalidArguments () {
114
125
assertThatThrownBy (() -> McpClient .async (null ).build ()).isInstanceOf (IllegalArgumentException .class )
@@ -121,14 +132,7 @@ void testConstructorWithInvalidArguments() {
121
132
122
133
@ Test
123
134
void testListToolsWithoutInitialization () {
124
- withClient (createMcpTransport (), mcpAsyncClient -> {
125
- StepVerifier .withVirtualTime (() -> mcpAsyncClient .listTools (null ))
126
- .expectSubscription ()
127
- .thenAwait (getInitializationTimeout ())
128
- .consumeErrorWith (e -> assertThat (e ).isInstanceOf (McpError .class )
129
- .hasMessage ("Client must be initialized before listing tools" ))
130
- .verify ();
131
- });
135
+ verifyInitializationTimeout (client -> client .listTools (null ), "listing tools" );
132
136
}
133
137
134
138
@ Test
@@ -148,14 +152,7 @@ void testListTools() {
148
152
149
153
@ Test
150
154
void testPingWithoutInitialization () {
151
- withClient (createMcpTransport (), mcpAsyncClient -> {
152
- StepVerifier .withVirtualTime (() -> mcpAsyncClient .ping ())
153
- .expectSubscription ()
154
- .thenAwait (getInitializationTimeout ())
155
- .consumeErrorWith (e -> assertThat (e ).isInstanceOf (McpError .class )
156
- .hasMessage ("Client must be initialized before pinging the " + "server" ))
157
- .verify ();
158
- });
155
+ verifyInitializationTimeout (client -> client .ping (), "pinging the server" );
159
156
}
160
157
161
158
@ Test
@@ -169,16 +166,8 @@ void testPing() {
169
166
170
167
@ Test
171
168
void testCallToolWithoutInitialization () {
172
- withClient (createMcpTransport (), mcpAsyncClient -> {
173
- CallToolRequest callToolRequest = new CallToolRequest ("echo" , Map .of ("message" , ECHO_TEST_MESSAGE ));
174
-
175
- StepVerifier .withVirtualTime (() -> mcpAsyncClient .callTool (callToolRequest ))
176
- .expectSubscription ()
177
- .thenAwait (getInitializationTimeout ())
178
- .consumeErrorWith (e -> assertThat (e ).isInstanceOf (McpError .class )
179
- .hasMessage ("Client must be initialized before calling tools" ))
180
- .verify ();
181
- });
169
+ CallToolRequest callToolRequest = new CallToolRequest ("echo" , Map .of ("message" , ECHO_TEST_MESSAGE ));
170
+ verifyInitializationTimeout (client -> client .callTool (callToolRequest ), "calling tools" );
182
171
}
183
172
184
173
@ Test
@@ -212,14 +201,7 @@ void testCallToolWithInvalidTool() {
212
201
213
202
@ Test
214
203
void testListResourcesWithoutInitialization () {
215
- withClient (createMcpTransport (), mcpAsyncClient -> {
216
- StepVerifier .withVirtualTime (() -> mcpAsyncClient .listResources (null ))
217
- .expectSubscription ()
218
- .thenAwait (getInitializationTimeout ())
219
- .consumeErrorWith (e -> assertThat (e ).isInstanceOf (McpError .class )
220
- .hasMessage ("Client must be initialized before listing resources" ))
221
- .verify ();
222
- });
204
+ verifyInitializationTimeout (client -> client .listResources (null ), "listing resources" );
223
205
}
224
206
225
207
@ Test
@@ -250,14 +232,7 @@ void testMcpAsyncClientState() {
250
232
251
233
@ Test
252
234
void testListPromptsWithoutInitialization () {
253
- withClient (createMcpTransport (), mcpAsyncClient -> {
254
- StepVerifier .withVirtualTime (() -> mcpAsyncClient .listPrompts (null ))
255
- .expectSubscription ()
256
- .thenAwait (getInitializationTimeout ())
257
- .consumeErrorWith (e -> assertThat (e ).isInstanceOf (McpError .class )
258
- .hasMessage ("Client must be initialized before listing prompts" ))
259
- .verify ();
260
- });
235
+ verifyInitializationTimeout (client -> client .listPrompts (null ), "listing " + "prompts" );
261
236
}
262
237
263
238
@ Test
@@ -281,16 +256,8 @@ void testListPrompts() {
281
256
282
257
@ Test
283
258
void testGetPromptWithoutInitialization () {
284
- withClient (createMcpTransport (), mcpAsyncClient -> {
285
- GetPromptRequest request = new GetPromptRequest ("simple_prompt" , Map .of ());
286
-
287
- StepVerifier .withVirtualTime (() -> mcpAsyncClient .getPrompt (request ))
288
- .expectSubscription ()
289
- .thenAwait (getInitializationTimeout ())
290
- .consumeErrorWith (e -> assertThat (e ).isInstanceOf (McpError .class )
291
- .hasMessage ("Client must be initialized before getting prompts" ))
292
- .verify ();
293
- });
259
+ GetPromptRequest request = new GetPromptRequest ("simple_prompt" , Map .of ());
260
+ verifyInitializationTimeout (client -> client .getPrompt (request ), "getting " + "prompts" );
294
261
}
295
262
296
263
@ Test
@@ -311,14 +278,8 @@ void testGetPrompt() {
311
278
312
279
@ Test
313
280
void testRootsListChangedWithoutInitialization () {
314
- withClient (createMcpTransport (), mcpAsyncClient -> {
315
- StepVerifier .withVirtualTime (() -> mcpAsyncClient .rootsListChangedNotification ())
316
- .expectSubscription ()
317
- .thenAwait (getInitializationTimeout ())
318
- .consumeErrorWith (e -> assertThat (e ).isInstanceOf (McpError .class )
319
- .hasMessage ("Client must be initialized before sending roots list changed notification" ))
320
- .verify ();
321
- });
281
+ verifyInitializationTimeout (client -> client .rootsListChangedNotification (),
282
+ "sending roots list changed notification" );
322
283
}
323
284
324
285
@ Test
@@ -392,14 +353,7 @@ void testReadResource() {
392
353
393
354
@ Test
394
355
void testListResourceTemplatesWithoutInitialization () {
395
- withClient (createMcpTransport (), mcpAsyncClient -> {
396
- StepVerifier .withVirtualTime (() -> mcpAsyncClient .listResourceTemplates ())
397
- .expectSubscription ()
398
- .thenAwait (getInitializationTimeout ())
399
- .consumeErrorWith (e -> assertThat (e ).isInstanceOf (McpError .class )
400
- .hasMessage ("Client must be initialized before listing resource templates" ))
401
- .verify ();
402
- });
356
+ verifyInitializationTimeout (client -> client .listResourceTemplates (), "listing resource templates" );
403
357
}
404
358
405
359
@ Test
@@ -492,14 +446,8 @@ void testInitializeWithAllCapabilities() {
492
446
493
447
@ Test
494
448
void testLoggingLevelsWithoutInitialization () {
495
- withClient (createMcpTransport (),
496
- mcpAsyncClient -> StepVerifier
497
- .withVirtualTime (() -> mcpAsyncClient .setLoggingLevel (McpSchema .LoggingLevel .DEBUG ))
498
- .expectSubscription ()
499
- .thenAwait (getInitializationTimeout ())
500
- .consumeErrorWith (e -> assertThat (e ).isInstanceOf (McpError .class )
501
- .hasMessage ("Client must be initialized before setting logging level" ))
502
- .verify ());
449
+ verifyInitializationTimeout (client -> client .setLoggingLevel (McpSchema .LoggingLevel .DEBUG ),
450
+ "setting logging level" );
503
451
}
504
452
505
453
@ Test
0 commit comments