Android Robolectric: OutOfMemoryError - 创建 ArrayList 时超出了 GC 开销限制

Android Robolectric: OutOfMemoryError - GC overhead limit exceeded, when Creating ArrayList

我想为 returns 和 ArrayList 的方法创建一个测试。 ArrayList 类型是一个名为 DateItem 的自定义对象。但是当我尝试在我的测试代码(位于 test 文件夹中)中创建 ArrayList 时,测试失败并显示以下消息:

java.lang.OutOfMemoryError: GC overhead limit exceeded

Process finished with exit code 255

这是我的代码:

var expectedDateItems: ArrayList<DateItem> = ArrayList()
val currentDate = date1Start
while (currentDate.isBefore(date1End)) {
    val dateItem = DateItem(currentDate, ArrayList())
    expectedDateItems.add(dateItem)
    currentDate.plusDays(1)
}

我想知道如何在我的测试代码中创建这样的 ArrayList。我调查了 this answer 但它是针对整个应用程序的,而不仅仅是为了测试目的。如何为单元测试分配更多内存?

编辑:调试后,代码在此行时失败:val dateItem = DateItem(currentDate, ArrayList())

实际上你有无限的 WHILE 循环,因为 currentDate.plusDays(1) return copy of currentDate。更改为:

currentDate = currentDate.plusDays(1)