FacesRequest gives false positives in unit tests
jervin Jul 16, 2009 10:53 PMI am new to Seam development, so forgive me if some of what I have to say betrays my newbie status. I was digging through some of the unit test code in a project at the company I am currently working at and I uncovered something rather unfortunate that the tests were running and, in reality should fail, but they were erroneously passed. The tests used the common pattern of the BaseSeamTest.FacesRequest class, overriding the methods as needed and placing the assertions in the renderResponse() method. In fact, not only is this pattern common in the codebase at my company, but is common when I was googling for examples on the web.
example:
@Test
public void testingSomething() {
new FacesRequest() {
public void renderResponse() {
assert( ... );
}
}.run();
}
The problem was that sometime before, functionality was added that forced a login before accessing the view being tested. This meant that the renderResponse() method was never called during the test, really invalidating it. In fact, it was not benign in this instance, the assertions should have failed.
So my question is, is there someway to work around this? I know I could add some boolean flag manually to check this:
@Test
public void testingSomething() {
final boolean[] assertionsInvoked = new boolean [] { false };
new FacesRequest() {
public void renderResponse() {
assertionsInvoked[ 0 ] = true;
assert( ... );
}
}.run();
assertTrue( assertionsInvoked[ 0 ] );
}
This is tedious and it is likely that developers (I should know since I am one) would forget to do this.
The second approach would be to try and enhance SeamTest and add a check so that the renderResponse method (almost all assertions seem to wind up there), is actually invoked during the test. The problem with this is that the SeamTest framework is not easily modified, since FacesRequest is an inner non-static class of BaseSeamTest. This prevented the use of a simple wrapper class and the user of Java utility libraries like CGLib and org.springframework.aop. So the solution I have been pursuing is the use of Aspects that will intercept calls to both run() and renderResponse() and ensure that they are invoked.
Still this leaves me with a sinking feeling, as a Software Dev I love the fact that I got the chance to try a bunch of new libraries and finally got a crack at Aspects, but I wonder if something simpler/better does not exist.
Any ideas?
Thanks,
James E. Ervin
example:
@Test
public void testingSomething() {
new FacesRequest() {
public void renderResponse() {
assert( ... );
}
}.run();
}
The problem was that sometime before, functionality was added that forced a login before accessing the view being tested. This meant that the renderResponse() method was never called during the test, really invalidating it. In fact, it was not benign in this instance, the assertions should have failed.
So my question is, is there someway to work around this? I know I could add some boolean flag manually to check this:
@Test
public void testingSomething() {
final boolean[] assertionsInvoked = new boolean [] { false };
new FacesRequest() {
public void renderResponse() {
assertionsInvoked[ 0 ] = true;
assert( ... );
}
}.run();
assertTrue( assertionsInvoked[ 0 ] );
}
This is tedious and it is likely that developers (I should know since I am one) would forget to do this.
The second approach would be to try and enhance SeamTest and add a check so that the renderResponse method (almost all assertions seem to wind up there), is actually invoked during the test. The problem with this is that the SeamTest framework is not easily modified, since FacesRequest is an inner non-static class of BaseSeamTest. This prevented the use of a simple wrapper class and the user of Java utility libraries like CGLib and org.springframework.aop. So the solution I have been pursuing is the use of Aspects that will intercept calls to both run() and renderResponse() and ensure that they are invoked.
Still this leaves me with a sinking feeling, as a Software Dev I love the fact that I got the chance to try a bunch of new libraries and finally got a crack at Aspects, but I wonder if something simpler/better does not exist.
Any ideas?
Thanks,
James E. Ervin