3

I am using MockK for Unit-Tests and want to verify if some function was called with current time(milliseconds) parameter.

In main app the function call:

functionName(System.currentTimeMillis())

But in test:

verify(exactly = 1) { 
       functionName(System.currentTimeMillis())  }

doesn't work, because milliseconds will be always different

Mahdi-Malv
  • 16,677
  • 10
  • 70
  • 117
Misha Akopov
  • 12,241
  • 27
  • 68
  • 82
  • 1
    You can store the timestamp before calling the function, and then check if that time is **about** the same time with _some threshold_ added. – x80486 Dec 02 '19 at 15:50
  • Ok, that would not be precise :) Hope there should be matcher for that. Thanks – Misha Akopov Dec 02 '19 at 15:59
  • Then store the timestamp as any other input parameter, before calling the function, then check for the output result based on that input. You don't need to mock that out since you are expecting `functionName` to do something with `System.currentTimeMillis()`. – x80486 Dec 02 '19 at 16:12
  • Does this answer your question? [Mocking time in Java 8's java.time API](https://stackoverflow.com/questions/24491260/mocking-time-in-java-8s-java-time-api) – Diego Marin Santos Dec 02 '19 at 17:58

1 Answers1

1

your could use any() instead. which just verifies if function was called with any parameter of the argument type (in this case Long)

verify(exactly = 1) { 
   functionName(any())
}
alireza easazade
  • 3,324
  • 4
  • 27
  • 35
  • Any will not test case when there was passed other time, not current. My main problem is to test exact time is current or not – Misha Akopov Dec 02 '19 at 21:06