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 2017/04/03 10:50:42 UTC

[3/6] struts-site git commit: cleaned up unit-testing page

cleaned up unit-testing page


Project: http://git-wip-us.apache.org/repos/asf/struts-site/repo
Commit: http://git-wip-us.apache.org/repos/asf/struts-site/commit/c7614476
Tree: http://git-wip-us.apache.org/repos/asf/struts-site/tree/c7614476
Diff: http://git-wip-us.apache.org/repos/asf/struts-site/diff/c7614476

Branch: refs/heads/master
Commit: c7614476981b5fca3041bd572c5b2ee55c649898
Parents: f73b9c7
Author: Stefaan Dutry <st...@gmail.com>
Authored: Sun Apr 2 15:50:17 2017 +0200
Committer: Stefaan Dutry <st...@gmail.com>
Committed: Sun Apr 2 15:50:17 2017 +0200

----------------------------------------------------------------------
 source/getting-started/unit-testing.md | 94 +++++++++--------------------
 1 file changed, 30 insertions(+), 64 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/struts-site/blob/c7614476/source/getting-started/unit-testing.md
----------------------------------------------------------------------
diff --git a/source/getting-started/unit-testing.md b/source/getting-started/unit-testing.md
index 8fbf911..0a9d8a8 100644
--- a/source/getting-started/unit-testing.md
+++ b/source/getting-started/unit-testing.md
@@ -6,54 +6,38 @@ title: Unit testing
 
 The example code for this tutorial, unit_testing, is available at [https://github.com/apache/struts-examples](https://github.com/apache/struts-examples)
 
-> 
+__Introduction__
 
-#####Introduction#####
+Struts 2 supports running unit tests of methods in the Struts Action class with the [Struts 2 JUnit plugin](//struts.apache.org/docs/junit-plugin.html). The JUnit plugin allows you to test methods of an Action class from within the Struts 2 framework. The Struts Servlet filter and interceptors fire just as if your application was running on a Servlet container.
 
-Struts 2 supports running unit tests of methods in the Struts Action class with the [Struts 2 JUnit plugin](http://struts.apache.org/2.3.1.2/docs/junit-plugin.html)^[http://struts.apache.org/2.3.1.2/docs/junit-plugin.html]. The JUnit plugin allows you to test methods of an Action class from within the Struts 2 framework. The Struts Servlet filter and interceptors fire just as if your application was running on a Servlet container.
+The [Struts 2 user mailing list](http://struts.apache.org/mail.html) is an excellent place to get help. If you are having a problem getting the tutorial example applications to work search the Struts 2 mailing list. If you don't find an answer to your problem, post a question on the mailing list.
 
-
-
-| The [Struts 2 user mailing list](http://struts.apache.org/mail.html)^[http://struts.apache.org/mail.html] is an excellent place to get help. If you are having a problem getting the tutorial example applications to work search the Struts 2 mailing list. If you don't find an answer to your problem, post a question on the mailing list.
-
-| 
-
-#####Setup#####
+__Setup__
 
 The Struts 2 JUnit plugin jar file must be on your application's class path. In the example application (see info above) the pom.xml includes a dependency for the struts2-junit-plugin. There are numerous transitive dependencies, including to JUnit and the Spring framework.
 
-#####Writing A Unit Test#####
+__Writing A Unit Test__
 
 In the example application, the Register Action class includes using the validate method. This method is automatically executed by the Struts 2 framework prior to the execute method. Additionally, this method needs the values from the user's input on the form to already have been provided to the instance fields of the Action class (this work is done by another Struts 2 interceptor). So it would be difficult to test the validate method without the overall Struts 2 framework running.
 
 To use the Struts 2 plugin to ensure the Strut 2 framework runs as part of the test, you need to have your JUnit test class extend StrutsTestCase (see RegisterTest class in the example application).
 
-
-> 
-
-> 
-
-> Note that the Struts 2 JUnit plugin can be used to design unit tests of other Action class methods such as the input method and also to test methods of a custom interceptor you add to the interceptor stack. Also in this example, the test is for validation performed in the _validate method_ . But the same type of test would work if the validation was done using [XML file validation](#PAGE_20644608).
-
-> 
+Note that the Struts 2 JUnit plugin can be used to design unit tests of other Action class methods such as the input method and also to test methods of a custom interceptor you add to the interceptor stack. Also in this example, the test is for validation performed in the [validate method](form-validation.html) . But the same type of test would work if the validation was done using [XML file validation](form-validation-using-xml.html).
 
 To test the validate method we want Struts to call the Struts action that will cause the Action class's validate and execute methods to be run. In the example application this action is register.
 
 **struts.xml**
 
-
-~~~~~~~
-	  <action name="register" class="org.apache.struts.register.action.Register" method="execute">
-		<result name="success">/thankyou.jsp</result>
-		<result name="input">/register.jsp</result>
-	  </action>
-
-
-~~~~~~~
+```xml
+    <action name="register" class="org.apache.struts.register.action.Register" method="execute">
+        <result name="success">/thankyou.jsp</result>
+        <result name="input">/register.jsp</result>
+    </action>
+```
 
 Remember the validate method will be called automatically by the framework before calling the execute method. If validation fails the Struts framework will return "input". If there are no validation errors then the framework will call the execute method and return whatever String the execute method returns.
 
-#####Test Validation Should Pass#####
+__Test Validation Should Pass__
 
 For our first test we'll test that there should be no validation errors. In the normal flow of this application the user would first enter the form data shown on the register.jsp page.
 
@@ -63,33 +47,24 @@ The input fields for the form have the following name values: personBean.firstNa
 
 **testExecuteValidationPasses method from RegisterTest class**
 
-
-~~~~~~~
+```java
 @Test
 public void testExecuteValidationPasses() throws Exception() {
+    request.setParameter("personBean.firstName", "Bruce");
+    request.setParameter("personBean.lastName", "Phillips");
+    request.setParameter("personBean.email", "bphillips@ku.edu");
+    request.setParameter("personBean.age", "19");
 
-  request.setParameter("personBean.firstName", "Bruce");
+    ActionProxy actionProxy = getActionProxy("/register.action");
+    Register action = (Register) actionProxy.getAction() ;
 
-  request.setParameter("personBean.lastName", "Phillips");
-		
-  request.setParameter("personBean.email", "bphillips@ku.edu");
-		
-  request.setParameter("personBean.age", "19");
+    assertNotNull("The action is null but should not be.", action);
 
-  ActionProxy actionProxy = getActionProxy("/register.action");
-
-  Register action = (Register) actionProxy.getAction() ;
+    String result = actionProxy.execute();
 
-  assertNotNull("The action is null but should not be.", action);
-
-  String result - actionProxy.execute();
-
-  assertEquals("The execute method did not return " + ActionSupport.SUCCESS + " but should have.", ActionSupport.SUCCESS, result);
-  
+    assertEquals("The execute method did not return " + ActionSupport.SUCCESS + " but should have.", ActionSupport.SUCCESS, result);
 }
-
-
-~~~~~~~
+```
 
 The first statements in the test method use the request object to set the values of any request parameters. These simulate the values the user would enter into the form fields. Note how the first argument to setParameter is the same as the value of the name attribute in the Struts textfield tag in the register.jsp page.
 
@@ -103,42 +78,33 @@ After that I can call actionProxy.execute(). This causes the Struts 2 framework
 
 So in the next statement, I check that success was returned.
 
-#####Test Validation Should Fail#####
+__Test Validation Should Fail__
 
 To test that validation should fail, I just need to have a test method that doesn't provide input for a form field. For example, in the validate method of the Register Action class, is a test to ensure the user has entered some information for the personBean.firstName input field. In the test method I would just not use the request object to set a parameter for that field.
 
 **testExecuteValidationFailsMissingFirstName method from RegisterTest class**
 
-
-~~~~~~~
+```java
 @Test
 public void testExecuteValidationFailsMissingFirstName() throws Exception() {
-
   //request.setParameter("personBean.firstName", "Bruce");
-
   request.setParameter("personBean.lastName", "Phillips");
-		
   request.setParameter("personBean.email", "bphillips@ku.edu");
-		
   request.setParameter("personBean.age", "19");
 
   ActionProxy actionProxy = getActionProxy("/register.action");
-
   Register action = (Register) actionProxy.getAction() ;
 
   assertNotNull("The action is null but should not be.", action);
 
-  String result - actionProxy.execute();
+  String result = actionProxy.execute();
 
   assertEquals("The execute method did not return " + ActionSupport.INPUT + " but should have.", ActionSupport.INPUT, result);
-  
 }
-
-
-~~~~~~~
+```
 
 In the last assertEquals statement my test checks that the Struts 2 framework returned "input" as that is what the Struts 2 framework will return if the validation adds a field or action error.
 
-#####Summary#####
+__Summary__
 
-There is much more you can do with the Struts 2 JUnit plugin to help you test the methods of your Action class in conjunction with the Struts 2 fraemwork. If your Struts 2 application uses Spring to inject dependencies into the Action class then the Struts 2 JUnit Plugin has a StrutsSpringTestCase that your test class should extend. Please read [Testing Actions](http://struts.apache.org/2.3.1.2/docs/testing-actions.html)^[http://struts.apache.org/2.3.1.2/docs/testing-actions.html] to learn more.
+There is much more you can do with the Struts 2 JUnit plugin to help you test the methods of your Action class in conjunction with the Struts 2 framemwork. If your Struts 2 application uses Spring to inject dependencies into the Action class then the Struts 2 JUnit Plugin has a StrutsSpringTestCase that your test class should extend. Please read [Testing Actions](//struts.apache.org/docs/testing-actions.html) to learn more.