2727import org .apache .catalina .LifecycleException ;
2828import org .apache .catalina .LifecycleState ;
2929import org .apache .catalina .startup .Tomcat ;
30+ import org .junit .jupiter .api .AfterAll ;
3031import org .junit .jupiter .api .AfterEach ;
32+ import org .junit .jupiter .api .BeforeAll ;
3133import org .junit .jupiter .api .BeforeEach ;
3234import org .junit .jupiter .api .Test ;
3335import org .junit .jupiter .api .Timeout ;
@@ -44,14 +46,42 @@ class HttpServletSseIntegrationTests extends AbstractMcpClientServerIntegrationT
4446
4547 private static final String CUSTOM_MESSAGE_ENDPOINT = "/otherPath/mcp/message" ;
4648
47- private HttpServletSseServerTransportProvider mcpServerTransportProvider ;
49+ // Tomcat is started once for the whole class; each test swaps in its own transport
50+ private static final TomcatTestUtil .DelegatingServlet MCP_SERVLET = new TomcatTestUtil .DelegatingServlet ();
51+
52+ private static Tomcat tomcat ;
4853
49- private Tomcat tomcat ;
54+ private HttpServletSseServerTransportProvider mcpServerTransportProvider ;
5055
5156 static Stream <Arguments > clientsForTesting () {
5257 return Stream .of (Arguments .of ("httpclient" ));
5358 }
5459
60+ @ BeforeAll
61+ public static void beforeAll () {
62+ tomcat = TomcatTestUtil .createTomcatServer ("" , PORT , MCP_SERVLET );
63+ try {
64+ tomcat .start ();
65+ assertThat (tomcat .getServer ().getState ()).isEqualTo (LifecycleState .STARTED );
66+ }
67+ catch (Exception e ) {
68+ throw new RuntimeException ("Failed to start Tomcat" , e );
69+ }
70+ }
71+
72+ @ AfterAll
73+ public static void afterAll () {
74+ if (tomcat != null ) {
75+ try {
76+ tomcat .stop ();
77+ tomcat .destroy ();
78+ }
79+ catch (LifecycleException e ) {
80+ throw new RuntimeException ("Failed to stop Tomcat" , e );
81+ }
82+ }
83+ }
84+
5585 @ BeforeEach
5686 public void before () {
5787 // Create and configure the transport provider
@@ -61,15 +91,7 @@ public void before() {
6191 .sseEndpoint (CUSTOM_SSE_ENDPOINT )
6292 .maxRequestSize (MAX_REQUEST_SIZE )
6393 .build ();
64-
65- tomcat = TomcatTestUtil .createTomcatServer ("" , PORT , mcpServerTransportProvider );
66- try {
67- tomcat .start ();
68- assertThat (tomcat .getServer ().getState ()).isEqualTo (LifecycleState .STARTED );
69- }
70- catch (Exception e ) {
71- throw new RuntimeException ("Failed to start Tomcat" , e );
72- }
94+ MCP_SERVLET .setDelegate (mcpServerTransportProvider );
7395
7496 clientBuilders
7597 .put ("httpclient" ,
@@ -93,15 +115,6 @@ public void after() {
93115 if (mcpServerTransportProvider != null ) {
94116 mcpServerTransportProvider .closeGracefully ().block ();
95117 }
96- if (tomcat != null ) {
97- try {
98- tomcat .stop ();
99- tomcat .destroy ();
100- }
101- catch (LifecycleException e ) {
102- throw new RuntimeException ("Failed to stop Tomcat" , e );
103- }
104- }
105118 }
106119
107120 @ Override
0 commit comments