I would like to test a custom handler class which is starting a new Activity
. I would like to test the Intent
's Bundle
if contains the pre defined parameters.
The test class:
@MockK
lateinit var activity: ActivityCalendar
@Before
fun setUp() {
MockKAnnotations.init(this)
}
@Test
fun testActivityBundles() {
val book = mockk<Book>()
every { book.releaseDate } returns GregorianCalendar().apply { this.timeInMillis = 1423825586000 }
every { activity.startActivity(any()) } just Runs
val handler = ActivityHandler(activity)
handler.startRequiredActivity(book)
verify { activity.startActivity(
withArg { intent ->
val bundle = intent.extras!!
val releaseDateTimeMillis = bundle.getLong("release_date", 0L)
Assert.assertEquals(1423825586000, releaseDateTimeMillis)
}
) }
}
The code above is crashing at line: val bundle = intent.extras!!
but it shouldn't.
The class that I want to test:
class ActivityHandler(val activity: Activity) {
fun startRequiredActivity(book: Book) {
val intent = buildIntent(book)
activity.startActivity(intent)
}
private fun buildIntent(book: Book): Intent {
val extras = Bundle().apply {
this.putLong("release_Date", book.releaseDate.timeInMillis)
}
return Intent(activity, ActivityBookDetails::class.java).apply {
putExtras(extras)
}
}
}
data class Book(
val releaseDate: GregorianCalendar
)
I debugged the code and I found out that function private fun buildIntent(book: Book): Intent
is returning an "null" object (string "null" and not Java NULL).