@org.junit.Test
public void testIterationAAA()
{
Iteration test = new Iteration("AAA");
int result = test.factorial("AAA");
assertEquals("exceptionMessage",result);
}
据说因为字符串的阶乘不能被计算出来我应该抛出异常但是如何使用Junit测试它?
答案 0 :(得分:0)
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.
}
}
答案 1 :(得分:0)
您应该使用expected
属性
@Test(expected=SomeException.class)