I have a singleton that contains reference to statistics object.
When I run couple of unit test on the program that uses that singleton - the values sustained between the tests.
I though that when I'm doing Program.Main() it all starts over between unit tests, but somehow it remembers the results from last test.
How can I write unit tests that will be isolated from each other (I don't want clean() functions - I want it to start over with new "everything"),
It's very difficult to write unit tests for code that uses singletons because it is generally tightly coupled with the singleton instance, which makes it hard to control the creation of singleton or mock it.
The Singleton pattern is a useful pattern in Android development, and Java development at large. The Singleton pattern is defined as follows: In software engineering, the singleton pattern is a design pattern that restricts the instantiation of a class to one object.
If your singleton contains mutable state that cannot be reset between tests, then it will affect unit testing as the tests will need to be run in a specific order and the state accounted for. If your singleton contains state that changes of time, it will prevent you reliably running tests in parallel.
Just use Reflection. With provided sample code you need to make sure the static constructor is called before setting the static field to the mocked object. Otherwise it might overwrite your mocked object. Just call anything on the singleton that has no effect before setting up the test.
I wrote a post about that here: http://pvlerick.github.io/2017/03/how-to-get-rid-of-a-singleton
TL;DR:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With