java - NullPointerException 未通过 JUnit 测试
问题描述
我无法让我的NullPointerException
测试用例通过。
import org.junit.After;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
public class MyCustomStringTest {
private MyCustomStringInterface mycustomstring;
@Before
public void setUp() {
mycustomstring = new MyCustomString();
}
@After
public void tearDown() {
mycustomstring = null;
}
// Test if a null point exception is thrown
@Test(expected = NullPointerException.class)
public void testCountNumbers2() {
mycustomstring.setString(null);
assertNull(mycustomstring.countNumbers());
}
}
即使我检查它不为空,这个测试仍然失败。mycustomstring
这是和的实现countNumbers()
。
public class MyCustomString implements MyCustomStringInterface {
private String string;
@Override
public String getString() {
return string;
}
@Override
public void setString(String string) {
this.string = string;
}
@Override
public int countNumbers() {
StringBuffer tmpString = new StringBuffer();
int count=0;
boolean inNumber=false;
//avoid null pointer exception!
if(string==null || string.isEmpty())
return 0;
for (int i = 0; i < string.length(); i++) {
char ch = string.charAt(i);
if (Character.isDigit(ch)) {
if (!inNumber) {
count++;
inNumber = true;
}
}
else {
if (inNumber) {
inNumber = false;
}
}
}
return count;
}
int countNumbers();
返回一个字符串,该字符串由原始字符串中的所有字符组成,每个字符段都n
颠倒了。如果padded
为真,则字符的最后一段将添加字符“X”,有足够的时间来制作:
* a full segment.
* <p>
* <p>
* Examples:
* - For n=2 and padded=true, the method would return the string with every pair of characters swapped in place, and
* if there were an odd number of characters, an X would be added to the last segment before it is reversed.
* - For n=3 and padded=false, the method would return the string with every segment of 3 characters reversed in place,
* and the final segment would be reversed even if less than 3 characters without any additional characters added.
* <p>
* Values n and padded are passed as parameters. The starting character is considered to be in Position 1.
*
* @param n Determines size of the string segments to be reversed
* @param padded Determines whether an incomplete final segment will be padded with 'X'.
* @return String with the segments in their original order and the contents of the segments reversed.
* @throws NullPointerException If the current string is null or uninitialized.
* @throws IllegalArgumentException If "n" less than or equal to zero, and the current string is not null.
*/
解决方案
您的countNumbers()
方法返回 an int
,这意味着始终返回结果。所以当你运行时:
assertNull(mycustomstring.countNumbers());
该int
值被自动装箱为 anInteger
并且是 never null
。此外,countNumbers()
从不抛出 a NullPointerException
,因此永远不会满足将抛出 a 的期望。
这些 JUnit 检查中的任何一个都会导致您的测试每次都失败。
推荐阅读
- dashboard - 在 python 中使用破折号的 Web 应用程序
- scala - zipwithindex 获取 Rdd Key 并获取新的 RDD
- video-streaming - 使用 GStreamer 的“观看”目录并即时解码新的媒体文件
- python - 如何使用 statsmodels ARIMA 预测下一个值?
- reactjs - React hooks linting:它总是有意义吗?
- python - Cloud DataFlowRunner 中的 python-mysql?
- r - 使 R 中的等高线图更高效
- generics - 为什么这个带有泛型的 Kotlin 代码会产生类型不匹配编译器错误?
- r - 矩阵每列最小行的返回索引
- java - 如何在 Android 中的本地文件(从互联网上下载)上使用 PDFRendered?