0

If I want to test a method that uses some 3rd party library, and I want to mock that library, the documentation seems to suggest that I need to pass in a mocked instance of a library class.

But what if the class imports its own dependencies, like this?

class SomeClass {
    import SomeLibrary

    fun MethodIWantToTest(val input) {
        val newinput = DoSomething(input)
        val instance = SomeLibrary()
        val result = instance.processData(newinput)
        return result
    }
}

class SomeTest {
    @Test
    fun TestSomething(){
        val sc = SomeClass
        AssertEquals (true, sc.MethodIWantToTest(false))
    }
}

Can I still mock SomeLibrary, and how would I do that?

TLP
  • 1,262
  • 1
  • 8
  • 20
  • Is SomeLibrary.processData a static method call? – fweigl Jun 06 '20 at 12:16
  • Code edited to make it an instance method call, but I would also like to mock static methods – TLP Jun 06 '20 at 12:21
  • The code is not valid, are you new to Kotlin? – Animesh Sahu Jun 06 '20 at 12:44
  • 1
    If I understood you correctly, I believe that this design is bad and not testable. Class initializing its dependencies makes the testing hard, where you introduce reflection and other stuff to have reasonable coverage. If that class initializes the dependency itself, then it is a private thing, so it should not be mocked, just like the reason why private methods are not tested. – Mansur Jun 06 '20 at 12:47
  • Understood, thanks – TLP Jun 06 '20 at 12:54

0 Answers0