有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何Junit测试默认构造函数?

我如何单位这个构造函数

private Item[] items;

private int currentIndex;



/**
 * Default Constructor
 */
public Inventory() 
{
    this.items = new Item[1];
    this.currentIndex = 0;
}

共 (1) 个答案

  1. # 1 楼答案

    将非公共测试成员和字段包私有化是标准做法。像这样,

    Item[] items;
    int currentIndex;
    
    public Inventory() 
    {
        this.items = new Item[1];
        this.currentIndex = 0;
    }
    

    现在只要单元测试与Inventory类在同一个包中

    public void testCtor() {
     Inventory i = new Inventory();
     assertNotNull(i.items);
     assertEqual(1, i.items.length);
     assertEqual(0, i.currentIndex);
    }