Showing posts with label JUnit. Show all posts
Showing posts with label JUnit. Show all posts

Monday, 2 January 2012

JSF: managed beans without JSF dependencies

A previous post discussed how to inject the FacesContext into managed beans using a broker. This post demonstrates how to build on that approach with greater levels of abstraction.

It is generally possible to remove direct JSF dependencies from managed bean code. You might want to do this to reduce coupling, improve cohesion or in the interests of writing testable code.

The code was written against Java 6, JSF 2 and JUnit 4 but the approach could be adapted to earlier versions of all of these.

Tuesday, 27 December 2011

JSF: mocking FacesContext for unit tests

Referencing the FacesContext directly in Java classes makes managed beans more difficult to unit test. This post discusses how to mock the context for testing outside the application container.

These examples use Mockito with JUnit. Familiarity with JSF and unit testing Java is assumed.

I've used the javax.faces.bean annotations but the techniques apply for other bean management mechanisms (e.g. using faces-config.xml or Spring).