| Raible's Wiki
    Raible Designs AppFuseHomepage- Korean - Chinese - Italian - Japanese QuickStart Guide User Guide Tutorials Other ApplicationsStruts ResumeSecurity Example Struts Menu 
 
    
    
         
        Set your name in UserPreferences 
 
 Referenced by 
   JSPWiki v2.2.33 
    
 Hide Menu | 
 
      
         
      
      
         
             This is version 3.  
            It is not the current version, and thus it cannot be edited. Simplify you test using EasyMockThis articel will give a short overview how to integrate EasyMock in AppFuse. For further questions you can contact me at josip(at)esistegalaber(dot)de I assume that you have a running AppFuse project: 
 Download a suitable version of EasyMock [#1]
 Integrate the libraries in AppFuse [#2]
 
 
 So the complete service.test.classpath would look like 
    <path id="service.test.classpath">
        <path refid="service.compile.classpath"/>
        <pathelement location="${junit.jar}"/>
        <pathelement location="${log4j.jar}"/>
        <pathelement location="${log4j.jar}"/>
    	<fileset dir="${servletapi.dir}" includes="*.jar"/>
        <pathelement location="${jakarta-oro.jar}"/>
        <pathelement location="${dumbster.jar}"/>
        <pathelement location="${jmock.jar}"/>
        <pathelement location="${easymock.jar}"/>
        <path location="test/dao"/>
    </path>
 Now you are set up to write some tests with EasyMock. Write the tests. [#3]Basically I will rewrite the PeronManagerTest known from the Appfuse tutorial . Using EasyMock the Testcase will be set up like this: 
 
 The difference to Mocktests known from the tutorial is simply that we don't use the Mock themselves but an MockControl that will ease the use of the actual Mock. In this case I use the static Method MockControl.createStrictControl(PersonDao.class); that will create a MockControl object that will take care of ordering of methodcalls to the Mocked object. Let's take a look at the actual TestMethods: 
 
 I think this form of mocking your test is very simple to understand. The MockControl simply keeps track of the calls you make to the mocked instance. This way it becomes very easy to write down your expectations and the rather complicated setup of expectaions using bare Mocks is not needed anymore. Just like with bare Mocks you can set (void) return values or exceptions to be thrown. There are even convenience methods to specify that a method i supposed to be called n times. For more information on what EasyMock can do for you visit the EasyMock Documentation Run the tests [#4]All that is left to do is run the test. Either execute ant test-service or call the single testcase by executing ant test-service -Dtestcase=PersonManager. Hopefully I haven't misstyped anything and you will see a BUILD SUCCESSFULL Yeah Baby, Yeah! Have fun!Change all AppFuse (1.9) Service tests to easymock [#5]Download the RAR file and extract all tests to your test/service folder. Maybe you have to change the package names... Some further thougts. [#6]I consider Mocks to be a bit more than just a means of White Box Tests . As the term itself indicates you need to have some knowlegde of the tested code in order to write a meaningful test. In contrast to Black box Tests  where you don't care about the code itself, but only about the outcome of the test it will give you some information about the code you wrote. So, as a rule of thumb I belive that if you have difficulties writing a mock test for you business object that simply means your business object is designed badly. Therefore my paradigm concerning mock tests is simply: "If you can't test it - refactor it!". Comments appreciated! 
 | |||||||||