代码之家  ›  专栏  ›  技术社区  ›  Adam Erstelle

LocalDateTime。返回值为空

  •  -1
  • Adam Erstelle  · 技术社区  · 8 年前

    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());
        }
    }
    

    我理解(我想我理解) LocalDateTime 无效的 价值

    .of 给我一个 无效的 价值为什么?

    4 回复  |  直到 8 年前
        1
  •  5
  •   Community Mohan Dere    6 年前

    根据 documentation :

    PowerMock.mockStatic(ClassThatContainsStaticMethod.class) 嘲笑 全部的

    以及:

    注意,即使类是final,也可以在类中模拟静态方法。该方法也可以是最终的。 要仅模拟类的特定静态方法,请参阅 partial mocking 在文档中。

    要在系统类中模拟静态方法,您需要遵循 this 方法

    of() 方法

    共() 共()

    大体上 阅读并遵循文档说明 .

        2
  •  4
  •   user7605325 user7605325    8 年前

    @Andreas' answer in your own answer

    我只想添加一种不同的方法。为了测试当前日期/时间,可以使用 java.time.Clock . 通过这个类,您可以创建一个 fixed clock (a) Clock use it in your test

    这样,就不需要模拟静态方法(我不得不从测试类中删除PowerMock注释)。唯一的区别是时钟必须传递给 now()

    @Test
    public void shouldCorrectlyCalculateTimeout() {
        // create a clock that always returns the same current date/time
        LocalDateTime fixedPointInTime = LocalDateTime.of(2017, 9, 11, 21, 28, 47);
        ZoneId zone = ZoneId.systemDefault();
        Clock clock = Clock.fixed(fixedPointInTime.atZone(zone).toInstant(), zone);
    
        // use the clock in now() method
        LocalDateTime fixedTomorrow = LocalDateTime.now(clock).plusDays(1);
    
        // assert (use equals() instead of == because it doesn't return the same instance)
        Assert.assertTrue(LocalDateTime.now(clock).equals(fixedPointInTime));
        Assert.assertNotNull(fixedTomorrow);
        Assert.assertEquals(12, fixedTomorrow.getDayOfMonth());
    }
    

    equals() == now(clock) 创建一个新实例,尽管所有实例都对应于相同的日期/时间(IMO,这才是重要的)。


    PS: 在上面的代码中,我使用的是JVM默认时区( ZoneId.systemDefault() can be changed without notice, even at runtime ,所以最好始终明确您使用的是哪一个。

    在这个特定的代码中,时区部分被忽略,因此您可以使用任何区域,这不会有太大的区别-除非您在夏令时转换时获得时区和本地日期的组合,这可能会产生意外的结果。

    如果你不想依赖它,你可以替换 ZoneOffset.UTC 相反(UTC没有任何夏令时效果)。在这种情况下,您可以这样创建时钟:

    ZoneOffset utc = ZoneOffset.UTC;
    Clock clock = Clock.fixed(fixedPointInTime.toInstant(utc), utc);
    
        3
  •  1
  •   Adam Erstelle    8 年前

    所以PowerMockito。mockStatic正在处理下一行代码。只需将fixedpointime的实例化移动到mockStatic之前执行。。。。让一切顺利。

        4
  •  0
  •   Nissa TheRealPir    8 年前

    public class SomeClass{
    
        public static void main(String[] args) {
            LocalDateTime now = getCurrentLocalDateTime(); 
            System.out.println(now);
        }
    
        private LocalDateTime getCurrentLocalDateTime() {
            return LocalDateTime.now();
        }
    
    }
    

    Test 您使用的类:

    @PrepareForTest(SomeClass.class)
    
    @RunWith(PowerMockRunner.class)
    

    在测试用例中:

    LocalDateTime tommorow= LocalDateTime.now().plusDays(1);
    
    SomeClass classUnderTest = PowerMockito.spy(new SomeClass());
    
    PowerMockito.when(classUnderTest, "getCurrentLocalDateTime").thenReturn(tommorow);
    
    推荐文章