https://kotlinlang.org logo
#mockk
Title
# mockk
i

ivano

04/20/2021, 2:53 PM
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

Mattia Tommasone

04/20/2021, 2:57 PM
what’s the value of Bundle.EMPTY? is it an enum?
i

ivano

04/20/2021, 2:57 PM
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

Mattia Tommasone

04/20/2021, 2:59 PM
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

ivano

04/20/2021, 3:02 PM
oh YESSSSSSS
thank you very much Mattia, and sorry I see why now
m

Mattia Tommasone

04/20/2021, 3:03 PM
np 🙂
m

Matteo Mirk

04/20/2021, 3:05 PM
another happy ending but… I could have never imagined in my nightmares that
Bundle.EMPTY
would be a null constant…wtf Firebase?! 😳
m

Mattia Tommasone

04/20/2021, 3:05 PM
well that’s perfectly valid in java unforunately 😞
m

Matteo Mirk

04/20/2021, 3:06 PM
yeah I know but… what the heck? It just violates conventions 😆
i

ivano

04/20/2021, 3:07 PM
Yes Matteo
As the Swiss tv
All is good that ends good
m

Matteo Mirk

04/20/2021, 3:10 PM
hahaha 🔝
19 Views