{"id":1208,"date":"2013-10-24T08:06:25","date_gmt":"2013-10-24T13:06:25","guid":{"rendered":"http:\/\/www.familytidings.com\/blog\/?p=1208"},"modified":"2013-10-24T08:07:34","modified_gmt":"2013-10-24T13:07:34","slug":"stubbing-the-application-object-in-android-junit-testing","status":"publish","type":"post","link":"https:\/\/www.familytidings.com\/blog\/?p=1208","title":{"rendered":"Stubbing the Application Object in Android JUnit Testing"},"content":{"rendered":"<p>I have an <a href=\"https:\/\/github.com\/zymurgeek\/TipOnDiscount\">Android app<\/a> that I wrote to demonstrate unit testing techniques applied to Android code. \u00c2\u00a0It has standalone JUnit tests for the code that is not dependent upon Android and Android JUnit tests for the code that is. \u00c2\u00a0Dependency injection is used wherever possible. \u00c2\u00a0Android code is harder to mock than other platforms, however, mostly because your Activity and Application are called by the OS and don&#8217;t support parameters in their constructors. \u00c2\u00a0My first tactic to inject dependencies involved passing flags in the Activities&#8217; Intents and having each Activity set up class factories with either the production classes or stubs, depending on the value of the flags. \u00c2\u00a0This proved cumbersome. \u00c2\u00a0For instance, if the factory has already been given an instantiation, say, from a different activity, should a request to set a new instantiation overwrite the old one? \u00c2\u00a0My next try moved the factory set-up to the Application. \u00c2\u00a0Since there is only one Application object per app, this solved the multiple Activity problem. \u00c2\u00a0However, creation of the Application is done by the OS and the Application lifetime might span the lifetime of multiple Activities or test runs. \u00c2\u00a0I was left with the same problem of determining whose factory initialization should be honored, either the Application&#8217;s or the Android JUnit Test Case&#8217;s. \u00c2\u00a0In the end, the answer was simple and I&#8217;m surprised I didn&#8217;t think of it sooner. \u00c2\u00a0All I needed was a link seam. \u00c2\u00a0In the Android JUnit test project, I declared an Application class with the same name and package as specified in the Android manifest. \u00c2\u00a0When the Android JUnit tests run, the stub Application is invoked and does nothing. \u00c2\u00a0Here, I depend on each JUnit test to set up the factories with the stubs they need. \u00c2\u00a0When the actual app runs, the real Application, that sets up the factories with production classes, is invoked.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have an Android app that I wrote to demonstrate unit testing techniques applied to Android code. \u00c2\u00a0It has standalone JUnit tests for the code that is not dependent upon Android and Android JUnit tests for the code that is. \u00c2\u00a0Dependency injection is used wherever possible. \u00c2\u00a0Android code is harder to mock than other platforms, [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1208","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.familytidings.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1208","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.familytidings.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.familytidings.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.familytidings.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.familytidings.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1208"}],"version-history":[{"count":3,"href":"https:\/\/www.familytidings.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1208\/revisions"}],"predecessor-version":[{"id":1211,"href":"https:\/\/www.familytidings.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1208\/revisions\/1211"}],"wp:attachment":[{"href":"https:\/\/www.familytidings.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1208"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.familytidings.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1208"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.familytidings.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1208"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}