How to ignore testing of one of the attribute in an argument passed in function using verify method of Mockk

1.2k Views Asked by At

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

1

There are 1 best solutions below

3
On

As you were intending to test, it seems you can also mock the created as well. Therefore, instead of using Instant.now().toEpochMilli().toString(), you should put a time milis value to those created, e.g.

val notificationData = NotificationData(
                notificationId = "test-notificationID",
                operation = "CREATE",
                partnerName = "test-partner",
                created = "1633719398404",
                services = []
            )

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 the NotificationData in your test