Updates production by Jenkins Project: http://git-wip-us.apache.org/repos/asf/struts-site/repo Commit: http://git-wip-us.apache.org/repos/asf/struts-site/commit/e8baeb93 Tree: http://git-wip-us.apache.org/repos/asf/struts-site/tree/e8baeb93 Diff: http://git-wip-us.apache.org/repos/asf/struts-site/diff/e8baeb93 Branch: refs/heads/asf-site Commit: e8baeb931e48f7cd360160b6923af48d534886f4 Parents: c4f3bdc Author: jenkins Authored: Wed Aug 23 11:43:40 2017 +0000 Committer: jenkins Committed: Wed Aug 23 11:43:40 2017 +0000 ---------------------------------------------------------------------- content/core-developers/index.html | 2 +- content/plugins/convention/index.html | 3 +- content/plugins/index.html | 1 + content/plugins/junit/index.html | 327 +++++++++++++++++++++++++++++ 4 files changed, 330 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/struts-site/blob/e8baeb93/content/core-developers/index.html ---------------------------------------------------------------------- diff --git a/content/core-developers/index.html b/content/core-developers/index.html index f8a2c41..20ab91c 100644 --- a/content/core-developers/index.html +++ b/content/core-developers/index.html @@ -164,7 +164,7 @@ Each may be configured via XML or annotations.

  • Application Servers
  • Performance Tuning
  • Security
  • -
  • [Testing Actions] - export https://cwiki.apache.org/confluence/display/WW/Testing+Actions +
  • Testing Actions
    • (arsenalist)
    • (rosa)
    • http://git-wip-us.apache.org/repos/asf/struts-site/blob/e8baeb93/content/plugins/convention/index.html ---------------------------------------------------------------------- diff --git a/content/plugins/convention/index.html b/content/plugins/convention/index.html index d3dff08..e1c7631 100644 --- a/content/plugins/convention/index.html +++ b/content/plugins/convention/index.html @@ -124,10 +124,9 @@
      << back to Plugins + [Edit on GitHub](https://github.com/apache/struts-site/edit/master/source/plugins/convention/index.md){: .edit-on-gh title="Edit this page on GitHub"}

      Convention Plugin

      -

      Edit on GitHub

      -
      • Introduction
      • Setup
      • http://git-wip-us.apache.org/repos/asf/struts-site/blob/e8baeb93/content/plugins/index.html ---------------------------------------------------------------------- diff --git a/content/plugins/index.html b/content/plugins/index.html index e7e8d1d..4872474 100644 --- a/content/plugins/index.html +++ b/content/plugins/index.html @@ -127,6 +127,7 @@ http://git-wip-us.apache.org/repos/asf/struts-site/blob/e8baeb93/content/plugins/junit/index.html ---------------------------------------------------------------------- diff --git a/content/plugins/junit/index.html b/content/plugins/junit/index.html new file mode 100644 index 0000000..b0fd087 --- /dev/null +++ b/content/plugins/junit/index.html @@ -0,0 +1,327 @@ + + + + + + + + + + Convention plugin + + + + + + + + + + + + + + + Fork me on GitHub + + +
        + +
        + + +
        +
        + << back to Plugins + [Edit on GitHub](https://github.com/apache/struts-site/edit/master/source/plugins/junit/index.md){: .edit-on-gh title="Edit this page on GitHub"} +

        JUnit plugin

        + + + +

        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 + + + + + /template.ftl + + + +

          ```

          +
        • +
        • +

          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 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 to your pom.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.

        + +
        +
        + + +
        +
        + Copyright © 2000-2016 The Apache Software Foundation . + All Rights Reserved. +
        +
        + Apache Struts, Struts, Apache, the Apache feather logo, and the Apache Struts project logos are + trademarks of The Apache Software Foundation. +
        +
        Logo and website design donated by SoftwareMill.
        +
        + + + + +
        + + + + + + + +