简体   繁体   中英

Unit-Testing in Android

I've written a lot of code for an app, but I didn't use a Model-View-Presenter approach. I need to do unit tests for the app. I've been going around looking for tutorials and many people either have their methods static or they use MVP. Should I change all my code to MVP or there's a simpler way to get unit tests working without making my methods and variables static?

I'm planning to use Mockito and JUnit4. I'm developing on AndroidX.

The architecture used (MVP or any other) has nothing to do with unit tests. What's more, it is even easier to test typical object instances than static methods or variables.

The only thing (and a very important one) you have to consider is NOT to mix pure Java-related code with Android-related code, so you will be able to unit test all Java objects with Mockito and JUnit, and maybe using Espresso or Robolectric to define integration tests for the Android-related objects. Separation of concerns (SoC) is really important, specially working with complex frameworks like Android. Architecture models and patterns help you to follow SoC, of course, but you don't have to follow any model or pattern to have SoC, since it can be (and should be) present in any piece of code.

Why can't you unit test any of your Java objects with Mockito right now?

I suggest putting all of the core code into a desktop project with a gradle build file. Put the android specific stuff into android studio.

This makes unit testing the core code very fast and easy.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM