在JUnit中,有3种方法来测试预期的异常:
-
@Test
,可选的'expected'属性 - 尝试捕获并始终
fail()
-
@Rule
ExpectedException
PS已通过JUnit 4.12测试
1. @Test预期属性
如果只想测试异常类型,请使用此选项,请参见以下内容:
Exception1Test.java
package com.mkyong;
import org.junit.Test;
import java.util.ArrayList;
public class Exception1Test {
@Test(expected = ArithmeticException.class)
public void testDivisionWithException() {
int i = 1 / 0;
}
@Test(expected = IndexOutOfBoundsException.class)
public void testEmptyList() {
new ArrayList<>().get(0);
}
}
2.尝试捕获并始终失败()
这有点老派了,在JUnit 3中广泛使用。测试异常类型以及异常详细信息。 请参阅以下内容:
Exception2Test.java
package com.mkyong;
import org.junit.Test;
import java.util.ArrayList;
import static junit.framework.TestCase.fail;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
public class Exception2Test {
@Test
public void testDivisionWithException() {
try {
int i = 1 / 0;
fail(); //remember this line, else 'may' false positive
} catch (ArithmeticException e) {
assertThat(e.getMessage(), is("/ by zero"));
//assert others
}
}
@Test
public void testEmptyList() {
try {
new ArrayList<>().get(0);
fail();
} catch (IndexOutOfBoundsException e) {
assertThat(e.getMessage(), is("Index: 0, Size: 0"));
}
}
}
永远记住fail()!
如果您要测试的行没有引发任何异常,而您忘记放置fail()
,则测试将通过(假肯定)。
3. @Rule ExpectedException
这个ExpectedException
规则(自JUnit 4.7起)使您既可以测试异常类型,也可以测试异常详细信息,就像“ 2. Try-catch and always fail() ”方法一样,但是以一种更为优雅的方式进行:
Exception3Test.java
package com.mkyong;
import com.mkyong.examples.CustomerService;
import com.mkyong.examples.exception.NameNotFoundException;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.Matchers.hasProperty;
public class Exception3Test {
@Rule
public ExpectedException thrown = ExpectedException.none();
@Test
public void testDivisionWithException() {
thrown.expect(ArithmeticException.class);
thrown.expectMessage(containsString("/ by zero"));
int i = 1 / 0;
}
@Test
public void testNameNotFoundException() throws NameNotFoundException {
//test type
thrown.expect(NameNotFoundException.class);
//test message
thrown.expectMessage(is("Name is empty!"));
//test detail
thrown.expect(hasProperty("errCode")); //make sure getters n setters are defined.
thrown.expect(hasProperty("errCode", is(666)));
CustomerService cust = new CustomerService();
cust.findByName("");
}
}
NameNotFoundException.java
package com.mkyong.examples.exception;
public class NameNotFoundException extends Exception {
private int errCode;
public NameNotFoundException(int errCode, String message) {
super(message);
this.errCode = errCode;
}
public int getErrCode() {
return errCode;
}
public void setErrCode(int errCode) {
this.errCode = errCode;
}
}
CustomerService.java
package com.mkyong.examples;
import com.mkyong.examples.exception.NameNotFoundException;
public class CustomerService {
public Customer findByName(String name) throws NameNotFoundException {
if ("".equals(name)) {
throw new NameNotFoundException(666, "Name is empty!");
}
return new Customer(name);
}
}
参考文献
翻译自: https://mkyong.com/unittest/junit-4-tutorial-2-expected-exception-test/