Testable Adapter class with MVP

I found this interesting article about Android adapters. It is not very well written but you can easily get the idea once you dig into the code from Github.

The Adapter dual nature

Well, Adapter and its name suggest that it adapts the data to the view. But what happens if you only want to test the view nature of the adapter or the data handling methods. If you work directly with the class it can cause you a lot of headaches. That’s why you split the adapter class into two interfaces: Data & View

View & Data models

So there is this FlickrAdapterClass which implements both the data interface (PhotoDataModel) and the view interface (PhotoAdapterView).

Dagger2 to the help

After that you can easily export the adapter from Dagger as it is done in the MainModule. You can also export only the data part of it or the view part and you can easily mock their implementations after that.


Now you can easily test and mock the adapter so your unit tests will be clearer. It is a neat idea and I really liked it.

You may also like...