Michael Pohl
09/29/2020, 7:28 AMcoEvery { mockRepo.saveToServer(mockArgument) } returns myResult
Now this will of course happen immediately. In reality it could take a moment. Now while this call is in flight, I flip a boolean to prevent issuing a second call while the first one is still underway. When it returns, I flip the boolean back. Is there a proper way to unit test this, preferably using Mockk
, of course...?Joe
09/29/2020, 4:30 PMcoEvery {...} coAnswers { delay(100); myResult }
should work for simulating a delay, though you probably want to use some sort of synchronization mechanism (atomic booleans/ channel/ mutex/ semaphore/ etc) rather than a delay
to coordinate between the test body and the mock.Michael Pohl
09/29/2020, 6:16 PMJoe
09/29/2020, 6:58 PMCountDownLatch
(which is blocking so not ideal) to do something similar (using easymock instead of mockk though)christophsturm
10/06/2020, 3:55 PMMichael Pohl
10/07/2020, 4:31 AMchristophsturm
10/07/2020, 8:51 AMMichael Pohl
10/19/2020, 9:50 AMJob
is running, it is stored in a variable, so that actions can first test if that variable isn't null and react to that (in this case by just returning if a second request is started).
(it's happening in a Android viewModel by the way, but that should not be important here.)