I am getting
java.lang.NullPointerException
at android.content.ComponentName.__constructor__(ComponentName.java:131)
because during unit test the class is not being found even though the package name is correct.
This is the method I am trying to test:
fun mainActivityIntentProvider(): Intent {
var myClass: Class<*>? = null
try {
myClass = Class.forName("packageName.MyMainActivity")
} catch (e: ClassNotFoundException) {
// Some logging statement stating class not found
}
return Intent(AppContextHolder.getAppContext(), myClass)
}
The unit test code that I have written (using MockK):
@Test
fun testMainActivityIntentProviderInHelper() {
context = mockk<Context>(relaxed = true)
every { AppContextHolder.getAppContext() } returns context
val expectedClassName = "packageName.MyMainActivity"
val result = myDemoClass.mainActivityIntentProvider()
val className = result.component?.className
assertEquals(expectedClassName, className)
}