Linked Questions
24 questions linked to/from Override Java System.currentTimeMillis for testing time sensitive code
2 votes
1 answer
231 views
Remove time & date dependency on os in java application [duplicate]
Have you ever encounter such a scenario: Need to test some date related functionality of your software, the software rely on OS to get current date, so we have to change the date of OS, but date &...
0 votes
0 answers
409 views
How to Unit Test a Time-related State Machine Using Mockito in Java [duplicate]
I have written a class called ErrorHandingFiniteStateMachine, which basically does something like: https://developers.google.com/safe-browsing/developers_guide_v3#RequestFrequencyHashes Basically it ...
0 votes
2 answers
96 views
How to set application time in Java? [duplicate]
In my Java application, I use several libraries which invoke System.currentTimeMillis() and Instant.now(). These functions provide the current timestamp. I would like to set the time of the ...
235 votes
3 answers
44k views
How to use ThreeTenABP in Android Project
I'm using Android Studio 2.1.2 and my Java setup is the following: >java -version > openjdk version "1.8.0_91" > OpenJDK Runtime Environment (build 1.8.0_91-8u91-b14-3ubuntu1~15.10....
59 votes
8 answers
159k views
How to mock new Date() in java using Mockito
I have a function that uses the current time to make some calculations. I'd like to mock it using mockito. An example of the class I'd like to test: public class ClassToTest { public long ...
11 votes
6 answers
19k views
Is it possible to freeze System.currentTimeMillis() for testing
For some testing purposes I would like to predict exactly what System.currentTimeMillis() will return. Is there any way in which I can freeze or manually set what will return when System....
17 votes
4 answers
9k views
java.lang.System.currentTimeMillis() replace method
Aside from recompiling rt.jar is there any way I can replace the currentTimeMillis() call with one of my own? 1# The right way to do it is use a Clock object and abstract time. I know it but we'...
11 votes
2 answers
17k views
How to Inject Clock.getInstance() in Spring Boot?
I want to unit test some code that calls System.currentTimeMillis(). As this answer points out, a good way to do this is to replace calls to System.currentTimeMillis() with Clock.getInstance()....
3 votes
3 answers
7k views
Unable to Mock System.currentTimeMillis()
I am writing unit tests using TestNG. The problem is when I mock System.currentTimeMillis, it returns the actual value instead of the mocked one. Ideally, it should return 0L , but it returns the ...
2 votes
2 answers
4k views
Robolectric: How can I set time that I want the Calendar class returns?
I have started to work with unit tests and still I do not know how to test somethings . My app has a method that receives 2 params, the opening and the closing time of a venue and returns true if the ...
-1 votes
1 answer
10k views
how to mock timestamp and date function in java?
how to mock following code? i dont want to change my code. Date date = new Date(); String res_timestamp=new Timestamp(date.getTime()).toString(); my code: SimpleDateFormat sdf = new ...
3 votes
2 answers
3k views
Do we have setCurrentMillisFixed in Java 8?
In Joda we have setCurrentMillisFixed method which can be used to set current system time: DateTimeUtils.setCurrentMillisSystem(); In Java 8 i am trying : ZonedDateTime.now(Clock.systemDefaultZone())...
5 votes
2 answers
2k views
Can AspectJ weave through sun.net.* packages?
I'm using AspectJ to intercept java.net.Socket calls. I've created very simple aspect after(): call(* java.net.Socket.connect(..)) { System.out.println("Connect intercepted!"); } and aop.xml &...
1 vote
3 answers
5k views
Fake system clock on certain date [duplicate]
I need to set the system clock on certain date in my tests. This way I must simulate a behavior happening that date under a test. I found this article http://www.javapractices.com/topic/TopicAction.do?...
2 votes
1 answer
2k views
Java Testing Calendar by changing current date
I have this fonction: /** * @return From november to december -> current year +1 else current year */ public static int getCurrentScolYear () { int month = Calendar.getInstance().get(...