java - 为什么我的 hashmap j 单元测试返回 'expecting null pointer exception' ?

标签 java unit-testing constructor hashmap logic

我应该使用 HashMap 制作直方图。我们要构建一些junit测试,但我的测试返回空。这与测试 ArrayList 中的空字符串不一样吗? 我的前两个测试也应该期待一个非法的参数,但即使这些测试也失败了.. 通常我们创建该类的一个新对象并测试它的方法,但是当我创建一个直方图对象时,我无法在逻辑上将它与我的 HashMap 关联起来或进行测试。我多次进行了 j 单元测试,但我在这里为 HashMap 绘制了一个空白,因为这是我第一次使用它。有什么建议吗?提前谢谢。

 public class Histogram{

 private HashMap<String, Integer> map;

/**
 * constructor for Histogram
 * 
 * @precondition map cannot contain null or empty string values
 * @postcondition sets map to new Hashmap
 * @param none
 * 
 */
public Histogram() {

   if ((this.map.containsValue("") || this.map.containsKey(null)) {
        throw new IllegalArgumentException(
                "there cannot be an empty string an String cannot be null");
    }
    this.map = new HashMap<String, Integer>();
}
/**
 * attempts to find the parameter in map.
 * 
 * @param add
 *            the string to find
 * 
 */
    public void add(String add) {
    for (int count = 1; count >= 1; count++) {
        if (this.map.containsKey(add)) {
            count++;
        }
        if (!this.map.containsKey(add)) {
            this.map.put(add, 1);
        }
    }
}

/**
 * finds the count associated with the parameter and returns it.
 * 
 * @param count
 *            the count/number relevant to the key in the hashmap
 * @return countb, the count converted into a integer
 */
public int getCount(String count) {
    this.map.get(count);
    int countb = Integer.parseInt(count);
    if (!this.map.containsKey(count)) {
        return 0;
    } else {
        return countb;
    }
}

/**
 * sorts the words in the list returned
 * 
 * @return keys a sorted list of keys in the hashmap maps
 */
public ArrayList<String> getSortedKeys() {
    ArrayList<String> keys = null;
    keys = new ArrayList<String>(this.map.keySet());
    Collections.sort(keys);
    return keys;

}


j uint testing class

public class WhenAddingToHistogram {

private HashMap<String, Integer> map;

/**
 * test for adding an empty string
 */
@Test
(expected = IllegalArgumentException.class)
public void testForAddingemptyString() {
    this.map = new HashMap<String, Integer>();
    //Histogram database   = new Histogram();

    this.map.put("kassy", 1);
    this.map.put("sid", 2);
    this.map.put("", 3);
    assertEquals(this.map.containsKey(""),true);
}


/**
 * test for adding an empty string
 */
@Test
(expected = IllegalArgumentException.class)
public void testForAddingNullString() {
    this.map = new HashMap<String, Integer>();
    //Histogram database   = new Histogram();

    this.map.put("kassy", 1);
    this.map.put("sid", 2);
    this.map.put(null, 3);
    assertEquals(this.map.get(null), true);
}

/**
 * test if hash map contains key
 */
@Test
// (expected = IllegalArgumentException.class)
public void testForcontainskey() {
    this.map = new HashMap<String, Integer>();
    this.map.put("kassy", 1);
    assertEquals(this.map.get("kassy"), 1 , 0);
}

/**
 * test is hash map does not contain key
 */
@Test
//(expected = IllegalArgumentException.class)
public void testFornotcontainskey() {
    this.map = new HashMap<String, Integer>();
    this.map.put("kassy", 1);
    this.map.put("joe",  2);
    assertEquals(this.map.containsKey("john"), false);
}

最佳答案

这个逻辑是错误的和/或不正确的:

if ((this.map.containsValue("") || this.map.containsKey(null)) {
    throw new IllegalArgumentException(
            "there cannot be an empty string an String cannot be null");
}

如果您想确保没有空键插入到您的 map 中,您应该在调用以将数据放入直方图中的方法上执行此操作。这使得完整性检查毫无意义,因为 Map 中不会有 null 或空字符串。

发生的情况是,您在实例化 map 实例之前运行上述逻辑。当您运行上述逻辑时,它在实例化时将为 null。如果您直接在字段中实例化它会更好:

map = new HashMap<>();

关于java - 为什么我的 hashmap j 单元测试返回 'expecting null pointer exception' ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24768076/

相关文章:

java - 有没有设计良好、维护良好的 Java RSS 解析库?

c# - 使用 AutoFixture 创建 EF 实体 stub

c# - 只运行一次的类是否应该包含静态构造函数?

java - 创建新变量与重用旧变量哪个更快?

java - 错误获取方法rest json

java - org.hibernate.exception.ConstraintViolationException 删除

asp.net-mvc - 使用 RedirectToAction 对 ASP.NET MVC 2 中的 Controller 进行单元测试

unit-testing - 如何参数化 TestFixtureSetUp (NUnit)

java - 我的 java 程序中有 2 个编译错误。请澄清

c++ - C++中的动态参数化构造函数问题