Scrivio Interview library
CodingFoundations6 min read · updated 12 November 2026

How do you mock a static method such as LocalDate.now() with Mockito, and why is it often a sign to change the design?

With Mockito 5 we can write try (MockedStatic<LocalDate> m = mockStatic(LocalDate.class, CALLS_REAL_METHODS)) { m.when(LocalDate::now).thenReturn(fixed); ... }. The mock is active only inside the try block and only on the current thread. It works, but needing it usually means the code hides a dependency on the clock, and the better design is to inject a java.time.Clock and call LocalDate.now(clock).

Why this question is asked

Code that depends on today's date is everywhere: discounts that end at midnight, invoices due in 30 days, trials that expire. Tests for such code break on certain days or pass by luck. Interviewers ask this question because it has two layers: the mechanics of static mocking, and the judgement to see that a hard-to-test static call is a design signal. In this answer, we will see why LocalDate.now() is hard to test, mock it with mockStatic, understand its scope and one important trap, and then remove the need for it with an injected Clock.

1Why LocalDate.now() Makes Tests Hard

Here is a small piece of code that decides whether a trial is still active. It calls LocalDate.now() directly. A static method belongs to the class, not to an object, so there is no object we can replace with a mock from the outside.

Java
public class TrialService {
public boolean isActive(Trial trial) {
return !LocalDate.now().isAfter(trial.endsOn());
}
}
// A test written on 2026-11-05 with endsOn = 2026-11-30
assertTrue(service.isActive(trial)); // passes today, fails from 2026-12-01

Notice that the test result depends on the day it runs. That is exactly the kind of test that fails months later in a build nobody touched. We need to control what 'today' means.

2Mocking the Static Call With mockStatic

Mockito can replace static methods since version 3.4, using the inline mock maker, which is the default from Mockito 5. mockStatic() returns a MockedStatic object, and we stub methods through it with a method reference.

Java
import static org.mockito.Mockito.*;
@Test
void trialIsActiveOnItsLastDay() {
LocalDate lastDay = LocalDate.of(2026, 11, 30); // created BEFORE the mock
Trial trial = new Trial(lastDay);
try (MockedStatic<LocalDate> m = mockStatic(LocalDate.class, CALLS_REAL_METHODS)) {
m.when(LocalDate::now).thenReturn(lastDay);
assertTrue(new TrialService().isActive(trial)); // passes on any real date
m.verify(LocalDate::now); // now() was called once
}
// here LocalDate.now() is real again
}

MockedStatic is AutoCloseable, which is why we use it in a try-with-resources block. When the block ends, close() runs and the real static methods come back. If we forget to close it, the mock stays active on that thread and leaks into later tests, and Mockito refuses to create a second static mock of the same class on that thread.

3The Scope: This Block, This Thread

A static mock is active only between mockStatic() and close(), and only on the thread that created it. The second part surprises people. If the code under test calls LocalDate.now() on another thread, for example inside CompletableFuture.supplyAsync() or a Spring @Async method, that thread sees the real date.

The reason for this design is safety. Static methods are shared by the whole JVM. If a mock of LocalDate.now() were global, two tests running in parallel would fight over it, and JDK or framework code using LocalDate on other threads would see the fake date too. Keeping the mock thread-local limits the blast radius to our own test.

Java
try (MockedStatic<LocalDate> m = mockStatic(LocalDate.class, CALLS_REAL_METHODS)) {
m.when(LocalDate::now).thenReturn(lastDay);
LocalDate.now(); // 2026-11-30
CompletableFuture.supplyAsync(LocalDate::now).join(); // the real date: another thread
}
Step throughLifetime of a static mock
  1. mockStatic(LocalDate.class)bytecode of LocalDate is changed for this thread
  2. m.when(LocalDate::now)stub the call
  3. Code under test runsnow() returns the stub on this thread only
  4. Block ends, close() runsreal behaviour is restored

4 steps in order. Play them, or step through them one at a time.

4A Trap: Every Static Method Is Mocked

Without CALLS_REAL_METHODS, mockStatic(LocalDate.class) replaces every static method of LocalDate, not just now(). Inside the block, LocalDate.of(2026, 11, 30) and LocalDate.parse(...) return null. A test that builds its expected date inside the block then fails with a confusing NullPointerException.

Java
try (MockedStatic<LocalDate> m = mockStatic(LocalDate.class)) {
m.when(LocalDate::now).thenReturn(LocalDate.of(2026, 11, 30));
// LocalDate.of(...) is mocked too and returns null, so now() is stubbed to return null
LocalDate.now(); // null
}

There are two ways out, and the earlier example used both. We create the dates we need before opening the mock, and we pass CALLS_REAL_METHODS as the default answer, so any static method we did not stub runs for real. Mocking core JDK classes has other limits too: some classes and methods used by Mockito itself cannot be mocked safely, which is one more reason to keep this tool for rare cases.

5The Better Design: Inject a Clock

Look again at the difficulty. TrialService secretly depends on the system clock, and the only way to control that dependency is to rewrite bytecode. That is the design signal. java.time already offers a clean way out: every now() method has an overload that takes a Clock, an object that answers the question 'what time is it?'. We make the clock a constructor dependency.

Java
public class TrialService {
private final Clock clock;
public TrialService(Clock clock) { this.clock = clock; }
public boolean isActive(Trial trial) {
return !LocalDate.now(clock).isAfter(trial.endsOn());
}
}
// Production (for example a Spring bean): @Bean Clock clock() { return Clock.systemDefaultZone(); }
// Test: no mocking framework needed
Clock fixed = Clock.fixed(Instant.parse("2026-11-30T12:00:00Z"), ZoneOffset.UTC);
assertTrue(new TrialService(fixed).isActive(new Trial(LocalDate.of(2026, 11, 30)))); // passes
Clock nextDay = Clock.offset(fixed, Duration.ofDays(1));
assertFalse(new TrialService(nextDay).isActive(new Trial(LocalDate.of(2026, 11, 30)))); // passes

Notice what changed. The dependency is now visible in the constructor, the test needs no special mock maker, it works on any thread, and it can move time forward with Clock.offset(). The time zone also becomes explicit, which removes a second hidden source of flaky tests. So mockStatic is a good emergency tool for legacy code we cannot change yet, while Clock injection is the lasting fix.

6Follow-up questions

Can I use mockStatic with JUnit 5 parallel execution?

Yes, because the mock is thread-local, tests on other threads are not affected. The risk is code under test that hops threads, where the mock silently does not apply.

Do I need any setup to use mockStatic in Mockito 5?

No extra dependency: Mockito 5 uses the inline mock maker by default, which supports static and final mocking. On older versions we needed mockito-inline. Recent JDKs warn when Mockito's agent attaches dynamically, so configuring it as a -javaagent in the build is recommended; check your Mockito and JDK versions.

How do I test code that calls Instant.now() or LocalDateTime.now()?

The same way. Instant.now(clock), LocalDateTime.now(clock) and ZonedDateTime.now(clock) all take the injected Clock, so one Clock bean covers the whole application.

7Conclusion

In this answer, we saw how to mock LocalDate.now() with Mockito's mockStatic inside a try-with-resources block, and that the mock is active only inside that block and only on the current thread. We met the trap that every static method of the class gets mocked unless we use CALLS_REAL_METHODS. Then we saw that needing a static mock is a sign of a hidden dependency, and that injecting a java.time.Clock and calling LocalDate.now(clock) gives simpler tests and clearer code.

What to say in the interviewAbout 70 seconds aloud

Open with

With Mockito 5, which uses the inline mock maker by default, I can open a static mock in a try-with-resources block: try (MockedStatic<LocalDate> m = mockStatic(LocalDate.class, CALLS_REAL_METHODS)), then m.when(LocalDate::now).thenReturn(fixedDate).

Then, if they want more

The mock is active only until the block closes and only on the current thread, so code that runs on another thread, like an async task, still sees the real date. I pass CALLS_REAL_METHODS and create my dates before the block, because otherwise every static method of LocalDate is mocked and LocalDate.of returns null.

But I treat the need for this as a design smell. The class secretly depends on the system clock, and I can only control it by rewriting bytecode. The cleaner fix is to inject a java.time.Clock through the constructor, call LocalDate.now(clock), register Clock.systemDefaultZone() as a bean in production, and pass Clock.fixed in tests.

Then the test needs no mocking at all, works on any thread, and I can move time with Clock.offset. I keep mockStatic for legacy code I cannot change yet.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "How do you mock a static method such as LocalDate.now() with Mockito, and why is it...": https://scrivio-interview-library.netlify.app/java/mocking/ac1719bcb0/