我如何模拟 java.time.LocalDate.now()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32792000/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 13:05:57  来源:igfitidea点击:

How can I mock java.time.LocalDate.now()

javajava-8java-timesystemtime

提问by Neil

In my test case, I need test time sensitive method, in that method we're using java 8 class LocalDate, it is not Joda.

在我的测试用例中,我需要测试时间敏感的方法,在该方法中我们使用的是 java 8 类 LocalDate,它不是 Joda

What can I do to change time, when I'm running test

当我运行测试时,我能做些什么来改变时间

采纳答案by assylias

In your code, replace LocalDate.now()with LocalDate.now(clock);.

在您的代码中,替换LocalDate.now()LocalDate.now(clock);.

You can then pass Clock.systemDefaultZone()for production and a fixed clockfor testing.

然后Clock.systemDefaultZone(),您可以通过生产和固定时钟进行测试。



This is an example :

这是一个例子:

First, inject the Clock. If you are using spring boot just do a :

首先,注入Clock. 如果您使用的是弹簧靴,只需执行以下操作:

@Bean
public Clock clock() {
    return Clock.systemDefaultZone();
}

Second, call LocalDate.now(clock)in your code :

其次,调用LocalDate.now(clock)您的代码:

@Component
public class SomeClass{

    @Autowired
    private Clock clock;

    public LocalDate someMethod(){
         return LocalDate.now(clock);
    }
}

Now, inside your unit test class :

现在,在您的单元测试类中:

// Some fixed date to make your tests
private final static LocalDate LOCAL_DATE = LocalDate.of(1989, 01, 13);

// mock your tested class
@InjectMocks
private SomeClass someClass;

//Mock your clock bean
@Mock
private Clock clock;

//field that will contain the fixed clock
private Clock fixedClock;


@Before
public void initMocks() {
    MockitoAnnotations.initMocks(this);

    //tell your tests to return the specified LOCAL_DATE when calling LocalDate.now(clock)
    fixedClock = Clock.fixed(LOCAL_DATE.atStartOfDay(ZoneId.systemDefault()).toInstant(), ZoneId.systemDefault());
    doReturn(fixedClock.instant()).when(clock).instant();
    doReturn(fixedClock.getZone()).when(clock).getZone();
}

@Test
public void testSomeMethod(){
    // call the method to test
    LocalDate returnedLocalDate = someClass.someMethod();

    //assert
    assertEquals(LOCAL_DATE, returnedLocalDate);
}

回答by Gebb

You might also want to pass a fixed clock in production (the value of which is fixed at the start of a transaction) to avoid using inconsistent "now" in different entities and requests. See this questionfor details.

您可能还希望在生产中传递一个固定的时钟(其值在事务开始时固定)以避免在不同的实体和请求中使用不一致的“现在”。有关详细信息,请参阅此问题

回答by Aivean

You can refactor you code to make it test-friendly, for example, replace all invocations of LocalDate.now()with invocation of some method of custom mockable non-static class.

您可以重构代码以使其对测试友好,例如,将所有调用替换LocalDate.now()为自定义可模拟非静态类的某些方法的调用。

Alternatively, you can use PowerMock's mockStatic.

或者,您可以使用PowerMock 的 mockStatic

回答by SHoko

If we need to mock static methods like now() we can use multiple alternatives like PowerMock:

如果我们需要模拟像 now() 这样的静态方法,我们可以使用多种替代方法,例如PowerMock

@RunWith(PowerMockRunner.class)
@PrepareForTest({ LocalDateTime.class })
public class LocalDateTimeUnitTest {

    @Test
    public void givenLocalDateTimeMock_whenNow_thenGetFixedLocalDateTime() {
        Clock clock = Clock.fixed(Instant.parse("2014-12-22T10:15:30.00Z"), ZoneId.of("UTC"));
        LocalDateTime dateTime = LocalDateTime.now(clock);
        mockStatic(LocalDateTime.class);
        when(LocalDateTime.now()).thenReturn(dateTime);
        String dateTimeExpected = "2014-12-22T10:15:30";

        LocalDateTime now = LocalDateTime.now();

        assertThat(now).isEqualTo(dateTimeExpected);
    }
}

Or JMockit, indeed with JMockit we can use the MockUp class:

或者JMockit,确实有了 JMockit 我们可以使用 MockUp 类:

@Test
public void givenLocalDateTimeWithJMock_whenNow_thenGetFixedLocalDateTime() {
    Clock clock = Clock.fixed(Instant.parse("2014-12-21T10:15:30.00Z"), ZoneId.of("UTC"));
    new MockUp<LocalDateTime>() {
        @Mock
        public LocalDateTime now() {
            return LocalDateTime.now(clock);
        }
    };
    String dateTimeExpected = "2014-12-21T10:15:30";

    LocalDateTime now = LocalDateTime.now();

    assertThat(now).isEqualTo(dateTimeExpected);
}

Or the Expectations class:

或期望类:

@Test
public void givenLocalDateTimeWithExpectations_whenNow_thenGetFixedLocalDateTime() {
    Clock clock = Clock.fixed(Instant.parse("2014-12-23T10:15:30.00Z"), ZoneId.of("UTC"));
    LocalDateTime dateTimeExpected = LocalDateTime.now(clock);
    new Expectations(LocalDateTime.class) {
        {
            LocalDateTime.now();
            result = dateTimeExpected;
        }
    };

    LocalDateTime now = LocalDateTime.now();

    assertThat(now).isEqualTo(dateTimeExpected);
}

We can find more examples here.

我们可以在此处找到更多示例。

Another simple alternative is to use the now()method with a fixed Clock instance. Certainly, most of the classes in java.time package have a now() method with a Clock parameter:

另一个简单的替代方法是将now()方法与固定的 Clock 实例一起使用。当然,java.time 包中的大多数类都有一个带有 Clock 参数的 now() 方法

@Test
public void givenFixedClock_whenNow_thenGetFixedLocalDateTime() {
    Clock clock = Clock.fixed(Instant.parse("2014-12-22T10:15:30.00Z"), ZoneId.of("UTC"));
    String dateTimeExpected = "2014-12-22T10:15:30";

    LocalDateTime dateTime = LocalDateTime.now(clock);

    assertThat(dateTime).isEqualTo(dateTimeExpected);
}

回答by Strax

Using Spring:

使用弹簧:

ClockConfiguration class:

时钟配置类:

@Configuration
public class ClockConfiguration {
    private final static LocalDate LOCAL_DATE = LocalDate.of(2019, 12, 17);

    @Bean
    @ConditionalOnMissingBean
    Clock getSystemDefaultZoneClock() {
        return Clock.systemDefaultZone();
    }

    @Bean
    @Profile("test")
    @Primary
    Clock getFixedClock() {
        return Clock.fixed(LOCAL_DATE.atStartOfDay(ZoneId.systemDefault()).toInstant(), ZoneId.systemDefault());
    }
}

SomeService class:

SomeService 类:

@Service
@RequiredArgsConstructor
public class SomeService {

    private final Clock clock;

    public void someMethod(){
        ...

        LocalDateTime.now(clock)
        LocalDate.now(clock)

        ...
    }
}

You must have an active "test" profile in the test:

您必须在测试中有一个活动的“测试”配置文件:

SomeServiceTest class:

SomeServiceTest 类:

@ActiveProfiles("test")
@EnableConfigurationProperties
@SpringBootTest(classes = [YourAppMainClass])
class SomeServiceTest {
    ...
}