2

I know that on official Robolectric website say that this framework is for unit test, but on all sample robolectric is used for test views... E g: Robolectric can be used to check if the text of textview X is "dog", can check if the button is clickable ... But it is not unit test... It is integration test, and I can do this with espresso too...

If robolectric is really unit test, I can use this for test viewmodel, repository, extensions?

If it is for unit test can you give me a sample that show viewmodel test using only Robolectric (no Mockito)?

felipe.rce
  • 237
  • 2
  • 8
  • 35

1 Answers1

5

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.

trocchietto
  • 2,607
  • 2
  • 23
  • 36
  • My confusion is because on Robolectric official website says that don't need mocking framework like Mockito and I can use only Robolectric , in a very clean MVVM arch my viewmodel don't have any views Soo... I can test using Mockito or only with junit (using Koin too)... Then... Robolectric is only for android access? – felipe.rce Jan 20 '20 at 21:19
  • Eg: If my app has logic on view, then I can use Robolectric for test this logic on the view, because Robolectric can access Android framework, but if I use MVVM then don't need robolectric for unit testing... I think I understand it... (Or not) – felipe.rce Jan 20 '20 at 21:21
  • the setup of Roboelectric can be complex if you use some CI tools as jacoco, etc. if your architecture is clean, for instance with LiveData that wrap RXJava use cases, you really need a simple mock library. If your code is more coupled or really important, you are a fan of TDD, test drive architecture, Roboelectric becomes really interesting – trocchietto Jan 20 '20 at 21:23
  • yeah you get my point, I would not suggest you to test "Android components" even with Roboelectric in your viewModel, because a viewModel in MVVM is made to separate views from business logic, for a number of architectural reasons where easier testing is one of them. Hope I replied you, but in the world of the architecture everything can become subjective, and this is not the nature of stackoverflow, when you solve specific problems, not broad discussions. – trocchietto Jan 20 '20 at 21:25