3 Replies Latest reply on Sep 23, 2010 7:32 AM by Stan Silvert

    Testing backing beans

    Michael Auracher Newbie

      Hi all!


      We want to test our backing beans "directly", without calling it through a JSF page.

      Is that possible?


      I haven't found any tutorial about this case.


      Perhaps someone has an example.


      Reason: In the case of get away with JSF (not planned tough) I still want to test the mbean.



      Best regards,


        • 1. Re: Testing backing beans
          Stan Silvert Master

          You can always instantiate the beans directly and test them as you would in an ordinary JUnit test.


          If this is not sufficient, perhaps you could post some code showing what you would like to do?



          • 2. Re: Testing backing beans
            Michael Auracher Newbie

            Thans for your answer Stan.


            The problem I have:

            I use Maven2 to build my projects.

            The "user interface" project gets built as WAR.

            As I am using Cactus to test our application I created a parallel Maven project just for testing.


            -main pom |

            |               -ear

            |               - UI

            |               - EJB1......

            - Tests (using above EAR file to cactify + code)


            The EJBs I can access per lookup:

                      InitialContext ic =  new InitialContext();

                      CustomerocalHome application_home = (CustomerLocalHome)ic.lookup("java:/local/Customer");
                       customerEJB  = customer_home.create();



            Which works great.

            The problem with testing the UI is following:

            As the get built OUTSIDE the WAR file I'm not able to access the MBeans (even when I put a dependency into the Test project).

            Which leaves me two possibilities:

            1. Call the backing beans "remotely" like the EJBs (I haven't found any solution for this)

            2. Move the test files INTO the UI WAR, but I don't want the tests deployed when i build a release candidate (only for testing porposes)


            Currently I build the Test project for testing purposes.

            By building the application "itself" i create an EAR file without tests (that EAR file i deploy on our staging+live servers).


            For any help I would be grateful,


            • 3. Re: Testing backing beans
              Stan Silvert Master

              I see three options:

              1. Put ordinary JUnit tests in your UI project.  Since these are ordinary (non-cactus) tests, they won't be deployed with the WAR.
              2. Put your managed beans in a seperate module with its own jar and test them in the new module.
              3. Put your managed beans in a seperate module with its own jar and test them along with your JSFUnit tests.


              I'd probably choose #1, but I'd put more energy into your JSFUnit tests than the ordinary tests.  Ordinary tests are great when you first go to develop one or two complex beans that work together.  But artifacts like managed beans that are meant to run in a JSF environment need to be tested thoroughly in that environment for the tests to be meaningful.