[jboss-user] [JBoss Seam] - Re: SeamTest and expectedExceptions

matt.drees do-not-reply at jboss.com
Wed Sep 26 16:03:00 EDT 2007


Ok, I felt like implementing this.  It works well enough for me, anyway.  Maybe you can do something similar:

  | import static org.jboss.seam.annotations.Install.MOCK;
  | 
  | import java.io.IOException;
  | 
  | import javax.servlet.FilterChain;
  | import javax.servlet.ServletException;
  | import javax.servlet.ServletRequest;
  | import javax.servlet.ServletResponse;
  | 
  | import org.jboss.seam.annotations.Install;
  | import org.jboss.seam.annotations.Name;
  | import org.jboss.seam.annotations.intercept.BypassInterceptors;
  | import org.jboss.seam.annotations.web.Filter;
  | 
  | /**
  |  * By default, Seam's exception filter is installed in integration tests.  Unfortunately,
  |  * if an exception occurs during Request.run(), the exception filter logs it and redirects,
  |  * but the exception is not thrown (which is correct for a prod environment, but not what 
  |  * we want for tests).  This filter overrides the default exception filter, and throws
  |  * any encountered exceptions.  ServletExceptions are unwrapped if the root exception
  |  * is a runtime exception.
  |  * @author Matthew.Drees
  |  *
  |  */
  | @Name("org.jboss.seam.web.exceptionFilter")
  | @Install(precedence = MOCK, classDependencies="javax.faces.context.FacesContext")
  | @BypassInterceptors
  | @Filter(within="org.jboss.seam.web.ajax4jsfFilter")
  | public class ExceptionFilter extends org.jboss.seam.web.ExceptionFilter {
  | 
  |    @Override
  |    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
  |       try {
  |          chain.doFilter(request, response);
  |       } catch (ServletException se) {
  |          //unwrap servletException if we can (we can't rethrow a checked non-servlet exception)
  |          Throwable throwable = se.getRootCause();
  |          if (throwable instanceof RuntimeException) {
  |             RuntimeException runtimeException = (RuntimeException) throwable;
  |             throw runtimeException;
  |          }
  |          throw se;
  |       }
  |    }
  | }
  | 

It works well if you don't care what kind of exception is thrown, or if you're looking for a runtime exception.  If you're looking for a checked exception, your testcase is somewhat verbose:

  | 
  | import javax.servlet.ServletException;
  | 
  | import org.testng.annotations.Test;
  | 
  | public class ExceptionFilterOverrideTest extends BaseIntegrationTest {
  | 
  |    @Test(expectedExceptions = IllegalStateException.class)
  |    public void testRuntimeException() throws Exception {
  |       new NonFacesRequest() {
  |          @Override
  |          protected void renderResponse() throws Exception {
  |             throw new IllegalStateException();
  |          }
  |       }.run();
  |    }
  |    
  |    @Test(expectedExceptions = Exception.class)
  |    public void testGeneralException() throws Exception {
  |       new NonFacesRequest() {
  |          @Override
  |          protected void renderResponse() throws Exception {
  |             throw new IllegalStateException();
  |          }
  |       }.run();
  |    }
  |    
  |    @Test(expectedExceptions = IllegalAccessException.class)
  |    public void testCheckedException() throws Exception {
  |       new NonFacesRequest() {
  |          @Override
  |          protected void renderResponse() throws Exception {
  |             throw new IllegalAccessException();
  |          }
  |          
  |          //this could be pulled into a superclass, if used frequently
  |          @Override
  |          public String run() throws Exception {
  |             try {
  |                return super.run();
  |             } catch (ServletException se) {
  |                handleServletException(se);
  |                return null; //nonreachable
  |             }
  |          }
  | 
  |       }.run();
  |    }
  |    
  | 
  |    private void handleServletException(ServletException se) throws Exception, ServletException {
  |       Throwable throwable = se.getRootCause();
  |       if (throwable instanceof Exception) {
  |          Exception exception = (Exception) throwable;
  |          throw exception;
  |       }
  |       throw se;
  |    }
  | }
  | 

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4089038#4089038

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4089038



More information about the jboss-user mailing list