https://kotlinlang.org logo
#kotlin-logging
Title
# kotlin-logging
c

christophsturm

06/14/2017, 12:19 PM
does kotlint-logging support mocking the logger?
o

oshai

06/14/2017, 5:37 PM
christophsturm: I think it is more a part of the underlying logging framework, but you can see an example here: https://github.com/MicroUtils/kotlin-logging/blob/master/src/test/kotlin/mu/LoggingTest.kt
Copy code
private lateinit var appenderWithWriter: AppenderWithWriter

    @Before
    fun setupAppender() {
        appenderWithWriter = AppenderWithWriter()
        Logger.getRootLogger().addAppender(appenderWithWriter.appender)
    }

    @After
    fun removeAppender() {
        Logger.getRootLogger().removeAppender(appenderWithWriter.appender)
    }
c

christophsturm

06/14/2017, 5:40 PM
ah interesting so i add an appender and check that.
usually i would just overwrite the java field of the logger with a mocked logger and verify the log method call. that also avoids log output in my tests. but i don’t know how to do that with kotlin-logging or how it should be done in such a context
o

oshai

06/14/2017, 5:42 PM
do you want to test the message or just not to see the logging?
c

christophsturm

06/14/2017, 5:46 PM
testing the message is more important, but both would be great.
o

oshai

06/14/2017, 5:50 PM
for testing the message the above is what I think of, in case you don't want to have the message in console/log I think using slf4j-nop https://mvnrepository.com/artifact/org.slf4j/slf4j-nop will work
not sure if both will work together though
and in my opinion it is better to have logging to console in tests
in some cases it helps
c

christophsturm

06/14/2017, 6:11 PM
thanks for your help!
418 Views