首页 > 解决方案 > 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.
*/

标签: javaunit-testingjunitnullpointerexception

解决方案


您的countNumbers()方法返回 an int,这意味着始终返回结果。所以当你运行时:

assertNull(mycustomstring.countNumbers());

int值被自动装箱为 anInteger并且是 never null。此外,countNumbers()从不抛出 a NullPointerException,因此永远不会满足将抛出 a 的期望。

这些 JUnit 检查中的任何一个都会导致您的测试每次都失败。


推荐阅读