开发者

Seam unit testing with exception filter and expectedExceptions annotation

开发者 https://www.devze.com 2022-12-20 23:33 出处:网络
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

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 fa开发者_StackOverflow中文版il. 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();
}


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..


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

0

精彩评论

暂无评论...
验证码 换一张
取 消