I test a service class like this.  MockDAO is just stub methods and I
override the methods (e.g., getMonthRecordsBefore) that I know the
service's method is supposed to invoke.  I don't test with the method
parameters in this case, because they are simply passed through from the
service method to the DAO method.

public void testGetBalanceBeforeDate() {
    LedgerService service = new LedgerService();
    service.setDao(new MockDAO() {
        @Override
        public List<MonthRecord> getMonthRecordsBefore(Employee emp,
                DayType dayType, Date beforeDate) {
            return new ArrayList<MonthRecord>() {
                {
                    add(new MonthRecord() {
                        {
                            setEarned(21.2);
                            setUsed(3);
                            setLost(10);
                        }
                    });
                    add(new MonthRecord() {
                        {
                            setEarned(5);
                            setUsed(0);
                            setLost(0);
                        }
                    });
                    add(new MonthRecord() {
                        {
                            setEarned(0.66);
                            setUsed(1);
                            setLost(1);
                        }
                    });
                }
            };
        }
    });

    assertEquals(21.2 - 3 - 10 + 5 + 0.66 - 1 - 1, service
            .getBalanceBeforeDate(null, null, null), 0.0001);
}


Murat Hazer wrote:
> Thanks,
> 
> I agree with you, before integration tests i need to write unit test,so
> i am looking for answer of "How can i do write correct unit tests for
> backing beans which use spring and hibernate?". I understand following
> points from your asnwers, if i am wrong please correct me;
> 1- to write correct unit tests i should isolate the uni
> t(class/function) which is under test from other layers/api's.
> 2- to test jsf backing bean shale-test framework is a good choice.
> 3- i should use jmock (or equivalent) to create mock objects of spring
> managed services and dao's then i should set these mocks to the backing
> bean in the test setUp() metod.
> 
> I am a little confused beacuse i couldn't find any good article,
> tutorial or sample code for a scanerio like this. I need to find
> something to get a clear understanding. Any suggestions?
> 
> Another question; can i use canoo web tests (or selenium) for
> integration tests? Is this a correct approach or should i follow a
> different way for integration testing?
> 
> best regards....
> 
> On 5/15/06, *Adam Brod* <[EMAIL PROTECTED]
> <mailto:[EMAIL PROTECTED]>> wrote:
> 
> 
>     Hi Murat-
> 
>     I agree with what Dave B. said - Unit Testing is very different from
>     Integration Testing.  I have found that it is best to start with
>     Unit Testing where you are testing an individual method or class (a
>     unit) in isolation from all other objects.  Therefore, you should
>     mock/stub out the other frameworks and classes that are used by the
>     backing bean you are testing.
> 
>     Once you have good unit testing coverage and know that your backing
>     bean does what you want it to, then you can build larger Integration
>     Tests that test the interactions between the backing bean and the
>     hibernate daos.  If you start with Integration Testing, it is much
>     more difficult to isolate bugs in your code because for any given
>     test, there is lots of code being executed.
> 
>     I hope this helps,
>     *
>     Adam Brod*/
>     Product Development Team/
> 
>     *"Murat Hazer" <[EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]>>*
> 
>     05/14/2006 08:11 AM
>     Please respond to
>     "MyFaces Discussion" <[email protected]
>     <mailto:[email protected]>>
> 
> 
>       
>     To
>       "MyFaces Discussion" <[email protected]
>     <mailto:[email protected]>>
>     cc
>       
>     Subject
>       JSF Backing Bean Unit Testing
> 
> 
> 
>       
> 
> 
> 
> 
> 
>     Hi,
> 
>     I am trying to write unit tests on my backing beans' action,
>     actionListener and other methods, and using shale-test framework to
>     accomplish this. From TDD principles i know every unit test should
>     test only itself not other functions or layers etc., but in my
>     scenario backing beans use spring managed services and these
>     services also use hibernate managed DAOs, and also these backing
>     beans use other request or session based beans. What are the best
>     practices about unit testing backing beans like this?
> 
>     Should i also use mock objects (like jmock or easymock) for these
>     properties of the backing beans? And what should i do to validate
>     faces-config files? I study appfuse (on backing bean tests exceutes
>     all layer functions..) and examples come with shale, they are not
>     very clear (or my misunderstaning), i think i need a clear and nice
>     sample (also other users) unit test on a such complicated backing bean)
> 
>     regards...
>     -- 
>     Murat HAZER
>     Elektrik-Elektronik Mühendisi - Electrical-Electronics Engineer
> 
>     Disclaimer: This electronic mail and any attachments are confidential and 
> may be privileged. If you are not the intended recipient, please notify the 
> sender immediately by replying to this email, and destroy all copies of this 
> email and any attachments. Thank you.
> 
> 
> 
> 
> 
> -- 
> Murat HAZER
> Elektrik-Elektronik Mühendisi - Electrical-Electronics Engineer
> Tel - Phone: +90 222 335 05 80 - 1395
> Cep Tel - Mobile Phone: +90 532 472 00 63
> Blog URL: http://www.projedunyasi.org
> Yahoo Group: http://groups.yahoo.com/group/malatyafenlisesi/


-- 
Dave Brondsema
Software Developer
Cornerstone University

Attachment: signature.asc
Description: OpenPGP digital signature

Reply via email to