Hi, sorry to ask another time help after one week,...
# mockk
i
Hi, sorry to ask another time help after one week, at least I will write everything on the same thread. I have to test a function to ascertain that returns a type. I am using firebase initialising via an Object the function that calls FirebaseAnalytics
Copy code
object AnalyticsReporter {

    fun logEvent(message: String, bundle: Bundle = Bundle.EMPTY) {

        FirebaseAnalytics.getInstance(ProActiveApplication.applicationContext()).logEvent(message, bundle)
    }
the method i call is this one :
Copy code
when (loginState.value) {
       
            is EnterPassword -> {
                unknownPassword.onNext(AuthenticationCredentials(client, emailAddress))
                logEvent("user_forgot_password")
            }
}
when i hit logEvent I get a NPE, this is the class
Copy code
mockkObject(AnalyticsReporter)
every { AnalyticsReporter.logEvent(any(),any()) } returns Unit
        println(viewModel.helpAction())
and this is the error I get :
Copy code
java.lang.NullPointerException: Bundle.EMPTY must not be null

	at com.xxxxe.analytics.AnalyticsReporter.logEvent$default(AnalyticsReporter.kt:13)
	at com.xxx.viewmodel.LoginViewModel.helpAction(LoginViewModel.kt:201)
why I get this BundleEmpty passed as default parameter null? I cannot mock a Bundle.Empty as well right?
m
what’s the value of Bundle.EMPTY? is it an enum?
i
Bundle Empty is from Bundle.java and is null indeed :
Copy code
public static final Bundle EMPTY;
from Bundle.java that is needed to firebase to work properly
but i passed any() in
Copy code
mockkObject(AnalyticsReporter)
every { AnalyticsReporter.logEvent(any(),any()) } returns Unit
m
i think if you write the signature of your function as
fun logEvent(message: String, bundle: Bundle? = Bundle.EMPTY) {
(note the ?)
it should work
i
oh YESSSSSSS
thank you very much Mattia, and sorry I see why now
m
np 🙂
m
another happy ending but… I could have never imagined in my nightmares that
Bundle.EMPTY
would be a null constant…wtf Firebase?! 😳
m
well that’s perfectly valid in java unforunately 😞
m
yeah I know but… what the heck? It just violates conventions 😆
i
Yes Matteo
As the Swiss tv
All is good that ends good
m
hahaha 🔝