0

I recently asked this question on Stack Overflow What role an android Service should play in the MVP pattern? and it was suggested to me that I use another class that the Presenter will have a reference to called Controller. Its purpose is to control the behavior of a Service (e.g. Start or Stop it).

Of course, in order to be able to do that it needed Context and I am passing it (injecting it with Dagger2) as a parameter in the constructor. My question is, interns of testing, how I will be able to test such a class later on in the development?

halfer
  • 19,824
  • 17
  • 99
  • 186
Georgi Koemdzhiev
  • 11,421
  • 18
  • 62
  • 126
  • MVP without a view ??? Intresting – Arun C Jan 28 '17 at 08:41
  • So you want to unit test it right? – Arun C Jan 28 '17 at 08:42
  • @TCA Well, my app is not comprised only of this one activity and this service :D I was just wondering how my Service will fit in the MVP :) – Georgi Koemdzhiev Jan 28 '17 at 08:42
  • @TCA Yes. At this stage (design) I am wondering how at a later point I will be able to test it, Yes :) – Georgi Koemdzhiev Jan 28 '17 at 08:43
  • If you want to generate context in your unit test. Robolectric http://robolectric.org/ – Arun C Jan 28 '17 at 08:45
  • I don't think it is good idea to write unit tests after writing a lot of code. It will be better if you follow TDD if you wants good unit test coverage . – Arun C Jan 28 '17 at 08:47
  • @TCA wonderful! Haven't used it but I guess I will have to give it a try :) Thank you so much! Would you add your answer so I can accept it :) One question, what do you think about this idea of having a "Controller" class that controls my Service. And Presenter having a reference to the controller? Just curious about your opinion :) – Georgi Koemdzhiev Jan 28 '17 at 08:50
  • @TCA TDD is a good suggestion. I will definitely consider that. Thank you! :) – Georgi Koemdzhiev Jan 28 '17 at 08:50
  • 1
    Forgive me to interrupt between your conversations. About _Controller in Presenter_, it's probably an inevitable choice if you make it in _must put things one of MVP in the name of architecture_ manner. – Youngjae Jan 28 '17 at 08:55
  • @Youngjae Thank you for your comment. Just to confirm. Are you suggesting renaming the "Controller" to "Presenter" just for the sake of the MVP pattern? If so, I totally agree with you, I just wanted a different name since it is not a typical Presenter (contains android specific logic) :p – Georgi Koemdzhiev Jan 28 '17 at 10:51
  • 1
    @GeorgiKoemdzhiev // I mean, just define your architecture to MVP-B(background jobs) or whatever you can. Because, your `Service` already not about _Presenting_ as you said. In fact, many proposed Android architectures are not separated nicely due to heavy dependency on `Context` and messy lifecycle management. I agree with what TCA mentioned at answer comment. use it only if its applicable :) too much digging on architecture will kill you. – Youngjae Jan 28 '17 at 11:01
  • @Youngjae Hey, thanks again! I see what you mean. I will consider adjusting the MVP pattern to my needs. – Georgi Koemdzhiev Jan 28 '17 at 17:06

1 Answers1

1

In order to write unit test for components related to android frame work use Roboelectric.

Arun C
  • 9,035
  • 2
  • 28
  • 42
  • Thanks again for the suggestion! :) – Georgi Koemdzhiev Jan 28 '17 at 08:53
  • 1
    Also one more thing to keep in mind that Controller and Presenter are almost the same stuff. MVP MVC :) The key is to avoid dependency of your business logic from framework. Also MVP is just a pattern. Make sure to use it only if its applicable. – Arun C Jan 28 '17 at 08:56