In Java how can I validate a thrown exception with JUnit?

前端 未结 10 1207
甜味超标
甜味超标 2020-11-30 05:47

When writing unit tests for a Java API there may be circumstances where you want to perform more detailed validation of an exception. I.e. more than is offered by the @t

10条回答
  •  鱼传尺愫
    2020-11-30 06:15

    Until this post I've done my exception validation by doing this:

    try {
        myObject.doThings();
        fail("Should've thrown SomeException!");
    } catch (SomeException e) {
        assertEquals("something", e.getSomething());
    }
    

    I spent a few moments thinking about the issue though and came up with the following (Java5, JUnit 3.x):

    // Functor interface for exception assertion.
    public interface AssertionContainer {
        void invoke() throws T;
        void validate(T throwable);
        Class getType();
    }
    
    // Actual assertion method.
    public  void assertThrowsException(AssertionContainer functor) {
        try {
            functor.invoke();
            fail("Should've thrown "+functor.getType()+"!");
        } catch (Throwable exc) {
            assertSame("Thrown exception was of the wrong type! Expected "+functor.getClass()+", actual "+exc.getType(),
                       exc.getClass(), functor.getType());
            functor.validate((T) exc);
        }
    }
    
    // Example implementation for servlet I used to actually test this. It was an inner class, actually.
    AssertionContainer functor = new AssertionContainer() {
        public void invoke() throws ServletException {
            servlet.getRequiredParameter(request, "some_param");
        }
    
        public void validate(ServletException e) {
            assertEquals("Parameter \"some_param\" wasn't found!", e.getMessage());
        }
    
        public Class getType() {
            return ServletException.class;
        }
    }
    
    // And this is how it's used.
    assertThrowsException(functor);
    

    Looking at these two I can't decide which one I like more. I guess this is one of those issues where achieving a goal (in my case, the assertion method with functor parameter) isn't worth it in the long run since it's just a lot easier to do those 6+ of code to assert the try..catch block.

    Then again, maybe my 10 minute result of problem solving at friday evening just isn't the most intelligent way to do this.

提交回复
热议问题