Skip to content

How to mock in java 8


I am testing a functionality ABC that uses

In the methode ABC i’am comparing an entry date with the

I want my test to be passed at any day so i have to mock the

This is my test:

public void testClass() {

       LocalDateTime mock = Mockito.mock(LocalDateTime.class);


I am using JAVA 8

the date shown in the console is always the real LacalDateTime not my wanted LacalDateTime (2030-01-01) .

I am not getting errors.

Any help please ?


You should use Mockito#mockStatic for this use case

You can use it like this

try(MockedStatic<LocalDateTime> mock = Mockito.mockStatic(LocalDateTime.class, Mockito.CALLS_REAL_METHODS)) {
    // Put the execution of the test inside of the try, otherwise it won't work

Notice the usage of Mockito.CALLS_REAL_METHODS which will guarantee that whenever LocalDateTime is invoked with another method, it will execute the real method of the class