Repository: struts-site Updated Branches: refs/heads/master a350d0800 -> 0a61c9a60
Fixes section formatting Project: http://git-wip-us.apache.org/repos/asf/struts-site/repo Commit: http://git-wip-us.apache.org/repos/asf/struts-site/commit/0a61c9a6 Tree: http://git-wip-us.apache.org/repos/asf/struts-site/tree/0a61c9a6 Diff: http://git-wip-us.apache.org/repos/asf/struts-site/diff/0a61c9a6 Branch: refs/heads/master Commit: 0a61c9a6092ed3acae0fce3a236f7e707bbb9f1e Parents: a350d08 Author: Lukasz Lenart <lukaszlen...@apache.org> Authored: Wed Aug 23 13:47:53 2017 +0200 Committer: Lukasz Lenart <lukaszlen...@apache.org> Committed: Wed Aug 23 13:47:53 2017 +0200 ---------------------------------------------------------------------- source/plugins/junit/index.md | 172 ++++++++++++++++++------------------- 1 file changed, 86 insertions(+), 86 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/struts-site/blob/0a61c9a6/source/plugins/junit/index.md ---------------------------------------------------------------------- diff --git a/source/plugins/junit/index.md b/source/plugins/junit/index.md index d3841b6..fdf388d 100644 --- a/source/plugins/junit/index.md +++ b/source/plugins/junit/index.md @@ -19,80 +19,80 @@ To test actions that do not use Spring, extend `StrutsTestCase`. The following e an action: - Mapping: - ```xml - <struts> - <constant name="struts.objectFactory" value="spring"/> - <package name="test" namespace="/test" extends="struts-default"> - <action name="testAction" class="org.apache.struts2.TestAction"> - <result type="freemarker">/template.ftl</result> - </action> - </package> - </struts> - ``` +```xml +<struts> + <constant name="struts.objectFactory" value="spring"/> + <package name="test" namespace="/test" extends="struts-default"> + <action name="testAction" class="org.apache.struts2.TestAction"> + <result type="freemarker">/template.ftl</result> + </action> + </package> +</struts> +``` - Action: - ```java - public class TestAction extends ActionSupport { - private String name; - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - } - ``` +```java +public class TestAction extends ActionSupport { + private String name; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} +``` - JUnit: - ```java - package org.apache.struts2; - - import org.apache.struts2.dispatcher.mapper.ActionMapping; - - import java.util.HashMap; - import java.io.UnsupportedEncodingException; - - import com.opensymphony.xwork2.ActionProxy; - import com.opensymphony.xwork2.Action; - - import javax.servlet.ServletException; - - public class StrutsTestCaseTest extends StrutsTestCase { - public void testGetActionMapping() { - ActionMapping mapping = getActionMapping("/test/testAction.action"); - assertNotNull(mapping); - assertEquals("/test", mapping.getNamespace()); - assertEquals("testAction", mapping.getName()); - } - - public void testGetActionProxy() throws Exception { - //set parameters before calling getActionProxy - request.setParameter("name", "FD"); - - ActionProxy proxy = getActionProxy("/test/testAction.action"); - assertNotNull(proxy); - - TestAction action = (TestAction) proxy.getAction(); - assertNotNull(action); - - String result = proxy.execute(); - assertEquals(Action.SUCCESS, result); - assertEquals("FD", action.getName()); - } - - public void testExecuteAction() throws ServletException, UnsupportedEncodingException { - String output = executeAction("/test/testAction.action"); - assertEquals("Hello", output); - } - - public void testGetValueFromStack() throws ServletException, UnsupportedEncodingException { - request.setParameter("name", "FD"); - executeAction("/test/testAction.action"); - String name = (String) findValueAfterExecute("name"); - assertEquals("FD", name); - } - } - ``` +```java +package org.apache.struts2; + +import org.apache.struts2.dispatcher.mapper.ActionMapping; + +import java.util.HashMap; +import java.io.UnsupportedEncodingException; + +import com.opensymphony.xwork2.ActionProxy; +import com.opensymphony.xwork2.Action; + +import javax.servlet.ServletException; + +public class StrutsTestCaseTest extends StrutsTestCase { + public void testGetActionMapping() { + ActionMapping mapping = getActionMapping("/test/testAction.action"); + assertNotNull(mapping); + assertEquals("/test", mapping.getNamespace()); + assertEquals("testAction", mapping.getName()); + } + + public void testGetActionProxy() throws Exception { + //set parameters before calling getActionProxy + request.setParameter("name", "FD"); + + ActionProxy proxy = getActionProxy("/test/testAction.action"); + assertNotNull(proxy); + + TestAction action = (TestAction) proxy.getAction(); + assertNotNull(action); + + String result = proxy.execute(); + assertEquals(Action.SUCCESS, result); + assertEquals("FD", action.getName()); + } + + public void testExecuteAction() throws ServletException, UnsupportedEncodingException { + String output = executeAction("/test/testAction.action"); + assertEquals("Hello", output); + } + + public void testGetValueFromStack() throws ServletException, UnsupportedEncodingException { + request.setParameter("name", "FD"); + executeAction("/test/testAction.action"); + String name = (String) findValueAfterExecute("name"); + assertEquals("FD", name); + } +} +``` ## The template @@ -103,21 +103,21 @@ the classpath and outside the container. There are several utility methods and mock objects defined in StrutsTestCase which can be used to facilitate the testing: - Methods: - |Method Name|Description| - |-----------|-----------| - |executeAction(String)|Pass the url for the action, and it will return the output of the action. This output **is not** the action result, like "success", but what would be written to the result stream. To use this the actions must be using a result type that can be read from the classpath, like FreeMarker, velocity, etc (if you are using the experimental Embedded JSP Plugin, you can use JSPs also)| - |getActionProxy(String)|Builds an action proxy that can be used to invoke an action, by calling execute() on the returned proxy object. The return value of execute() is the action result, like "success"| - |getActionMapping(String)|Gets an ActionMapping for the url| - |injectStrutsDependencies(object)|Injects Struts dependencies into an object (dependencies are marked with Inject)| - |findValueAfterExecute(String)|Finds an object in the value stack, after an action has been executed| - |applyAdditionalParams(ActionContext)|Can be overwritten in subclass to provide additional params and settings used during action invocation| - |createAction(Class)|Can be used to instantiate an action which requires framework's dependencies to be injected (e.g. extending ActionSupport requires inject some internal dependencies)| +|Method Name|Description| +|-----------|-----------| +|executeAction(String)|Pass the url for the action, and it will return the output of the action. This output **is not** the action result, like "success", but what would be written to the result stream. To use this the actions must be using a result type that can be read from the classpath, like FreeMarker, velocity, etc (if you are using the experimental Embedded JSP Plugin, you can use JSPs also)| +|getActionProxy(String)|Builds an action proxy that can be used to invoke an action, by calling execute() on the returned proxy object. The return value of execute() is the action result, like "success"| +|getActionMapping(String)|Gets an ActionMapping for the url| +|injectStrutsDependencies(object)|Injects Struts dependencies into an object (dependencies are marked with Inject)| +|findValueAfterExecute(String)|Finds an object in the value stack, after an action has been executed| +|applyAdditionalParams(ActionContext)|Can be overwritten in subclass to provide additional params and settings used during action invocation| +|createAction(Class)|Can be used to instantiate an action which requires framework's dependencies to be injected (e.g. extending ActionSupport requires inject some internal dependencies)| - Fields: - |Field|Description| - |-----|-----------| - |MockHttpServletRequest request|The request that will be passed to Struts. Make sure to set parameters in this object before calling methods like getActionProxy| - |MockHttpServletResponse response|The response object passed to Struts, you can use this class to test the output, response headers, etc| - |MockServletContext servletContext|The servlet context object passed to Struts| +|Field|Description| +|-----|-----------| +|MockHttpServletRequest request|The request that will be passed to Struts. Make sure to set parameters in this object before calling methods like getActionProxy| +|MockHttpServletResponse response|The response object passed to Struts, you can use this class to test the output, response headers, etc| +|MockServletContext servletContext|The servlet context object passed to Struts| ## Struts Actions using Spring