This is an automated email from the ASF dual-hosted git repository.
lukaszlenart pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/struts.git
The following commit(s) were added to refs/heads/main by this push:
new 9831e0f6b WW-3353 fix(junit): make StrutsRestTestCase work against a
REST/Convention app (#1956)
9831e0f6b is described below
commit 9831e0f6bd53e54d11adb80765f30a76ec6beeef
Author: Lukasz Lenart <[email protected]>
AuthorDate: Wed Sep 16 10:25:29 2026 +0200
WW-3353 fix(junit): make StrutsRestTestCase work against a REST/Convention
app (#1956)
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) <[email protected]>
---
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();
}
}