You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@struts.apache.org by lu...@apache.org on 2026/09/16 07:23:44 UTC

(struts) branch fix/WW-3353-rest-test-case created (now d9b58cf01)

This is an automated email from the ASF dual-hosted git repository.

lukaszlenart pushed a change to branch fix/WW-3353-rest-test-case
in repository https://gitbox.apache.org/repos/asf/struts.git


      at d9b58cf01 WW-3353 fix(junit): make StrutsRestTestCase work against a REST/Convention app

This branch includes the following new commits:

     new d9b58cf01 WW-3353 fix(junit): make StrutsRestTestCase work against a REST/Convention app

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



(struts) 01/01: WW-3353 fix(junit): make StrutsRestTestCase work against a REST/Convention app

Posted by lu...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

lukaszlenart pushed a commit to branch fix/WW-3353-rest-test-case
in repository https://gitbox.apache.org/repos/asf/struts.git

commit d9b58cf01b31b5f5c05cbb9af560dc4c5130b77e
Author: Lukasz Lenart <lu...@apache.org>
AuthorDate: Wed Sep 16 09:23:34 2026 +0200

    WW-3353 fix(junit): make StrutsRestTestCase work against a REST/Convention app
    
    StrutsRestTestCase could not run a REST action to completion:
    
    - initServletMockObjects() built the MockServletContext before
      assigning ConventionPluginResourceLoader, so the loader was a dead
      assignment and Convention could not resolve results under
      /WEB-INF/content ("No result defined for action ... and result show").
    - spring-test's MockHttpServletResponse needs spring-web, which the
      plugin only reached through its optional struts2-spring-plugin
      dependency; a project without the Spring plugin failed with
      NoClassDefFoundError: org/springframework/http/MediaType.
    
    Adds the first test of StrutsRestTestCase, in rest-showcase, executing
    GET /orders/3 through the proxy and rendering /orders/3.json. The
    showcase gets the junit plugin at test scope and the servlet/JSP APIs
    at provided scope, as apps/showcase already declares.
    
    Co-Authored-By: Claude Opus 5 (1M context) <no...@anthropic.com>
---
 apps/rest-showcase/pom.xml                         | 15 +++++++
 .../example/OrdersControllerRestTestCaseTest.java  | 47 ++++++++++++++++++++++
 plugins/junit/pom.xml                              |  4 ++
 .../apache/struts2/junit/StrutsRestTestCase.java   |  2 +-
 4 files changed, 67 insertions(+), 1 deletion(-)

diff --git a/apps/rest-showcase/pom.xml b/apps/rest-showcase/pom.xml
index 158c522db..74131067f 100644
--- a/apps/rest-showcase/pom.xml
+++ b/apps/rest-showcase/pom.xml
@@ -67,6 +67,21 @@
             <artifactId>junit</artifactId>
             <scope>test</scope>
         </dependency>
+        <dependency>
+            <groupId>org.apache.struts</groupId>
+            <artifactId>struts2-junit-plugin</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>jakarta.servlet</groupId>
+            <artifactId>jakarta.servlet-api</artifactId>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>jakarta.servlet.jsp</groupId>
+            <artifactId>jakarta.servlet.jsp-api</artifactId>
+            <scope>provided</scope>
+        </dependency>
 
         <dependency>
             <groupId>net.sourceforge.jwebunit</groupId>
diff --git a/apps/rest-showcase/src/test/java/org/demo/rest/example/OrdersControllerRestTestCaseTest.java b/apps/rest-showcase/src/test/java/org/demo/rest/example/OrdersControllerRestTestCaseTest.java
new file mode 100644
index 000000000..3bd143f4f
--- /dev/null
+++ b/apps/rest-showcase/src/test/java/org/demo/rest/example/OrdersControllerRestTestCaseTest.java
@@ -0,0 +1,47 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.demo.rest.example;
+
+import org.apache.struts2.ActionProxy;
+import org.apache.struts2.junit.StrutsRestTestCase;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+public class OrdersControllerRestTestCaseTest extends StrutsRestTestCase<OrdersController> {
+
+    @Test
+    public void showBindsTheIdFromTheRequestPath() throws Exception {
+        ActionProxy proxy = getActionProxy("GET", "/orders/3");
+
+        assertEquals("show", proxy.getMethod());
+        proxy.execute();
+
+        OrdersController controller = (OrdersController) proxy.getAction();
+        assertEquals("Bob", ((Order) controller.getModel()).getClientName());
+    }
+
+    @Test
+    public void executeActionRendersTheOrderAsJson() throws Exception {
+        String json = executeAction("GET", "/orders/3.json");
+
+        assertTrue(json, json.contains("\"clientName\":\"Bob\""));
+    }
+}
diff --git a/plugins/junit/pom.xml b/plugins/junit/pom.xml
index 332296d97..361b23ffc 100644
--- a/plugins/junit/pom.xml
+++ b/plugins/junit/pom.xml
@@ -44,6 +44,10 @@
             <groupId>org.springframework</groupId>
             <artifactId>spring-context</artifactId>
         </dependency>
+        <dependency>
+            <groupId>org.springframework</groupId>
+            <artifactId>spring-web</artifactId>
+        </dependency>
         <dependency>
             <groupId>org.apache.struts</groupId>
             <artifactId>struts2-spring-plugin</artifactId>
diff --git a/plugins/junit/src/main/java/org/apache/struts2/junit/StrutsRestTestCase.java b/plugins/junit/src/main/java/org/apache/struts2/junit/StrutsRestTestCase.java
index ebfd95a97..0112e6b8d 100644
--- a/plugins/junit/src/main/java/org/apache/struts2/junit/StrutsRestTestCase.java
+++ b/plugins/junit/src/main/java/org/apache/struts2/junit/StrutsRestTestCase.java
@@ -136,10 +136,10 @@ public class StrutsRestTestCase<T> extends StrutsJUnit4TestCase<T> {
 
     @Override
     protected void initServletMockObjects() {
+        resourceLoader = new ConventionPluginResourceLoader();
         servletContext = new MockServletContext(resourceLoader);
         response = new MockHttpServletResponse();
         request = new MockHttpServletRequest();
         pageContext = new MockPageContext(servletContext, request, response);
-        resourceLoader = new ConventionPluginResourceLoader();
     }
 }