Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Testing in MVVM With Clean Architecture

Testing in MVVM With Clean Architecture

Overview on MVVM and clean architecture in android and how to write Unit and UI test case in clean architecture.

Sujata Swamy

October 19, 2019
Tweet

Other Decks in Technology

Transcript

  1. Testing in MVVM with clean Architecture - Sujata Gopal Swamy

    - Sr Android Developer @ Airfi Aviation Solutions
  2. Features of MVVM architecture • Separation of concerns • Separation

    of UI from backend logic • Its target is to achieve the following principle “Keeping UI code simple and free of app logic in order to make it easier to manage”.
  3. • Your code is even more easily testable than with

    plain MVVM. Why MVVM with Clean Architecture? • Your code is further decoupled (the biggest advantage.) • The package structure is even easier to navigate. • The project is even easier to maintain. • Your team can add new features even more quickly.
  4. Pros of using a Clean Architecture • It's easier to

    maintain the project and add new features • Code is more decoupled and testable • You can replace the framework and presentation layers and port your app on a different platform
  5. Cons of Using Clean Architecture • You’ll have to learn

    and understand clean architecture to work on the project • You’ll have to write more code but it pays off
  6. Layers to test in clean architecture 1. Activity / Fragment

    2. Viewmodel 3. Use cases 4. Repository 5. Datasource
  7. UI or Espresso Test @Test public void test() { onView(withId(R.id.spinner)).perform(click());

    onData(allOf(is(instanceOf(String.class)), is("Author1"))) .perform(click()); onView(withId(R.id.spinner)) .check(matches(withSpinnerText("Author1"))); } @Rule public ActivityTestRule<BookActivity> activityRule = new ActivityTestRule<>(BookActivity.class);
  8. Example of writing a unit test for a View model

    @Before public void setupAddEditTaskViewModel() { MockitoAnnotations.initMocks(this); bookViewModel = spy(new BookViewModel(application,mAuthorNameUseCase,mBookUseCase)); } @Rule public InstantTaskExecutorRule instantExecutorRule = new InstantTaskExecutorRule();
  9. Continued.. example. @Test Public void testauthorList() { } MutableLiveData<List<String>> listMutableLiveData

    = new MutableLiveData<>(); List<String> stringList = new ArrayList<>(); stringList.add("author1"); listMutableLiveData.setValue(stringList); verify(mAuthorNameUseCase).getAuthorNamesList(mGetAuthorNameArgumentCaptor.capture()); mGetAuthorNameArgumentCaptor.getValue().onAuthorNameLoaded(listMutableLiveData); assertThat(bookViewModel.getAuthorNameListLiveData() .getValue().size(), is(equalTo(1)));
  10. Example of writing unit test for a datasource @Test public

    void testSalesData() { } List<BookData> bookList = new ArrayList<>(); BookDataSource dataSource = Mockito.mock(BookDataSource.class); Mockito.doReturn(Observable.just(bookList)).when(dataSource) .getBookNameList("Author1"); dataSource.getBookNameList("Author1") .test() .assertValues(bookList) .dispose();
  11. Example of writing unit test for repository @Test public void

    testAuthorNamesListCallback() { } MutableLiveData<List<String>> listMutableLiveData = new MutableLiveData<>(); List<String> stringList = new ArrayList<>(); stringList.add("Author1"); listMutableLiveData.setValue(stringList); bookRepository.getAuthorNameList(mAuthorNameCallBack); verify(bookDataSource).getAuthorNameList(mAuthorNameCallbackCaptor.capture()); mAuthorNameCallbackCaptor.getValue().onAuthorNameLoaded(listMutableLiveData); verify(bookDataSource) .getAuthorNameList(ArgumentMatchers.any(BookDataSource.GetAuthorNameCallback.class));
  12. Writing a unit test case for Use-Case @Test public void

    testUseCaseCallBack() { } MutableLiveData<List<String>> listMutableLiveData = new MutableLiveData<>(); List<String> bookdataList = new ArrayList<>(); bookdataList.add("Author2"); listMutableLiveData.setValue(bookdataList); authorNameUseCase.getAuthorNamesList(mAuthorNameCallBack); // passing mocked object verify(authorNameUseCase).getAuthorNamesList( mAuthorUseCaseCallbackCaptor.capture()); mAuthorUseCaseCallbackCaptor.getValue().onAuthorNameLoaded(listMutableLiveData); verify(authorNameUseCase) .getAuthorNamesList(any(BookRepositoryContract.GetAuthorNameCallback.class));
  13. Key takeaways Overview on MVVM architecture Overview on clean architecture

    Overview on Unit & UI Testing in android with clean architecture
  14. Useful links and resources for mvvm with clean architecture https://www.raywenderlich.com/3595916-clean-architecture-tutorial-for-androi

    d-getting-started#toc-anchor-026 https://five.agency/android-architecture-part-5-test-clean-architecture/ https://www.youtube.com/watch?v=VJi2vmaQe6w&t=1997s
  15. Thank you @sw_swamy Please find below github link for the

    demo project https://github.com/SujataSwamy/testing_in_mvvm_with_clean_architecture https://www.linkedin.com/in/sujata-swamy/