Java 对 assertEqual NullPointerException 的 JUnit 测试
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22623649/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JUnit testing for assertEqual NullPointerException
提问by Liondancer
I am not sure why the test case doesn't have an output of true
. Both cases should give a NullPointerException
.
我不确定为什么测试用例没有true
. 这两种情况都应该给出一个NullPointerException
.
I've tried doing this (Not exactly the same but it gives and output of true
) :
我试过这样做(不完全相同,但它给出和输出true
):
String nullStr = null;
//@Test
public int NullOutput1() {
nullStr.indexOf(3);
return 0;
}
//@Test(expected=NullPointerException.class)
public int NullOutput2() {
nullStr.indexOf(2);
return 0;
}
@Test(expected=NullPointerException.class)
public void testboth() {
assertEquals(NullOutput1(), NullOutput2());
}
Runner:
赛跑者:
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
public class TestRunnerStringMethods {
public static void main(String[] args) {
Result result = JUnitCore.runClasses(TestJunitMyIndexOf.class);
for (Failure failure : result.getFailures()) {
System.out.println(failure.toString());
}
System.out.println(result.wasSuccessful());
}
}
Method:
方法:
public static int myIndexOf(char[] str, int ch, int index) {
if (str == null) {
throw new NullPointerException();
}
// increase efficiency
if (str.length <= index || index < 0) {
return -1;
}
for (int i = index; i < str.length; i++) {
if (index == str[i]) {
return i;
}
}
// if not found
return -1;
}
Test Case:
测试用例:
@Test(expected=NullPointerException.class)
public void testNullInput() {
assertEquals(nullString.indexOf(3), StringMethods.myIndexOf(null, 'd',3));
}
采纳答案by David Koelle
I believe you want to use fail
here:
我相信你想在fail
这里使用:
@Test(expected=NullPointerException.class)
public void testNullInput() {
fail(nullString.indexOf(3));
}
Make sure to add import static org.junit.Assert.fail;
if you need to.
import static org.junit.Assert.fail;
如果需要,请务必添加。
回答by prime
In Java 8 and JUnit 5 (Jupiter) we can assert for exceptions as follows.
Using org.junit.jupiter.api.Assertions.assertThrows
在 Java 8 和 JUnit 5 (Jupiter) 中,我们可以如下断言异常。使用org.junit.jupiter.api.Assertions.assertThrows
public static < T extends Throwable > T assertThrows(Class< T > expectedType, Executable executable)
Asserts that execution of the supplied executable throws an exception of the expectedType and returns the exception.
If no exception is thrown, or if an exception of a different type is thrown, this method will fail.
If you do not want to perform additional checks on the exception instance, simply ignore the return value.
public static < T extends Throwable > T assertThrows(Class< T > expectedType, Executable executable)
断言所提供的可执行文件的执行会引发预期类型的异常并返回异常。
如果没有抛出异常,或者抛出了不同类型的异常,则此方法将失败。
如果您不想对异常实例执行额外检查,只需忽略返回值即可。
@Test
public void itShouldThrowNullPointerExceptionWhenBlahBlah() {
assertThrows(NullPointerException.class,
()->{
//do whatever you want to do here
//ex : objectName.thisMethodShoulThrowNullPointerExceptionForNullParameter(null);
});
}
That approach will use the Functional Interface Executable
in org.junit.jupiter.api
.
这一方法将使用功能接口Executable
在org.junit.jupiter.api
。
Refer :
参考 :
- http://junit.org/junit5/docs/current/user-guide/#writing-tests-assertions
- http://junit.org/junit5/docs/5.0.0-M2/api/org/junit/jupiter/api/Executable.html
- http://junit.org/junit5/docs/5.0.0-M4/api/org/junit/jupiter/api/Assertions.html#assertThrows-java.lang.Class-org.junit.jupiter.api.function.Executable-