in android kotlin class it has a lateinit var
member. The coverage test is failing on the function referring to it.
The simplified class here:
open class TestC constructor(context: Context) {
private lateinit var mModule: NotificationModule
protected fun setModule(module: NotificationModule) {
mModule = module
}
protected fun getModule(): NotificationModule {
return mModule
}
fun doSomething() {
if (mModule.pushToken == null) {
System.out.println(("+++ 111"))
} else {
System.out.println(("+++ 222"))
}
}
@JvmField
protected var mContext: Context
init {
mContext = context
}
}
class NotificationModule {
private var _token: String? = null
val pushToken: String?
get() = _token
protected open fun setPushToken(pushToken: String) {
_token = pushToken
}
}
the coverage test failed on getModule()
, and doSomething()
.
the decompiled code shows it will check null
on the non-null member, so that the coverage test reports the branch is missed :
@NotNull
protected final NotificationModule getModule() {
NotificationModule var10000 = this.mModule;
if (var10000 == null) { //<== never null
Intrinsics.throwUninitializedPropertyAccessException("mModule");
}
return var10000;
}
public final void doSomething() {
NotificationModule var10000 = this.mModule;
if (var10000 == null) { //<== never null
Intrinsics.throwUninitializedPropertyAccessException("mModule");
}
if (var10000.getPushToken() == null) {
System.out.println("+++ 111");
} else {
System.out.println("+++ 222");
}
}
How to test those hidden code branches?
Update:
It is the test missed a case that should test access the field before init-ed it.
closing this question.