Mocking Joda DateTime method using Mockito

前端 未结 1 637
北荒
北荒 2021-01-02 05:03

I want millis to return specified value.

public long myMethod(){
    DateTime nowDateTime = new DateTime(DateTimeZone.UTC);
    long millis = no         


        
相关标签:
1条回答
  • 2021-01-02 05:59

    Just use the org.joda.time.DateTimeUtils#setCurrentMillisFixed method of JodaTime which was designed to fix new DateTime() to a different time than the current time. To return to the normal time use org.joda.time.DateTimeUtils#setCurrentMillisSystem afterwards. No mocking needed.

    
    @Test
    public void test() {
    DateTimeUtils.setCurrentMillisFixed(10L);
    // .. your code
    }
    
    @After
    public void cleanup() {
    // Make sure to cleanup afterwards
    DateTimeUtils.setCurrentMillisSystem()
    }
    
    
    0 讨论(0)
提交回复
热议问题