Sagar Malhotra
1 min readSep 16, 2023

--

Because we want to ensure that the Main dispatcher is reset to its original state after each test. This is important because the Main dispatcher is a single-threaded dispatcher, and if we don't reset it after each test, it can lead to unexpected behavior in subsequent tests.

If we call Dispatchers.resetMain() in an @AfterAll function, it would only be called once after all of the tests in the class have finished running. This would mean that the Main dispatcher would not be reset after each individual test, which could lead to problems.

For example, let's say we have two tests that both launch coroutines on the Main dispatcher. If we don't reset the Main dispatcher after each test, the coroutines from the first test could still be running when the second test starts. This could lead to unexpected behavior in the second test, such as race conditions or deadlocks.

To avoid these problems, it is best to call Dispatchers.resetMain() in an @AfterEach function. This will ensure that the Main dispatcher is reset to its original state after each test, which will help to prevent unexpected behavior.

--

--

Sagar Malhotra
Sagar Malhotra

Written by Sagar Malhotra

Android dev by day, Creator by night.🥷🏻 Sharing my passion through videos, blogs and conferences.🚀

No responses yet