2 回答
TA贡献1803条经验 获得超3个赞
您的@BeforeClass方法需要是静态的,它访问的变量也是如此。试试下面的代码,现在b1应该可以访问了testAddNew()。请注意,我已转移b1到班级。
import org.junit.Test;
import org.junit.BeforeClass;
import junit.framework.TestCase;
public class InventoryTest extends TestCase {
private static Inventory inv;
private static Book b1;
@BeforeClass
public static void setupTestObjs() throws Exception {
inv = new Inventory();
b1 = new Book();
CD c1 = new CD();
}
@Test
public void testAddNew() {
inv.addNew(b1);
inv.addNew(b1);
}
}
TA贡献1900条经验 获得超5个赞
的范围b1仅限于setupTestObjs()方法。因此无法在testAddNew().
(您没有发布编译器错误,所以我假设错误发生在testAddNew().)
为了使这项工作,即。b1为所有测试设置一次,您需要给它类范围,即。之后直接声明inv。
你的setupTestObjs()方法和测试夹具将需要static为@BeforeClass工作按预期。
添加回答
举报
