Repository: struts-site Updated Branches: refs/heads/master ad42b41cc -> 5041eb302
Exports and cleans up JUnit plugin Project: http://git-wip-us.apache.org/repos/asf/struts-site/repo Commit: http://git-wip-us.apache.org/repos/asf/struts-site/commit/5041eb30 Tree: http://git-wip-us.apache.org/repos/asf/struts-site/tree/5041eb30 Diff: http://git-wip-us.apache.org/repos/asf/struts-site/diff/5041eb30 Branch: refs/heads/master Commit: 5041eb3024e7f31b501aec9ad9f938fe80b7be78 Parents: ad42b41 Author: Lukasz Lenart <lukaszlen...@apache.org> Authored: Wed Aug 23 13:42:50 2017 +0200 Committer: Lukasz Lenart <lukaszlen...@apache.org> Committed: Wed Aug 23 13:42:50 2017 +0200 ---------------------------------------------------------------------- pom.xml | 4 +- source/_layouts/plugin.html | 1 + source/core-developers/index.md | 2 +- source/plugins/convention/index.md | 2 - source/plugins/index.md | 1 + source/plugins/junit/index.md | 138 ++++++++++++++++++++++++++++++++ 6 files changed, 143 insertions(+), 5 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/struts-site/blob/5041eb30/pom.xml ---------------------------------------------------------------------- diff --git a/pom.xml b/pom.xml index e5fbb97..420bb63 100644 --- a/pom.xml +++ b/pom.xml @@ -94,14 +94,14 @@ <argument>-a</argument> <argument>${project.build.directory}/md/attachments</argument> <argument>-o</argument> - <argument>${project.build.directory}/md/convention-plugin.md</argument> + <argument>${project.build.directory}/md/junit-plugin.md</argument> <argument>-u</argument> <argument>${confluence.user}:${confluence.password}</argument> <argument>-server</argument> <argument>https://cwiki.apache.org/confluence</argument> <argument>+gfm</argument> <argument>true</argument> - <argument>105613</argument> + <argument>2330106</argument> </arguments> </configuration> </execution> http://git-wip-us.apache.org/repos/asf/struts-site/blob/5041eb30/source/_layouts/plugin.html ---------------------------------------------------------------------- diff --git a/source/_layouts/plugin.html b/source/_layouts/plugin.html index 88d96db..1c15ec6 100644 --- a/source/_layouts/plugin.html +++ b/source/_layouts/plugin.html @@ -26,6 +26,7 @@ <article class="container"> <section class="col-md-12"> <a href="../" title="back to Plugins"><< back to Plugins</a> + [Edit on GitHub]({{ site.repository_url }}/edit/master/source/{{ page.path }}){: .edit-on-gh title="Edit this page on GitHub"} {{ content }} </section> </article> http://git-wip-us.apache.org/repos/asf/struts-site/blob/5041eb30/source/core-developers/index.md ---------------------------------------------------------------------- diff --git a/source/core-developers/index.md b/source/core-developers/index.md index 1f28710..87c2d36 100644 --- a/source/core-developers/index.md +++ b/source/core-developers/index.md @@ -35,7 +35,7 @@ Each may be configured via XML or annotations. - [Application Servers](application-servers.html) - [Performance Tuning](performance-tuning.html) - [Security](../security/) -- [Testing Actions] - export https://cwiki.apache.org/confluence/display/WW/Testing+Actions +- [Testing Actions](../plugins/junit/) - [(arsenalist)](https://depressedprogrammer.wordpress.com/2007/06/18/unit-testing-struts-2-actions-spring-junit/) - [(rosa)](http://fassisrosa.blogspot.com/2006/11/unit-testing-struts-20\.html) - [Interceptors](interceptors.html) http://git-wip-us.apache.org/repos/asf/struts-site/blob/5041eb30/source/plugins/convention/index.md ---------------------------------------------------------------------- diff --git a/source/plugins/convention/index.md b/source/plugins/convention/index.md index 424b428..4506147 100644 --- a/source/plugins/convention/index.md +++ b/source/plugins/convention/index.md @@ -6,8 +6,6 @@ title: Convention plugin # Convention Plugin {:.no_toc} -[Edit on GitHub]({{ site.repository_url }}/edit/master/source/{{ page.path }}){: .edit-on-gh title="Edit this page on GitHub"} - * Will be replaced with the ToC, excluding a header {:toc} http://git-wip-us.apache.org/repos/asf/struts-site/blob/5041eb30/source/plugins/index.md ---------------------------------------------------------------------- diff --git a/source/plugins/index.md b/source/plugins/index.md index 98ff721..653e591 100644 --- a/source/plugins/index.md +++ b/source/plugins/index.md @@ -6,4 +6,5 @@ title: Plugins (WIP) # Plugins - [Convention plugin](convention/) + - [JUnit plugin](junit/) \ No newline at end of file http://git-wip-us.apache.org/repos/asf/struts-site/blob/5041eb30/source/plugins/junit/index.md ---------------------------------------------------------------------- diff --git a/source/plugins/junit/index.md b/source/plugins/junit/index.md new file mode 100644 index 0000000..d3841b6 --- /dev/null +++ b/source/plugins/junit/index.md @@ -0,0 +1,138 @@ +--- +layout: plugin +title: Convention plugin +--- + +# JUnit plugin +{:.no_toc} + +* Will be replaced with the ToC, excluding a header +{:toc} + +The recommended way to test actions is to instantiate the action classes and test them. The _JUnit Plugin_ supports +testing actions within a Struts invocation, meaning that a full request is simulated, and the output of the action can +be tested. + +## Struts actions (without Spring) + +To test actions that do not use Spring, extend `StrutsTestCase`. The following example shows different ways of testing +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> + ``` +- Action: + ```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); + } + } + ``` + +## The template + +If you use JSPs as the template engine you won't be able to test the action output outside the container. +The [Embedded JSP Plugin](../embeddedjsp/) can be used to overcome this limitation and be able to use JSPs from +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)| +- 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| + +## Struts Actions using Spring + +Make sure to add a dependency to the [Spring Plugin](../spring/) to your `pom.xml`: + +```xml +<dependency> + <groupId>org.apache.struts</groupId> + <artifactId>struts2-spring-plugin</artifactId> + <version>STRUTS_VERSION</version> +</dependency> +``` + +If you use Spring as the object factory, the `StrutsSpringTestCase` class can be used to write your JUnits. This class +extends `StrutsTestCase` and has an `applicationContext` field of type `ApplicationContext`. + +The Spring context is loaded from `classpath\*:applicationContext.xml` by default. To provide a different location, +overwrite `getContextLocations`.