在Java中如何使用JUnit验证抛出的异常?

时间:2009-04-24 12:19:47

标签: java exception exception-handling junit

在为Java API编写单元测试时,可能会出现需要对异常执行更详细验证的情况。即比JUnit提供的 @test 注释提供的更多。

例如,考虑一个应该从其他接口捕获异常的类,包装该异常并抛出包装的异常。您可能需要验证:

  1. 抛出包装异常的确切方法调用。
  2. 包装器异常将原始异常作为其原因。
  3. 包装器异常的消息。
  4. 这里的要点是你想在单元测试中对异常进行额外的验证(而不是关于你是否应该验证异常消息之类的事情的辩论)。

    对此有什么好处?

10 个答案:

答案 0 :(得分:24)

正如your answer所述,这是一个很好的方法。除此之外:

您可以将函数expectException包装到一个名为ExpectedException的新注释中 带注释的方法如下所示:

@Test
@ExpectedException(class=WrapperException.class, message="Exception Message", causeException)
public void testAnExceptionWrappingFunction() {
  //whatever you test
}

这种方式更具可读性,但它的方法完全相同。

另一个原因是:我喜欢Annotations:)

答案 1 :(得分:21)

在JUnit 4中,可以使用ExpectedException规则轻松完成。

以下是javadocs的示例:

// These tests all pass.
public static class HasExpectedException {
    @Rule
    public ExpectedException thrown = ExpectedException.none();

    @Test
    public void throwsNothing() {
        // no exception expected, none thrown: passes.
    }

    @Test
    public void throwsNullPointerException() {
        thrown.expect(NullPointerException.class);
        throw new NullPointerException();
    }

    @Test
    public void throwsNullPointerExceptionWithMessage() {
        thrown.expect(NullPointerException.class);
        thrown.expectMessage("happened?");
        thrown.expectMessage(startsWith("What"));
        throw new NullPointerException("What happened?");
    }
}

答案 2 :(得分:18)

看看提出的答案,你真的可以感受到Java中没有闭包的痛苦。恕我直言,最可读的解决方案是你好老尝试赶上。

@Test
public void test() {
    ...
    ...
    try {
        ...
        fail("No exception caught :(");
    }
    catch (RuntimeException ex) {
        assertEquals(Whatever.class, ex.getCause().getClass());
        assertEquals("Message", ex.getMessage());
    }
}

答案 3 :(得分:11)

对于JUNIT 3.x

public void test(){
   boolean thrown = false;
   try{
      mightThrowEx();
   } catch ( Surprise expected ){
      thrown = true;
      assertEquals( "message", expected.getMessage());
   }
  assertTrue(thrown );
}

答案 4 :(得分:5)

在这篇文章之前,我通过这样做完了我的异常验证:

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

我花了一些时间考虑这个问题,并想出了以下内容(Java5,JUnit 3.x):

// Functor interface for exception assertion.
public interface AssertionContainer<T extends Throwable> {
    void invoke() throws T;
    void validate(T throwable);
    Class<T> getType();
}

// Actual assertion method.
public <T extends Throwable> void assertThrowsException(AssertionContainer<T> 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<ServletException> functor = new AssertionContainer<ServletException>() {
    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<ServletException> getType() {
        return ServletException.class;
    }
}

// And this is how it's used.
assertThrowsException(functor);

看着这两个我无法决定哪一个我更喜欢。我想这是其中一个问题,其中实现目标(在我的情况下,使用functor参数的断言方法)从长远来看是不值得的,因为这样做6+代码来断言尝试要容易得多..catch块。

然后,也许我在星期五晚上解决问题的10分钟结果并不是最聪明的方法。

答案 5 :(得分:4)

@akuhn:

即使没有闭包,我们也可以获得更易读的解决方案(使用catch-exception):

import static com.googlecode.catchexception.CatchException.*;

public void test() {
    ...
    ...
    catchException(nastyBoy).doNastyStuff();
    assertTrue(caughtException() instanceof WhateverException);
    assertEquals("Message", caughtException().getMessage());
}

答案 6 :(得分:2)

以下帮助方法(改编自this博客文章)可以解决问题:

/**
 * Run a test body expecting an exception of the
 * given class and with the given message.
 *
 * @param test              To be executed and is expected to throw the exception.
 * @param expectedException The type of the expected exception.
 * @param expectedMessage   If not null, should be the message of the expected exception.
 * @param expectedCause     If not null, should be the same as the cause of the received exception.
 */
public static void expectException(
        Runnable test,
        Class<? extends Throwable> expectedException,
        String expectedMessage,
        Throwable expectedCause) {
    try {
        test.run();
    }
    catch (Exception ex) {
        assertSame(expectedException, ex.getClass());
        if (expectedMessage != null) {
            assertEquals(expectedMessage, ex.getMessage());
        }

        if (expectedCause != null) {
            assertSame(expectedCause, ex.getCause());
        }

        return;
    }

    fail("Didn't find expected exception of type " + expectedException.getName());
}

然后,测试代码可以按如下方式调用它:

TestHelper.expectException(
        new Runnable() {
            public void run() {
                classInstanceBeingTested.methodThatThrows();
            }
        },
        WrapperException.class,
        "Exception Message",
        causeException
);

答案 7 :(得分:2)

我做了一件非常简单的事情

testBla(){
    try {
        someFailingMethod()
        fail(); //method provided by junit
    } catch(Exception e) {
          //do nothing
    }
}

答案 8 :(得分:0)

我做了一个类似于其他帖子的帮手:

public class ExpectExceptionsExecutor {

    private ExpectExceptionsExecutor() {
    }

    public static  void execute(ExpectExceptionsTemplate e) {
        Class<? extends Throwable> aClass = e.getExpectedException();

        try {
            Method method = ExpectExceptionsTemplate.class.getMethod("doInttemplate");
            method.invoke(e);
        } catch (NoSuchMethodException e1) {


            throw new RuntimeException();
        } catch (InvocationTargetException e1) {


            Throwable throwable = e1.getTargetException();
            if (!aClass.isAssignableFrom(throwable.getClass())) {
                //  assert false
                fail("Exception isn't the one expected");
            } else {
                assertTrue("Exception captured ", true);
                return;
            }
            ;


        } catch (IllegalAccessException e1) {
            throw new RuntimeException();
        }

        fail("No exception has been thrown");
    }


}

客户端应该实施的模板

public interface ExpectExceptionsTemplate<T extends Throwable> {


    /**
     * Specify the type of exception that doInttemplate is expected to throw
     * @return
     */
    Class<T> getExpectedException();


    /**
     * Execute risky code inside this method
     * TODO specify expected exception using an annotation
     */
    public void doInttemplate();

}

客户端代码将是这样的:

@Test
    public void myTest() throws Exception {
        ExpectExceptionsExecutor.execute(new ExpectExceptionsTemplate() {
            @Override
            public Class getExpectedException() {
                return IllegalArgumentException.class;
            }

            @Override
            public void doInttemplate() {
                riskyMethod.doSomething(null);
            }
        });
     }

它看起来非常冗长,但是如果你使用具有良好自动完成功能的IDE,你只需要编写异常类型和正在测试的实际代码。 (其余的将由IDE完成:D)

答案 9 :(得分:0)

对于 JUnit 5 ,它更容易:

    @Test
    void testAppleIsSweetAndRed() throws Exception {

        IllegalArgumentException ex = assertThrows(
                IllegalArgumentException.class,
                () -> testClass.appleIsSweetAndRed("orange", "red", "sweet"));

        assertEquals("this is the exception message", ex.getMessage());
        assertEquals(NullPointerException.class, ex.getCause().getClass());
    }

通过返回异常对象本身,assertThrows()允许您测试有关抛出异常的每个方面。