小编nui*_*cca的帖子

使用真实实现的Mockito模拟对象

为什么要mockMap使用真正的实现?我该如何防止这种情况?

在方法testFirstKeyMatch中

when(mockMap.keySet().toArray()[0])...
Run Code Online (Sandbox Code Playgroud)

抛出ArrayIndexOutOfBoundsException:运行测试时为0.

MaxSizeHashMap是一个最大大小为7的LinkedHashMap,当我尝试添加更多内容时抛出一个IndexOutOfBoundsException.

配置文件记录对此不重要的内容.

SuperClass.java

public class SuperClass {

protected String[] days;
protected MaxSizeHashMap<String, String> map;

public SuperClass() {
    days = new String[7];
    map = new MaxSizeHashMap<String, String>();
    //...
}

void updateDays() {

    cal = Calendar.getInstance();

    for (int i = 0; i < 7; i = i + 1) {

        //adds short names "Mon", "Tue", ... to days
        days[i] = cal.getDisplayName(Calendar.DAY_OF_WEEK, 
                Calendar.SHORT, Locale.US);

        cal.add(Calendar.DATE, 1);
    }
}

void firstKeyMatch(Profile profile) {

    updateDays(); 

    //checks if …
Run Code Online (Sandbox Code Playgroud)

java junit mockito

5
推荐指数
1
解决办法
1035
查看次数

标签 统计

java ×1

junit ×1

mockito ×1