java - LocalDateTime.of returns null -
i trying unit test code uses java.time.localdatetime
. i'm able mock working, when add time (either minutes or days) end null
value.
@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 have npe? // assert assert.asserttrue(localdatetime.now() == fixedpointintime); //edit - both null assert.assertnotnull(fixedtomorrow); //test fails here assert.assertequals(12, fixedtomorrow.getdayofmonth()); } }
i understand (well think do) localdatetime
immutable, , think should new instance instead of null
value.
turns out .of
method giving me null
value. why?
according documentation:
use
powermock.mockstatic(classthatcontainsstaticmethod.class)
mock all methods of class.
and:
note can mock static methods in class though class final. method can final. to mock specific static methods of class refer partial mocking section in documentation.
to mock static method in system class need follow this approach.
you told mock static methods, didn't supply mock of()
method.
solution: either add mock of()
method, or change use partial mocking, of()
method not mocked.
basically, read , follow instructions of documentation.
Comments
Post a Comment