16

I'm trying to find a practical unit testing framework for JSF.

I know about JSFUnit, but this is very impractical to me. I need to include about 10 JARs to my project, and jump through many other hoops just to get it running.

I realize that -- due to the need to simulate a platform and a client -- unit testing web applications is difficult. But is there a better way?

Zack Marrapese
  • 12,072
  • 9
  • 51
  • 69
  • 1
    What frameworks are you using with JSF or are you using JSF raw? – Drew Jun 09 '09 at 19:59
  • I'm using richFaces for AJAX functionality, and facelets for my rendering. Also, I'm using JSF RI 1.2 for my implementation. For my app server, I'm using glassfish v2. – Zack Marrapese Jun 09 '09 at 21:13
  • 1
    Some responders mention Selenium. Note that Selenium isn't unit testing tool. It's for integration testing an assembled and deployed application. Unit test means testing individual parts of code raised out from environment. – pcjuzer Dec 10 '10 at 15:02
  • Any luck yet on a good **unit-testing** helper for JSF 1.2, such as JSF-Extensions or Apache Shale ? – abdelrahman-sinno Mar 07 '16 at 13:36

6 Answers6

9

Have you thought about doing integration testing with Selenium or another tool? Selenium allows you to record and run tests directly in the browser. You can also run tests in multiple browsers and on multiple platforms with Selenium Remote Control.

Writing unit tests is good, but it might provide more to create some functional integration tests rather than unit-testing the presentation layer code.

Paul Morie
  • 15,528
  • 9
  • 52
  • 57
3

On the project I'm working on at the moment we dabbled with using selenium. We actually spent a lot of time writing these selenium tests, but found that they added little value because the UI changes so much and you just end up doubling your effort for very little return on investment.

Another problem with selenium is that it requires your code to be deployed, which means it doesn't play well with unit test frameworks eg maven.

What I would say is that writing really good unit tests for your managed beans is invaluable.

Manrico Corazzi
  • 11,299
  • 10
  • 48
  • 62
mR_fr0g
  • 8,462
  • 7
  • 39
  • 54
2

Have you taken a look at the jsfunitwar Ant task or alternatively the Maven plugin provided by JSFUnit? Both greatly reduce the complexity of generating the .war file to be tested. I'm using JSFUnit on my current project and find the combination of white box and black box testing capabilities to be very powerful. Because JSFUnit uses HtmlUnit under the covers, you can very easily and effectively examine the generated HTML, or conversely, verify the state of your internal JSF backing beans. I was able to incorporate the JSFUnit tests into my Continuous Integration process and have been quite pleased with the outcome.


1

Selenium is superficial, jsfunit is inward. I recommend that use jsfunit if project is not simple. Because team member can change jsf managedbean names or etc, you can catch that with jsfunit.

bora.oren
  • 3,439
  • 3
  • 33
  • 31
1

HttpUnit can also be an alternative. It provides apis so you have a choice to automate the tests.

http://httpunit.sourceforge.net/index.html

rangalo
  • 5,448
  • 8
  • 46
  • 66
0

I'm with Paul on Selenium being very easy to setup and start working with. I use Selenium IDE in Firefox with some customization at that level, then you can export these to other platforms such as Java JUnit tests. It was quite easy to download and launch the selenium-server.jar, add the selenium-java-client-driver to my existing Eclipse Maven POM driver project; then launch the same exported JUnit test in Eclipse. I mainly wanted to use Java just for looping which the basic Selenium IDE didn't support.

I have configured JSF Unit for my project too which does require more time to configure... more importantly though with in-container tests like JSFUnit changes to the test require rebuilding the WAR, redeploying in the container and then executing from Eclipse or via a browser. So for quickly trying a small change this is time consuming. Of course with JSFUnit you have access to all the internals of the JSFSession etc so it depends what granularity of testing you need I guess.

I'd be interested if anybody knows a faster way to turnaround changes to a JSFUnit test and execute it. Definitely Selenium tests feel more like JUnit tests in that regard.

Wayne Earnshaw
  • 527
  • 6
  • 15