JUnit Eclipse检查是否引发异常?

JUnit Eclipse检查是否引发异常?,eclipse,junit,Eclipse,Junit,嘿,伙计们,我有一个关于阶乘的Junit测试代码 @org.junit.Test public void testIterationAAA() { Iteration test = new Iteration("AAA"); int result = test.factorial("AAA"); assertEquals("exceptionMessage",result); } 假设由于无法计算字符串的阶乘,应该抛出我所做的异常,但是如何使用Junit测试它呢 您

嘿,伙计们,我有一个关于阶乘的Junit测试代码

@org.junit.Test
public void testIterationAAA()
{
    Iteration test = new Iteration("AAA");
    int result = test.factorial("AAA");

    assertEquals("exceptionMessage",result);

}

假设由于无法计算字符串的阶乘,应该抛出我所做的异常,但是如何使用Junit测试它呢

您应该使用
预期的
属性

import org.junit.Assert;

...

@org.junit.Test
public void testIterationAAA()
{
    try {
         Iteration test = new Iteration("AAA");
         int result = test.factorial("AAA");
         // The above line is expected to throw an exception.
         // If the code does not throw an exception, fail the test.
         Assert.fail("An exception should have been thrown");
    } catch (Exception ex) {
         // Nothing to do.  Exception was expected in this test case.
    }

}
@Test(expected=SomeException.class)
可能重复的