As I need to test publish function parameter passed is correct using Mockk in Kotlin
Below is the code :
val notificationData = NotificationData(
notificationId = "test-notificationID",
operation = "CREATE",
partnerName = "test-partner",
created = Instant.now().toEpochMilli().toString(),
services = []
)
verify(exactly = 1) { publish(notificationData) }
But as the created attribute in notificationData object will be having real time value in mock as well as in called function, both are not matching and giving below error
Verification failed: call 1 of 1: publish(eq(NotificationData(notificationId=test-notificationId, partnerName=test-partner, operation=CREATE, mutatedAttributes=null, services=[], created=1633719398360)))). Only one matching call to Notification(object Notification)/publish(NotificationData) happened, but arguments are not matching: [0]: argument: NotificationData(notificationId=test-notificationId, partnerName=test-partner, operation=CREATE, mutatedAttributes=null, services=[], created=1633719398404), matcher: eq(NotificationData(notificationId=test-notificationId, partnerName=test-partner, operation=CREATE, mutatedAttributes=null, services=[], created=1633719398360)), result: -
Anyone please help me to find out, how can I ignore the "created" attribute to get test successful
As you were intending to test, it seems you can also mock the
created
as well. Therefore, instead of usingInstant.now().toEpochMilli().toString()
, you should put a time milis value to thosecreated
, e.g.EDIT
Seems there is another approach that may help, but quite takes effort, by injecting the
NotificationData
instead in your original class. So, you can mock theNotificationData
in your test