novuhq / novu-kotlin

Kotlin SDK for Novu - The open-source notification infrastructure for engineers. 🚀
MIT License
20 stars 6 forks source link

Unit Testing Triage: Issue while mocking Novu object #75

Open therealansh opened 4 months ago

therealansh commented 4 months ago

Hey folks, i am trying to write some UTs around my Novu service class. For that i am using mockito to mock the behavior of Novu. However, I am blocked because the APIs are marked as internal which makes is accessible only to its project scope. Any workaround i can do to mock Novu and do something like this:

@Mock
private lateinit var novu: Novu

@InjectMocks
private lateinit var notifService: NotifService

...

@Test
fun `test-success`() = runTest {
    whenever(novu.subscribers()).thenReturn(...)     <---------- this is where it throws NPE because subscriberApi isnt mocked and is internal
    notifService.getSubscribers()
}
therealansh commented 4 months ago

@unicodeveloper @Cliftonz @mayorJAY ^^

mayorJAY commented 4 months ago

All the publicly accessible functions in this SDK are extension functions, try using mockk or mockito-kotlin to mock extension functions. That will solve the problem. See https://stackoverflow.com/questions/44382540/mocking-extension-function-in-kotlin

mayorJAY commented 3 months ago

@therealansh Is this resolved?