-2

I am facing the same issue as asked in the below question. please help me out.

Mock a "global" property in Kotlin

I tried solution provided in above question but nothing is working. and I am asking the same question because I am not able to post any comment on the previous question.

I am trying to write test case for below class

class CustomLogger constructor(val ctx: Context, embEnabled: Boolean = false) : Logger {

private val loggers = arrayListOf<Logger>()
fun get() = loggers

init {
    if (embEnabled)
        loggers.add(Emb(ctx))
    if (BuildConfig.DEBUG)
        loggers.add(DebugLogger(ctx))
}

override fun logError(t: Throwable, msg: String?) {
    loggers.forEach { logger ->
        logger.logError(t, msg)
    }
 }
}
enter code here

Here I am trying to mock get() or init{}

Jitendra
  • 37
  • 2
  • 7

1 Answers1

0

that was on dam question but i got you note this can only be used in unittest as mockito static mock is not support on Android JVM

testImplementation "org.mockito:mockito-inline:4.8.1" you gonna need 

this so added 
   Update you need to call this i forgot to add it sorry in your test case before call the method
Mockito.mockStatic(Class.forName("com.udacity.project4.locationreminders.RemindersActivityKt"))

fun getMockForMethod(clazz: Class<*>, methodName: String, methodResponse: Any) {
    val method: Method = clazz.getMethod(methodName)
    Mockito.`when`(method.invoke(null)).thenReturn(methodResponse)
}

now i created the method to handle no argument methods you can modifiy it as you see fit just pass the class using it name

getMockForMethod(Class.forName("com.udacity.project4.locationreminders.RemindersActivityKt"),
    "doSomething","New Response")
    Assert.assertEquals("New Response", doSomething())

works like a charm Enjoy

i have updated the above code for anyone to use with static members in kotlin

your updates makes this easy to do now it is a class that you can mock entirly and easliy mock any methods

val loggerMock= Mockito.mock(Logger::class.java)
    Mockito.`when`(loggerMock.loggers).thenReturn(new array of loggers)
  • Thanks for the help. I tried above solution but I am getting null pointer exception on at Mockito.`when`(method.invoke(null)).thenReturn(methodResponse I have edited my question please have a look. – Jitendra Oct 21 '22 at 07:10
  • @Jitendra take alook at the updates – Ahmed Mostafa Oct 21 '22 at 12:45
  • here Logger is an interface which is implemented by both the class EMB and DebugLogger. I am not getting any option to mock loggerMock.loggers I tried mocking EMB and DebugLogger and then adding it to the list but as soon I am calling compositeLogger = CompositeLogger(context, true) loggers = compositeLogger.get() real instances are getting assigned to loggers hence cursor is going inside EMB class which I don't want – Jitendra Oct 26 '22 at 07:47
  • your case should not use a mock you should use FakeClass that implement Logger Instead of the real class and using Contructor Injection or and DI framework test it – Ahmed Mostafa Oct 27 '22 at 18:19