views:

158

answers:

2

I've got a unit test for a Seam component that should fail if a value isn't provided in the context before the backing bean is created. I've been able to test this manually but would like to cover this scenario with a unit test.

I'm basically getting a org.jboss.seam.InstantiationException caused by a java.lang.IllegalArgumentException when Seam tries to create the backing bean. This is good and is what I'd expect to happen. The problem is that when I write the unit test, I can neither put a try/catch around the new FacesRequest(..) {}.run(); or use the expectedExceptions annotation. In both cases, the exception is not caught and causes the test to fail. I assume this is because of Seam's exception filter but I don't know enough about the filter to know what the correct pattern to test this..

My code with the annotation looks something like this:

// also tried IlligalArgumentException here
@Test( enabled = true, expectedExceptions = InstantiationException.class )
public void noDataTest() throws Exception
{
    login( USERNAME );

    // the stack trace says that the test fails on the next line.
    // this is expected.
    new FacesRequest( "/blah/blah/show.xhtml" ) {

        @Override
        protected void updateModelValues() {
        }

        @Override
        protected void invokeApplication()
        {
            // we should never get here
            // i'll put an failure here eventually
        }
    }.run();
}
A: 

looks to me that your test case is expecting a empty no-arg constructor in backing bean whic is probably missing

foo
@foo, this is actually a bean that requires a property be injected and uses that property in its init() method to do a lookup. Because that property doesn't exist, the lookup fails (the IllegalArgumentException) and the object can't be created (InstantiationException). The problem is that the exception that is thrown can't be caught by a try/catch or by the expectedExceptions annotation.
Chris Williams
+2  A: 

I found the answer. Hopefully this helps someone else who was banging their head against the wall..

I was looking for a specific Exception but Seam was catching that Exception, asserting that an error had occurred, and then throwing a java.lang.AssertionError (java.lang.Error, not java.lang.Exception). Catching the correct Throwable and using the correct type in the annotation now work..

Chris Williams
+1. Don't forget to mark this answer **accepted**. You're allowed to accept your own answers. Else the Community may poke this topic up and up at times because it's unaccepted.
BalusC
Will do. Have to wait 1 day...
Chris Williams
Good to know (+1)
Arthur Ronald F D Garcia