mockStatic(Realm.class);
Realm mockRealm = mock(Realm.class);
when(Realm.getDefaultInstance())
.thenReturn(mockRealm);
This mocking of Realm.getDefaultInstance() works completely fine with simple RxJava observable chains/trees even when some of them are executed in different threads and are calling the Realm.getDefaultInstance() method. But when the test (chain/tree of observables) is quite complex it happens that in some threads the call of Realm.getDefaultInstance() executes the real method which leads to unit test stuck. Unfortunatelly I haven't found the only and necessary conditions when that happens so I cannot present you some short code which reproduces the issue by itself.
Is that some known issue? Do you have any idea where to look for an explanation of this issue?
MockingProgress e = (MockingProgress)Whitebox.invokeMethod(ThreadSafeMockingProgress.class, "threadSafely", new Object[0]);
Forgot to mention that I'm using PowerMock 1.6.4 because of Robolectric.
Have you tried @PrepareForTest(Realm.class) ?
Is Mockito thread-safe?
For healthy scenarios Mockito plays nicely with threads. For instance, you can run tests in parallel to speed up the build. Also, you can let multiple threads call methods on a shared mock to test in concurrent conditions. Check out a
timeout()
feature for testing concurrency.However Mockito is only thread-safe in healthy tests, that is tests without multiple threads stubbing/verifying a shared mock. Stubbing or verification of a shared mock from different threads is NOT the proper way of testing because it will always lead to intermittent behavior. In general, mutable state + assertions in multi-threaded environment lead to random results. If you do stub/verify a shared mock across threads you will face occasional exceptions like:
WrongTypeOfReturnValue
, etc.
--
You received this message because you are subscribed to the Google Groups "PowerMock" group.
To unsubscribe from this group and stop receiving emails from it, send an email to powermock+...@googlegroups.com.
To post to this group, send email to powe...@googlegroups.com.
Visit this group at https://groups.google.com/group/powermock.
For more options, visit https://groups.google.com/d/optout.
MockingProgress e = (MockingProgress)Whitebox.invokeMethod(ThreadSafeMockingProgress.class, "threadSafely", new Object[0]);
in MockitoMethodInvocationControl class. By the stuck I mean that the test is waiting infinitely on that line or at least I cannot step into or step over to another line of PowerMock/Mockito code.