Hi Javier,
Thank you for contacting us.
Your conclusion about not applying static mocks in parallel execution is correct. For example, take the following:
We have a static class, like this:
Now, we can write the following test:
It will work as expected. However, if we use Parallel.For, the test will fail:
This is because, as noted above, the static arrangement is not applied for all threads.
One way to make the above test green (to make it pass) is
to
add the mock creations and their arrangements inside the parallel for, like this:
This, however is not a good practice as it leads to more complicated and tiresome tests.
Another way to make this work, is to
use a singleton. Below is the a singleton of the previous Foo class:
Now, the parallel unit test works:
To summarize:
- Firstly, I do not recommend parallel test execution. It may give performance, but a lot of concurrency issues can occur.
- If you decide to stick to the parallel test execution, I suggest using singleton classes instead of statics. By far, this is the only reliable way to isolate the different threads.
- If singleton is not an option for you (e.g.: you may need to refactor a lot of code), you can try adding the mock creations and their arrangements in parallel, as well.
I hope this gives the required answers. Let me know if I can assist you further.
Regards,
Kaloyan
Telerik
Share what you think about JustTrace & JustMock with us, so we can become even better! You can use the built-in feedback tool inside JustTrace, our
forums, or our
JustTrace or
JustMock portals.