brekke      02/01/09 19:49:41

  Modified:    xdocs    application-testing.xml
               xdocs/testing standalone.xml
  Added:       xdocs/testing usingmocks.xml
  Log:
  Added a very simple, short example of using mock objects to test an action
  event method.  Hopefully we can add a more involved sample test in the near
  future.  Any comments/ideas/corrections/additions are welcome.
  
  Added link to the references and updated the package on the
  sample code to match the package in the tdk 2.1 sample application.
  
  Revision  Changes    Path
  1.3       +4 -1      jakarta-turbine-tdk/xdocs/application-testing.xml
  
  Index: application-testing.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-turbine-tdk/xdocs/application-testing.xml,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- application-testing.xml   4 Jan 2002 05:24:15 -0000       1.2
  +++ application-testing.xml   10 Jan 2002 03:49:41 -0000      1.3
  @@ -70,11 +70,13 @@
   </LI>
   
   <LI>
  +<a href="testing/usingmocks.html">
   <b>Tip:</b> Use mock objects for RunData and Context
  +</a>
   </LI>
   
   <LI>
  -<b>Tip:</b> Simple anonymous inner classes to mock torque objects
  +<b>Tip:</b> Simple anonymous inner classes to mock objects
   </LI>
   
   </UL>
  @@ -93,6 +95,7 @@
   <section name="Reference">
   <UL>
   <li><a href="http://jakarta.apache.org/cactus";>Cactus</a></li>
  +<li><a href="http://tammofreese.de/easymock";>EasyMock</a></li>
   <li><a href="http://httpunit.sourceforge.net";>HttpUnit</a></li>
   <li><a href="http://www.junit.org";>JUnit</a></li>
   <li><a href="http://www.mockobjects.com";>Mock Objects</a></li>
  
  
  
  1.2       +2 -4      jakarta-turbine-tdk/xdocs/testing/standalone.xml
  
  Index: standalone.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-turbine-tdk/xdocs/testing/standalone.xml,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- standalone.xml    4 Jan 2002 05:24:15 -0000       1.1
  +++ standalone.xml    10 Jan 2002 03:49:41 -0000      1.2
  @@ -35,8 +35,7 @@
   </p>
   
   <source><![CDATA[
  -
  -package myapp.test;
  +package org.mycompany.newapp.test;
   
   import junit.framework.TestCase;
   import org.apache.turbine.util.TurbineConfig;
  @@ -92,8 +91,7 @@
   </p>
   
   <source><![CDATA[
  -
  -package myapp.test;
  +package org.mycompany.newapp.test;
   
   import junit.framework.TestCase;
   import junit.framework.TestSuite;
  
  
  
  1.1                  jakarta-turbine-tdk/xdocs/testing/usingmocks.xml
  
  Index: usingmocks.xml
  ===================================================================
  <?xml version="1.0"?>
  
  <document>
   <properties>
    <title>Use mock objects for RunData and Context</title>
    <author email="[EMAIL PROTECTED]">Jeffrey D. Brekke</author>
   </properties>
  
  <body>
  
  <section name="Use mock objects for RunData and Context">
  
  <p>
  Using mock objects, you can test Turbine action methods directly
  without having Turbine running.  Mock objects are simple implementations of an
  interface in the system to monitor operations on the objects.  In
  Turbine, VeloctyAction classes have methods with two objects passed, RunData and
  Context.  By passing mock objects to our action event method, we can
  call the method directly from within a test case and exercise our action
  code.  In this example, we'll test the simplest method, doPerform from the sample
  application in the TDK.  This is code directly from the SQL action class.
  </p>
  
  <source><![CDATA[
  public void doPerform(RunData data, Context context)
      throws Exception
  {
      data.setMessage("Can't find the button!");
  }
  ]]></source>
  
  <p>
  To test the doPerform method, we need to pass a mock RunData and
  Context object to the method.  We should be able to inspect the RunData
  object and verify that the setMessage() method was called and the
  correct text was passed.  The <a href="http://www.mockobjects.com";>Mock
  Objects</a> project provides a set of classes to aid in implementing
  mock objects for testing.  This example uses the <a
  href="http://tammofreese.de/easymock/index.html";>EasyMock</a> package.
  </p>
  
  <source><![CDATA[
  package org.mycompany.newapp.modules.actions;
  
  import junit.framework.TestCase;
  import junit.framework.TestSuite;
  import junit.framework.Test;
  
  import org.easymock.EasyMock;
  import org.easymock.MockControl;
  
  import org.apache.turbine.util.RunData;
  import org.apache.turbine.modules.actions.VelocityAction;
  
  public class SQLTest extends TestCase
  {
      public SQLTest(String name)
      {
          super(name);
      }
  
      public static Test suite()
      {
          return new TestSuite( SQLTest.class );
      }
  
      public void testDoPerform() throws Exception
      {
          /* Create a control and mock RunData object */
          MockControl rundataCtl = EasyMock.controlFor(RunData.class);
          RunData rundata = (RunData) rundataCtl.getMock();
  
          /* Set up our mock RunData */
          rundata.setMessage("Can't find the button!");
  
          /* Create our action to test */
          VelocityAction action = new SQL();
          assertNotNull(action);
  
          /* Activate the RunData object, call the method, verify results */
          rundataCtl.activate();
          action.doPerform(rundata, null);
          rundataCtl.verify();
      }
  }
  ]]></source>
  
  <p>
  In the setUp() method we configure the mock RunData object to expect
  one call to the setMessage() method with the correct text.  We then
  create the action, call doPerform() passing our mock RunData object and
  verify that contents of the mock after the action has executed.  If the
  setMessage() method was called with the incorrect parameter, or called
  more or less than one time, the test will fail.
  </p>
  
  <p>
  The mock object for RunData could have been implemented with the Mock
  Objects library and compiled with the test code.  Having the mock
  objects coded this way makes the test case simpler and the test will
  run slightly faster than using EasyMock.  The trade off is another
  source file to maintain and update when the Turbine interface changes. 
  Using EasyMock, the test cases are more complicated to setup with the
  control objects, but there is no additional source code to write other
  than the test.
  </p>
  
  </section>
  
  </body>
  </document>
  
  
  

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to