Java Reference
In-Depth Information
use tools such as Selenium, HttpUnit, or HtmlUnit (see chapters 12 and 13). The
point is that in most cases using plain Selenium or HtmlUnit to test your JSF applica-
tion falls short in several ways. It's hard to validate expected HTML (which can be com-
plicated by the use of Ajax), and in case you succeed, your tests may fail on minor
HTML changes.
15.4.2
Mock objects to the rescue
Another approach you might take in order to test the MusicStore application is the
white box approach. In this case you test only the server-side classes using mocks, 3
without running the whole application in a container.
The managed beans are so simple that to test them you don't need any kind of
mocking. The problems appear when you want to involve objects like FacesContext ,
which you normally don't have access to. You can mock those objects, as well as
HttpServletRequest , HttpServletResponse , and some other objects, using the tech-
niques described in chapter 7.
For example, let's test the showAlbumDetails method of the AlbumDetailsBean
from listing 15.6 using the JM ock library. This method extracts a parameter from the
request, so we need to mock the request and pass different values for the parameter,
as shown in listing 15.9.
Listing 15.9
Testing the AlbumDetailsBean using JMock
[...]
public class TestAlbumDetailsBeanMock {
B
private Mockery context = new JUnit4Mockery();
C
private HttpServletRequest mockRequest;
@Before
public void setUp() {
mockRequest = context.mock( HttpServletRequest.class );
}
D
@Test
public void testShowAlbumDetailsRealAlbum() {
context.checking( new Expectations() {
{
oneOf( mockRequest ).getParameter( "albumName" );
will( returnValue( "Achtung Baby" ) );
}
} );
E
F
AlbumDetailsBean albumDetailsBean = new AlbumDetailsBean();
albumDetailsBean.setRequest( mockRequest );
G
H
String forwardString = albumDetailsBean.showAlbumDetails();
3
You can learn more about mocks in chapter 7.
 
 
 
 
Search WWH ::




Custom Search