Some years ago I reported some explanation from their site
in this old post I wrote
An alternate approach to Robolectric is to use mock frameworks such as
Mockito or to mock out the Android SDK. While this is a valid
approach, it often yields tests that are essentially reverse
implementations of the application code. Roboelectric allows a test
style that is closer to black box testing, making the tests more
effective for refactoring and allowing the tests to focus on the
behavior of the application instead of the implementation of Android.
You can still use a mocking framework along with Robolectric if you
like.
To understand the concept of Roboelectric is needed to understand what is a Shadows
objects, that mimicks the Android classes. When you need to test Android implementations, can be difficult to execute Junit Tests, because there are a lot of Android components: Views, Intent, Bundle,Fragments just to name some of them that Roboelectric could test. Furthermore if your code is tightly coupled can be difficult to run UI tests, that are tests that need to run via emulator and are quite slow, although often needed. Junit tests are much faster and do not need emulator. Roboelectric helps you to test specific Android components, without using an emulator.
The sample you ask for the viewModel is really a generic question, because ViewModels can be really different. Usually a viewModel contains the business logic of the view without usually having reference to the view, so should be testable with Mockito with. In Stackoverflow we do not express opinions regarding which framework is used, so I would not make any judgement. As consequence a whatever Roboelectric example should clarify you how to use it in the viewModel or whatever place is needed, a view could be probably a better place. Please notice that Roboelectric as every framework has pro and cons, but we cannot discuss of that on StackOverflow, a research in the internet and your toy projects will do for you.
Also when you do Junit testing you test in isolation, so the class has a relative value as far there is business logic to test your SUT
, system under test.