You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@myfaces.apache.org by Murat Hazer <mu...@gmail.com> on 2006/05/14 14:11:55 UTC

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

Re: JSF Backing Bean Unit Testing

Posted by Murat Hazer <mu...@gmail.com>.
Thanks Craig,

I understand that, i should prepare the backing beans which are under test
by hand in the setUp() method, i should not try to validate or read anything
from faces-config.xml, is this correct?

Ok then, what about services and DAOs used by these services? For example I
am using jmock to test spring managed services (to isolate from the data
layer), should i use same principle here? (mock service and dao objects)

I have a backing bean definition like this;

<managed-bean>
        <description>Grades List</description>
        <managed-bean-name>gradeList</managed-bean-name>
        <managed-bean-class>org.mhazer.GradeList</managed-bean-class>
        <managed-bean-scope>request</managed-bean-scope>
        <managed-property>
            <property-name>serviceLocator</property-name>
            <value>#{serviceLocator}</value>
        </managed-property>
        <managed-property>
            <property-name>visit</property-name>
            <value>#{sessionScope.visit}</value>
        </managed-property>
        <managed-property>
            <property-name>chosenLessonCode</property-name>
            <value>#{sessionScope.visit.chosenLessonCode}</value>
        </managed-property>
    </managed-bean>

serviceLocator prepares spring managed singleton services which are used to
access DB layer, and visit object is used to store session based values. How
can i write a unit test for a method of this backing using shale-test
framework?

best regards....

On 5/14/06, Craig McClanahan <cr...@apache.org> wrote:
>
>
>
> On 5/14/06, Murat Hazer <mu...@gmail.com> wrote:
> >
> > 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?
> >
>
> For the third scenario (need access to other request and session scope
> beans) you can populate the attributes on the MockHttpServletRequest and
> MockHttpSession instances.  I do this in the setUp() method of my unit
> tests.
>
>
>  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
> >
>
> Craig
>
>



-- 
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/

Re: JSF Backing Bean Unit Testing

Posted by Craig McClanahan <cr...@apache.org>.
On 5/14/06, Murat Hazer <mu...@gmail.com> wrote:
>
> 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?
>

For the third scenario (need access to other request and session scope
beans) you can populate the attributes on the MockHttpServletRequest and
MockHttpSession instances.  I do this in the setUp() method of my unit
tests.


 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
>

Craig

Re: JSF Backing Bean Unit Testing

Posted by Dave Brondsema <da...@brondsema.net>.
Murat Hazer wrote:
> 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)
> 

If you want to do unit tests, use mock objects so that you test only the
backing bean methods.  If you want to do
functional/integration/acceptance tests, your test needs to set up
spring and container resources so that the bean executes through all the
tiers.  Depends on which you want to do.

-- 
Dave Brondsema
Software Developer
Cornerstone University


Re: JSF Backing Bean Unit Testing

Posted by Dave Brondsema <da...@brondsema.net>.
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* <ABrod@intralinks.com
> <ma...@intralinks.com>> 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" <murathazer@gmail.com <ma...@gmail.com>>*
> 
>     05/14/2006 08:11 AM
>     Please respond to
>     "MyFaces Discussion" <users@myfaces.apache.org
>     <ma...@myfaces.apache.org>>
> 
> 
>     	
>     To
>     	"MyFaces Discussion" <users@myfaces.apache.org
>     <ma...@myfaces.apache.org>>
>     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


Re: JSF Backing Bean Unit Testing

Posted by Adam Brod <AB...@intralinks.com>.
Hi Murat-

Your understanding appears to be correct.  If you want more details on the 
differences between unit testing and integration testing, google for some 
of the testing websites such as junit.org, testing.com, etc.

Since JSF managed beans are regular POJOs, unit testing a managed bean is 
no different from unit testing any other plain java object.

Adam Brod
Product Development Team


"Murat Hazer" <mu...@gmail.com> 
05/15/2006 11:15 AM
Please respond to
"MyFaces Discussion" <us...@myfaces.apache.org>


To
"MyFaces Discussion" <us...@myfaces.apache.org>
cc

Subject
Re: JSF Backing Bean Unit Testing






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 <AB...@intralinks.com> 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" <mu...@gmail.com> 
05/14/2006 08:11 AM 

Please respond to
"MyFaces Discussion" <us...@myfaces.apache.org>



To
"MyFaces Discussion" <us...@myfaces.apache.org> 
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/ 
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.


Re: JSF Backing Bean Unit Testing

Posted by Murat Hazer <mu...@gmail.com>.
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 <AB...@intralinks.com> 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" <mu...@gmail.com>*
>
> 05/14/2006 08:11 AM  Please respond to
> "MyFaces Discussion" <us...@myfaces.apache.org>
>
>   To
> "MyFaces Discussion" <us...@myfaces.apache.org>  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/

Re: JSF Backing Bean Unit Testing

Posted by Adam Brod <AB...@intralinks.com>.
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" <mu...@gmail.com> 
05/14/2006 08:11 AM
Please respond to
"MyFaces Discussion" <us...@myfaces.apache.org>


To
"MyFaces Discussion" <us...@myfaces.apache.org>
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.