Ada*_*lle -1 java datetime mockito java-time powermockito
我正在尝试对使用java.time.LocalDateTime
. 我能够让模拟工作,但是当我增加时间(分钟或天)时,我最终会得到一个null
值。
@RunWith(PowerMockRunner.class)
@PrepareForTest({ LocalDateTime.class })
public class LocalDateTimeMockTest
{
@Test
public void shouldCorrectlyCalculateTimeout()
{
// arrange
PowerMockito.mockStatic(LocalDateTime.class);
LocalDateTime fixedPointInTime = LocalDateTime.of(2017, 9, 11, 21, 28, 47);
BDDMockito.given(LocalDateTime.now()).willReturn(fixedPointInTime);
// act
LocalDateTime fixedTomorrow = LocalDateTime.now().plusDays(1); //shouldn't this have a NPE?
// assert
Assert.assertTrue(LocalDateTime.now() == fixedPointInTime); //Edit - both are Null
Assert.assertNotNull(fixedTomorrow); //Test fails here
Assert.assertEquals(12, fixedTomorrow.getDayOfMonth());
}
}
Run Code Online (Sandbox Code Playgroud)
我明白(我想我明白)这LocalDateTime
是不可变的,我认为我应该得到一个新实例而不是null
值。
原来是.of
方法给了我一个null
价值。为什么?
归档时间: |
|
查看次数: |
2382 次 |
最近记录: |