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

时间:2016-09-01 23:52:09

标签: java

我如何组合这个构造函数?

private Item[] items;

private int currentIndex;



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

1 个答案:

答案 0 :(得分:-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);
}