How to Mock only LocalDate.now() using PowerMock

Viewed 7268

I want to mock only the now() of LocalDate using PowerMock. It seems that it's ignoring the thenReturn value:

java.lang.AssertionError: 
Expected :2008
Actual   :2017

Test setup:

@PrepareForTest(LocalDate.class)
@RunWith(PowerMockRunner.class)
public class UserTest {

    @Test
    public void nowShouldReturnSameYear() throws Exception {
        LocalDate expected = LocalDate.parse("2008-04-04");

        PowerMockito.spy(LocalDate.class);
        when(LocalDate.now()).thenReturn(expected);

        Foo foo = new Foo();
        assertEquals(expected.getYear(), foo.getRightNow().getYear());
    }

Foo.java

public LocalDate getRightNow(){
        final LocalDate rightNow = LocalDate.now();
        return rightNow;
    }
1 Answers
Related