Search code examples
androidmodel-view-controllermvvmmvpandroid-mvvm

Why is unit testing harder in MVC than in MVP and MVVM


I've been doing some research on pros and cons of MVC/MPV/MVVM and one common theme is that MVC is harder to unit test than MVP and MVVM but I don't fully understand why.

From my current understanding, in MVC, the view is dependent on the model and and controller so to test the view, both the controller and the model must be mocked. How is MVP/MVVM improve on this?


Solution

  • In MVC, There is no separate component to handle UI or Presentation logic. Most of the time, it is written in View layer (Eg. Activity). You have to depend on frameworks like Robolectric which can understand Android ecosystem while writing unit tests. This makes difficult to unit test presentation logic. The other two patterns (MVP, MVVM) focus on decoupling Android dependencies with the help of interfaces and event driven pattern which simplifies unit testing. This blog explains more in detail.

    I would suggest to explore Guide to app architecture and Clean architecture for building highly modularized and testable application.