https://kotlinlang.org logo
#android
Title
# android
d

Daniel

05/23/2017, 9:19 PM
Might I ask how you guys use a logging framework in code that has to be unit tested? Standard Android Log. provokes the not mocked exception.
d

Daniel

05/23/2017, 9:25 PM
Just for clarification: Its a singleton and you can switch out the logging function if necessary? So before each test you would do that and in production code it defaults to the firebase implementation?
m

mg6maciej

05/23/2017, 9:26 PM
Yes, although this is more for functional tests.
d

Daniel

05/23/2017, 9:26 PM
That's very smart! Thank you for sharing that!
m

mg6maciej

05/23/2017, 9:27 PM
Works with everything really. Retrofit interfaces, GPS or even facebook login button (if you wrap your head around how to create a provider for that).
For unit tests I'd recommend natural dependency inversion tho.
I rarely log stuff, but for testing analytics events calls are made, it's good.
d

Daniel

05/23/2017, 9:30 PM
Its just amazing what you can do with functions in kotlin, storing a function in a property is just so science fiction if you come from java 6. And for logging: the same. Since I learned automated testing there is no need for it in my app - but the use case for logging mght arise somewhere. So its good to know (thank you for that!)
l

lekz112

05/24/2017, 2:03 PM
In my case, I use slf4j + timber for app, simplelogger for unittests. While you can inject logger, with slf4j you usually use static factory method:
Copy code
companion object {
        private val logger = LoggerFactory.getLogger(BaseActivity::class.java.simpleName)
    }
After that it can be used as usual:
Copy code
logger.debug("onCreate() called with: savedInstanceState = [{}]", savedInstanceState)