嘿伙计们,我有这个 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.
}
}
你应该
使用expected
属性
@Test(expected=SomeException.class)