struts-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lukaszlen...@apache.org
Subject svn commit: r1009674 [2/2] - in /websites/production/struts/content/getting-started: annotations.html exclude-parameters.html http-session.html introducing-interceptors.html preperable-interface.html unit-testing.html
Date Mon, 03 Apr 2017 10:51:24 GMT
Modified: websites/production/struts/content/getting-started/unit-testing.html
==============================================================================
--- websites/production/struts/content/getting-started/unit-testing.html (original)
+++ websites/production/struts/content/getting-started/unit-testing.html Mon Apr  3 10:51:24
2017
@@ -125,71 +125,38 @@
 
 <p>The example code for this tutorial, unit_testing, is available at <a href="https://github.com/apache/struts-examples">https://github.com/apache/struts-examples</a></p>
 
-<blockquote>
+<p><strong>Introduction</strong></p>
 
-</blockquote>
+<p>Struts 2 supports running unit tests of methods in the Struts Action class with
the <a href="//struts.apache.org/docs/junit-plugin.html">Struts 2 JUnit plugin</a>.
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.</p>
 
-<p>#####Introduction#####</p>
+<p>The <a href="http://struts.apache.org/mail.html">Struts 2 user mailing list</a>
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.</p>
 
-<p>Struts 2 supports running unit tests of methods in the Struts Action class with
the <a href="http://struts.apache.org/2.3.1.2/docs/junit-plugin.html">Struts 2 JUnit
plugin</a>^[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.</p>
-
-<table>
-  <tbody>
-    <tr>
-      <td>The <a href="http://struts.apache.org/mail.html">Struts 2 user mailing
list</a>^[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.</td>
-    </tr>
-  </tbody>
-</table>
-
-<table>
-  <tbody>
-    <tr>
-    </tr>
-  </tbody>
-</table>
-
-<p>#####Setup#####</p>
+<p><strong>Setup</strong></p>
 
 <p>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.</p>
 
-<p>#####Writing A Unit Test#####</p>
+<p><strong>Writing A Unit Test</strong></p>
 
 <p>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.</p>
 
 <p>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).</p>
 
-<blockquote>
-
-</blockquote>
-
-<blockquote>
-
-</blockquote>
-
-<blockquote>
-  <p>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 <em>validate method</em> . But the same type of test would work if the
validation was done using <a href="#PAGE_20644608">XML file validation</a>.</p>
-</blockquote>
-
-<blockquote>
-
-</blockquote>
+<p>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 <a href="form-validation.html">validate method</a> . But the same type of
test would work if the validation was done using <a href="form-validation-using-xml.html">XML
file validation</a>.</p>
 
 <p>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.</p>
 
 <p><strong>struts.xml</strong></p>
 
-<div class="highlighter-rouge"><pre class="highlight"><code>	  &lt;action
name="register" class="org.apache.struts.register.action.Register" method="execute"&gt;
-		&lt;result name="success"&gt;/thankyou.jsp&lt;/result&gt;
-		&lt;result name="input"&gt;/register.jsp&lt;/result&gt;
-	  &lt;/action&gt;
-
-
+<div class="highlighter-rouge"><pre class="highlight"><code>    <span
class="nt">&lt;action</span> <span class="na">name=</span><span
class="s">"register"</span> <span class="na">class=</span><span class="s">"org.apache.struts.register.action.Register"</span>
<span class="na">method=</span><span class="s">"execute"</span><span
class="nt">&gt;</span>
+        <span class="nt">&lt;result</span> <span class="na">name=</span><span
class="s">"success"</span><span class="nt">&gt;</span>/thankyou.jsp<span
class="nt">&lt;/result&gt;</span>
+        <span class="nt">&lt;result</span> <span class="na">name=</span><span
class="s">"input"</span><span class="nt">&gt;</span>/register.jsp<span
class="nt">&lt;/result&gt;</span>
+    <span class="nt">&lt;/action&gt;</span>
 </code></pre>
 </div>
 
 <p>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.</p>
 
-<p>#####Test Validation Should Pass#####</p>
+<p><strong>Test Validation Should Pass</strong></p>
 
 <p>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.</p>
 
@@ -199,30 +166,22 @@
 
 <p><strong>testExecuteValidationPasses method from RegisterTest class</strong></p>
 
-<div class="highlighter-rouge"><pre class="highlight"><code>@Test
-public void testExecuteValidationPasses() throws Exception() {
-
-  request.setParameter("personBean.firstName", "Bruce");
+<div class="highlighter-rouge"><pre class="highlight"><code><span class="nd">@Test</span>
+<span class="kd">public</span> <span class="kt">void</span> <span
class="nf">testExecuteValidationPasses</span><span class="p">(</span><span
class="o">)</span> <span class="kd">throws</span> <span class="n">Exception</span><span
class="o">()</span> <span class="o">{</span>
+    <span class="n">request</span><span class="o">.</span><span
class="na">setParameter</span><span class="o">(</span><span class="s">"personBean.firstName"</span><span
class="o">,</span> <span class="s">"Bruce"</span><span class="o">);</span>
+    <span class="n">request</span><span class="o">.</span><span
class="na">setParameter</span><span class="o">(</span><span class="s">"personBean.lastName"</span><span
class="o">,</span> <span class="s">"Phillips"</span><span class="o">);</span>
+    <span class="n">request</span><span class="o">.</span><span
class="na">setParameter</span><span class="o">(</span><span class="s">"personBean.email"</span><span
class="o">,</span> <span class="s">"bphillips@ku.edu"</span><span
class="o">);</span>
+    <span class="n">request</span><span class="o">.</span><span
class="na">setParameter</span><span class="o">(</span><span class="s">"personBean.age"</span><span
class="o">,</span> <span class="s">"19"</span><span class="o">);</span>
+
+    <span class="n">ActionProxy</span> <span class="n">actionProxy</span>
<span class="o">=</span> <span class="n">getActionProxy</span><span
class="o">(</span><span class="s">"/register.action"</span><span class="o">);</span>
+    <span class="n">Register</span> <span class="n">action</span>
<span class="o">=</span> <span class="o">(</span><span class="n">Register</span><span
class="o">)</span> <span class="n">actionProxy</span><span class="o">.</span><span
class="na">getAction</span><span class="o">()</span> <span class="o">;</span>
 
-  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();
-
-  assertEquals("The execute method did not return " + ActionSupport.SUCCESS + " but should
have.", ActionSupport.SUCCESS, result);
-  
-}
+    <span class="n">assertNotNull</span><span class="o">(</span><span
class="s">"The action is null but should not be."</span><span class="o">,</span>
<span class="n">action</span><span class="o">);</span>
 
+    <span class="n">String</span> <span class="n">result</span> <span
class="o">=</span> <span class="n">actionProxy</span><span class="o">.</span><span
class="na">execute</span><span class="o">();</span>
 
+    <span class="n">assertEquals</span><span class="o">(</span><span
class="s">"The execute method did not return "</span> <span class="o">+</span>
<span class="n">ActionSupport</span><span class="o">.</span><span
class="na">SUCCESS</span> <span class="o">+</span> <span class="s">"
but should have."</span><span class="o">,</span> <span class="n">ActionSupport</span><span
class="o">.</span><span class="na">SUCCESS</span><span class="o">,</span>
<span class="n">result</span><span class="o">);</span>
+<span class="o">}</span>
 </code></pre>
 </div>
 
@@ -238,44 +197,36 @@ public void testExecuteValidationPasses(
 
 <p>So in the next statement, I check that success was returned.</p>
 
-<p>#####Test Validation Should Fail#####</p>
+<p><strong>Test Validation Should Fail</strong></p>
 
 <p>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.</p>
 
 <p><strong>testExecuteValidationFailsMissingFirstName method from RegisterTest
class</strong></p>
 
-<div class="highlighter-rouge"><pre class="highlight"><code>@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();
+<div class="highlighter-rouge"><pre class="highlight"><code><span class="nd">@Test</span>
+<span class="kd">public</span> <span class="kt">void</span> <span
class="nf">testExecuteValidationFailsMissingFirstName</span><span class="p">(</span><span
class="o">)</span> <span class="kd">throws</span> <span class="n">Exception</span><span
class="o">()</span> <span class="o">{</span>
+  <span class="c1">//request.setParameter("personBean.firstName", "Bruce");</span>
+  <span class="n">request</span><span class="o">.</span><span
class="na">setParameter</span><span class="o">(</span><span class="s">"personBean.lastName"</span><span
class="o">,</span> <span class="s">"Phillips"</span><span class="o">);</span>
+  <span class="n">request</span><span class="o">.</span><span
class="na">setParameter</span><span class="o">(</span><span class="s">"personBean.email"</span><span
class="o">,</span> <span class="s">"bphillips@ku.edu"</span><span
class="o">);</span>
+  <span class="n">request</span><span class="o">.</span><span
class="na">setParameter</span><span class="o">(</span><span class="s">"personBean.age"</span><span
class="o">,</span> <span class="s">"19"</span><span class="o">);</span>
+
+  <span class="n">ActionProxy</span> <span class="n">actionProxy</span>
<span class="o">=</span> <span class="n">getActionProxy</span><span
class="o">(</span><span class="s">"/register.action"</span><span class="o">);</span>
+  <span class="n">Register</span> <span class="n">action</span> <span
class="o">=</span> <span class="o">(</span><span class="n">Register</span><span
class="o">)</span> <span class="n">actionProxy</span><span class="o">.</span><span
class="na">getAction</span><span class="o">()</span> <span class="o">;</span>
 
-  assertEquals("The execute method did not return " + ActionSupport.INPUT + " but should
have.", ActionSupport.INPUT, result);
-  
-}
+  <span class="n">assertNotNull</span><span class="o">(</span><span
class="s">"The action is null but should not be."</span><span class="o">,</span>
<span class="n">action</span><span class="o">);</span>
 
+  <span class="n">String</span> <span class="n">result</span> <span
class="o">=</span> <span class="n">actionProxy</span><span class="o">.</span><span
class="na">execute</span><span class="o">();</span>
 
+  <span class="n">assertEquals</span><span class="o">(</span><span
class="s">"The execute method did not return "</span> <span class="o">+</span>
<span class="n">ActionSupport</span><span class="o">.</span><span
class="na">INPUT</span> <span class="o">+</span> <span class="s">"
but should have."</span><span class="o">,</span> <span class="n">ActionSupport</span><span
class="o">.</span><span class="na">INPUT</span><span class="o">,</span>
<span class="n">result</span><span class="o">);</span>
+<span class="o">}</span>
 </code></pre>
 </div>
 
 <p>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.</p>
 
-<p>#####Summary#####</p>
+<p><strong>Summary</strong></p>
 
-<p>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 <a href="http://struts.apache.org/2.3.1.2/docs/testing-actions.html">Testing
Actions</a>^[http://struts.apache.org/2.3.1.2/docs/testing-actions.html] to learn more.</p>
+<p>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 <a href="//struts.apache.org/docs/testing-actions.html">Testing
Actions</a> to learn more.</p>
 
   </section>
 </article>



Mime
View raw message