Is it possible to freeze System.currentTimeMillis() for testing

前端 未结 6 2544
孤街浪徒
孤街浪徒 2021-02-20 02:24

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 w

相关标签:
6条回答
  • 2021-02-20 02:47

    I would strongly suggest that you avoid using System.currentTimeMillis (and new Date() etc) in your general code.

    Instead, create a Clock interface representing "a service to give you the current time" and then create one implementation which does use System.currentTimeMillis or whatever, and a fake implementation that you can control explicitly.

    Use dependency injection to make an instance of this service available to code which needs it. In production, use the System.currentTimeMillis version, and in testing use your fake.

    This gives you the ability not just to stop time, but to set it to whatever you want - so you can have static test data which you know will never expire, and you can easily test tricky things around boundaries etc. I've used this approach very successfully in many projects, to the extent that in my Noda Time project it's the way of getting at "the current time".

    Note that if you're doing any serious amount of time work in Java, I'd recommend using Joda Time, and making your Clock interface return an Instant:

    public interface Clock {
        Instant now();
    }
    
    0 讨论(0)
  • 2021-02-20 02:54

    Is it possible to freeze System.currentTimeMillis()? NO

    You need to use a sort of wrapper around that time value if you want your code to be able to be tested

    0 讨论(0)
  • 2021-02-20 02:55

    The new java.time package built into Java 8 includes a java.time.Clock interface "to allow alternate clocks to be plugged in as and when required. Use this instead.

    0 讨论(0)
  • 2021-02-20 02:56

    No, You cann't set or freeze System.currentTimeMills(). But if your requirement is something like that so in that case you can set time in variable and used when ever you want.but System.currentTimeMills() will always returns you the current time value in milliseconds.

    0 讨论(0)
  • 2021-02-20 02:58

    Yes, it is possible, but it is a test code smell.

    You can use a mocking library which enables you to mock static methods (as in this PowerMock example), but you should avoid doing this, and encapsulate the time data as the other answers suggest.

    This is how the test would look like, using PowerMock and Mockito:

    @RunWith(PowerMockRunner.class)
    @PrepareForTest(System.class)
    public class TestTime {
    
        @Test
        public void testTime() {
            PowerMockito.mockStatic(System.class);
            PowerMockito.when(System.currentTimeMillis()).thenReturn(42l);
            System.out.println(System.currentTimeMillis()); //prints 42
    
            //your test code here
        }
    
    }
    
    0 讨论(0)
  • 2021-02-20 03:07

    Yes, solution exists:

    For testing purposes you may create your own wrapper System, for example

    package my.pack;
    
    import java.io.PrintStream;
    
    public class System
    {
        // reuse standard System.out:
        public static PrintStream out = java.lang.System.out;
    
        // do anything with chosen method
        public static long currentTimeMillis()
        {
            return 1234567;
        }
    
    }
    

    and import it into your class which you want to test

    import my.pack.System;
    

    In that case all calls to System will be passed through your own System class.

    NOTE:

    • This is a solution for "how to intercept System.currentTimeMillis()"

    • This is NOT suitable for automatic testing

    • This is NOT an example of "how-to-design-a-good-program". If you ask for a good design, then you need to refactor your code, replace System.currentTimeMillis() - see other answers.

    0 讨论(0)
提交回复
热议问题