你能不能告诉我,通常的做法是编写一个抛出异常的JUnit Test方法,例如
class A {
public String f(int param) throws Exception {
if (param == 100500)
throw new Exception();
return "";
}
}
private A object = new A();
@Test
public void testSomething() throws Exception {
String expected = "";
assertEquals(object.f(5), expected);
}
实际上,方法f()
不会为该参数抛出异常(5)但是我必须声明该异常。
答案 0 :(得分:6)
是的,它完全正常,如果它确实抛出异常,则测试将被视为失败。
您需要指定该方法抛出Exception
,即使您知道具体情况没有(此检查由编译器完成)。
在这种情况下,您期望的是object.f(5)
返回一个空字符串。任何其他结果(非空字符串或抛出异常)都会导致测试用例失败。
答案 1 :(得分:2)
如果您正在调用的方法抛出一个已检查的异常,则您需要尝试捕获或重新抛出。从测试本身做到这一点很好。有多种方法可以使用JUnit测试Exception。我试图在下面提供一个简短的摘要:
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
/**
* Example uses Kent Beck - Test Driven Development style test naming
* conventions
*/
public class StackOverflowExample {
@Rule
public ExpectedException expectedException = ExpectedException.none();
@Test
// Note the checked exception makes us re-throw or try / catch (we're
// re-throwing in this case)
public void calling_a_method_which_throws_a_checked_exception_which_wont_be_thrown() throws Exception {
throwCheckedException(false);
}
/*
* Put the class of the specific Exception you're looking to trigger in the
* annotation below. Note the test would fail if it weren't for the expected
* annotation.
*/
@Test(expected = Exception.class)
public void calling_a_method_which_throws_a_checked_exception_which_will_be_thrown_and_asserting_the_type()
throws Exception {
throwCheckedException(true);
}
/*
* Using ExpectedException we can also test for the message. This is my
* preferred method.
*/
@Test
public void calling_a_method_which_throws_a_checked_exception_which_will_be_thrown_and_asserting_the_type_and_message()
throws Exception {
expectedException.expect(Exception.class);
expectedException.expectMessage("Stack overflow example: checkedExceptionThrower");
throwCheckedException(true);
}
// Note we don't need to rethrow, or try / catch as the Exception is
// unchecked.
@Test
public void calling_a_method_which_throws_an_unchecked_exception() {
expectedException.expect(Exception.class);
expectedException.expectMessage("Stack overflow example: uncheckedExceptionThrower");
throwUncheckedException();
}
private void throwCheckedException(boolean willThrow) throws Exception {
// Exception is a checked Exception
if (willThrow) {
throw new Exception("Stack overflow example: checkedExceptionThrower");
}
}
private void throwUncheckedException() throws NullPointerException {
// NullPointerException is an unchecked Exception
throw new NullPointerException("Stack overflow example: uncheckedExceptionThrower");
}
}
答案 2 :(得分:2)
JUnit-Test旨在测试给定方法的正确行为。测试方法抛出错误(例如,错误的参数)是一个完全有效的方案。如果它是一个经过检查的异常,您必须将其添加到测试方法声明中或在方法中捕获它并将Assert设置为false(如果不应该发生异常)。
您可以使用expected
注释中的@Test
字段告诉JUnit,如果发生异常,此测试应该通过。
@Test(expected = Exception.class)
public void testSomething() throws Exception {
String expected = "";
assertEquals(object.f(5), expected);
}
在这种情况下,测试方法应抛出异常,因此测试将通过。如果从注释中删除expected = Exception.class
,则在发生异常时测试将失败。
答案 3 :(得分:-2)
您可以使用以下方法测试启动异常:
@Test(expected = ValidationException.class)
public void testGreaterEqual() throws ValidationException {
Validate.greaterEqual(new Float(-5), 0f, "error7");
}