Mocked static methods are not available in other threads
See original GitHub issue- The mockito message in the stacktrace have useful information, but it didn’t help
- The problematic code (if that’s possible) is copied here; Note that some configuration are impossible to mock via Mockito
- Provide versions (mockito / jdk / os / any other relevant information)
- Provide a Short, Self Contained, Correct (Compilable), Example of the issue (same as any question on stackoverflow.com)
- Read the contributing guide
Mockito v3.4.0
Mockito.mockStatic()
mocks method seems to be working in the current thread only. I need the static mock to be available globally in multiple threads.
for e.g:
try ( MockedStatic<LegacyTextUtils> utilMock = Mockito.mockStatic(LegacyTextUtils.class)){
utilMock.when(LegacyTextUtils::getReqText).thenReturn("test-123");
}
The above mock is only available in the current thread. Is there any way to make it available globally?
Issue Analytics
- State:
- Created 3 years ago
- Reactions:45
- Comments:9
Top Results From Across the Web
mockito mock static function does not work if the function is ...
There seem to be a few things wrong with your test: Don't mock static methods. Refactor your code to use an interface and...
Read more >Static method call not being mocked on some threads using ...
This mocking of Realm.getDefaultInstance() works completely fine with simple RxJava observable chains/trees even when some of them are executed in different ...
Read more >Mocking Static Methods With Mockito - Baeldung
In this tutorial, we'll take a look at how we can now mock static methods using the latest version of Mockito.
Read more >Mocking static methods made possible in Mockito 3.4.0
The mocked static method is only visible in the try-with-resources block. Different outcomes can be tested in isolation. · Static methods can ...
Read more >MockedStatic (Mockito 3.4.3 API) - javadoc.io
Represents an active mock of a type's static methods. The mocking only affects the thread on which this static mock was created and...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This workaround worked for me. Thanks @bastiao !
@Ignore on the methods have mockStatic (1) Write a method to call (1) async Ex: