运行测试时出现间歇性故障。

java.lang.NullPointerException
    at org.robolectric.res.ResBundle$Value.compareTo(ResBundle.java:189)
    at org.robolectric.res.ResBundle$Value.compareTo(ResBundle.java:166)
    at java.util.ComparableTimSort.countRunAndMakeAscending(ComparableTimSort.java:320)
    at java.util.ComparableTimSort.sort(ComparableTimSort.java:188)

在下面的代码
@RunWith(RobolectricGradleTestRunner::class)
@Config(constants = BuildConfig::class, sdk = intArrayOf(21), packageName = "my.package")
class MyActivityTest {
    lateinit var myActivity : MyActivity

    @Before
    fun setup() {
        myActivity = MyActivity()
    }
}

特别上线
        myActivity = MyActivity()

最佳答案

显然解决方法是

@RunWith(RobolectricGradleTestRunner::class)
@Config(constants = BuildConfig::class, sdk = intArrayOf(21), packageName = "my.package")
class MyActivityTest {
    lateinit var myActivity : MyActivity

    @Before
    fun setup() {
        myActivity = Robolectric.buildActivity(MyActivity::class.java).get()
    }
}

10-07 19:47