Make Mocked Static Methods Available for All Threads (or at least subthreads)

Viewed 391

I have the following code that I want to test using Junit5

public void myMethod() {
   final ExecutorService executor = ThreadsUtils.newFixedThreadPool();
   executor.submit(() -> {
      ClassWithStaticMethod.staticMethod();
   })
}

I wrote the the following test case

try (MockedStatic<ClassWithStaticMethod> mockedStatic = mockStatic(ClassWithStaticMethod.class)) {
            ...
            someObject.myMethod(COUNTRY_CODE);
            ...
}

The above mocking does not work as per the the docs of mockStatic, the mocked static method's scope is only the current thread. However, how can we mock static method for multiple-threads scenario?

0 Answers
Related