It's hard for Android to do cell testing.-part3

Source: Internet
Author: User
<span id="Label3"></p> <blockquote> <blockquote> <ul> <li>Original Link: How to make our ANDROID APPS UNIT testable (PT. 1)</li> <li>Original Author: Matthew Dupree</li> <li>Development technology Front www.devtf.cn</li> <li>Translator: chaossss</li> <li>Reviewer: Tiiime</li> <li>Status: Complete</li> </ul> </blockquote> </blockquote><p><p>Unit testing is very difficult in Android Applications. Sometimes it's not even possible. In the previous two blog posts, I've explained to you why it's so difficult to do a unit test on ANDROID. And the last one we've come to the conclusion of this analysis: it is the application architecture that Google officially advocates that makes unit testing in Android a disaster. Because of the officially advocated architecture, Google seems to want us to put the business logic in the Application's component class (for example: Activity,fragment. Service, etc... )。 This development is also a development template that we have been using for a long time.</p></p><p><p>In this blog Post. I've listed several ways to architect Android apps, and using these methods for development can make it easier for the unit to Test. But as I said in the preface, the best thing I've ever admired is the blog post from Square: square: abandon fragment from Today. The common method used in The. Because this approach was developed by the Android Developer project in Square. So I'm going to call it "Square dafa" in the next blog Post.</p></p><p><p></p></p><p><p>The core idea of Square Dafa is to remove all the business logic from the application component class (for example: Activity,fragment,service. Wait a minute...... )。 and transfer business logic to business objects, which are all pure Java objects that are injected by dependency. and Android-agnostic interfaces in this android-specific Implementation. Let's say that we use Square Dafa when we develop our applications, which is a lot easier to do with a unit Test.</p></p><p><p>In this blog post, i'll explain how Square Dafa can help us refactor UI-agnostic application Components (like The one we discussed in previous blog Post) and make it much easier to unit test it.</p></p>Refactoring UI-agnostic application components with Square Dafa<p><p>Using Square Dafa to refactor a UI-agnostic application component like Service,contentprovider,broadcastreceiver is relatively easy.</p></p><p><p>Let me repeat what we are going to do: remove the business logic in these Classes. And put them in the business Object.</p></p><p><p></p></p><p><p>Since "business logic" is a very easy and ambiguous term, let me explain what it means when I use the word "business logic". When I refer to the "business logic", its meaning is consistent with the interpretation on Wikipedia: The program is based on Real-world rules used to determine how the data will be created, displayed, stored and changed in that part of the Code. So now that we can agree on the meaning of the word "business logic," let's see what Square Dafa is.</p></p><p><p>Let's take a look at how to use Square Dafa to implement the Sessioncalendarservice I introduced in my previous blog post, with detailed code such as the Following:</p></p><pre class="prettyprint"><code class="language-java hljs "><span class="hljs-javadoc"><span class="hljs-javadoc">/** * Background {@link android.app.Service} that adds or removes session Calendar events through * the {@link Calenda rcontract} API available in Android 4.0 or Above. */</span></span><span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-class"><span class="hljs-class"> <span class="hljs-keyword">class</span> <span class="hljs-title">sessioncalendarservice</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">intentservice</span> {</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Private</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Static</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Final</span></span>String TAG = Makelogtag (sessioncalendarservice.class);<span class="hljs-comment"><span class="hljs-comment">//...</span></span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-title"><span class="hljs-title">Sessioncalendarservice</span></span>() {<span class="hljs-keyword"><span class="hljs-keyword">Super</span></span>(TAG); }<span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword">protected</span></span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">onhandleintent</span></span>(Intent Intent) {<span class="hljs-keyword"><span class="hljs-keyword">Final</span></span>String action = intent.getaction (); LOG.D (TAG,<span class="hljs-string"><span class="hljs-string">"Received intent:"</span></span>+ action);<span class="hljs-keyword"><span class="hljs-keyword">Final</span></span>Contentresolver resolver = Getcontentresolver ();<span class="hljs-keyword"><span class="hljs-keyword">Boolean</span></span>Isaddevent =<span class="hljs-keyword"><span class="hljs-keyword">false</span></span>;<span class="hljs-keyword"><span class="hljs-keyword">if</span></span>(action_add_session_calendar.equals (ACTION)) {isaddevent =<span class="hljs-keyword"><span class="hljs-keyword">true</span></span>; }<span class="hljs-keyword"><span class="hljs-keyword">Else</span></span> <span class="hljs-keyword"><span class="hljs-keyword">if</span></span>(action_remove_session_calendar.equals (ACTION)) {isaddevent =<span class="hljs-keyword"><span class="hljs-keyword">false</span></span>; }<span class="hljs-keyword"><span class="hljs-keyword">Else</span></span> <span class="hljs-keyword"><span class="hljs-keyword">if</span></span>(action_update_all_sessions_calendar.equals (ACTION) && Prefutils.shouldsynccalendar (<span class="hljs-keyword"><span class="hljs-keyword"></span> this</span>)) {<span class="hljs-keyword"><span class="hljs-keyword">Try</span></span>{getcontentresolver (). Applybatch (calendarcontract.authority, Processallsessionscale Ndar (resolver, Getcalendarid (intent))); Sendbroadcast (<span class="hljs-keyword"><span class="hljs-keyword">New</span></span>Intent (sessioncalendarservice.action_update_all_sessions_calendar_completed)); }<span class="hljs-keyword"><span class="hljs-keyword">Catch</span></span>(remoteexception E) {LOGE (TAG,<span class="hljs-string"><span class="hljs-string">"Error Adding all sessions to Google Calendar"</span></span>, e); }<span class="hljs-keyword"><span class="hljs-keyword">Catch</span></span>(operationapplicationexception E) {LOGE (TAG,<span class="hljs-string"><span class="hljs-string">"Error Adding all sessions to Google Calendar"</span></span>, e); } }<span class="hljs-keyword"><span class="hljs-keyword">Else</span></span> <span class="hljs-keyword"><span class="hljs-keyword">if</span></span>(action_clear_all_sessions_calendar.equals (ACTION)) {<span class="hljs-keyword"><span class="hljs-keyword">Try</span></span>{getcontentresolver (). Applybatch (calendarcontract.authority, processclearallsession s (resolver, Getcalendarid (intent))); }<span class="hljs-keyword"><span class="hljs-keyword">Catch</span></span>(remoteexception E) {LOGE (TAG,<span class="hljs-string"><span class="hljs-string">"Error clearing all sessions from Google Calendar"</span></span>, e); }<span class="hljs-keyword"><span class="hljs-keyword">Catch</span></span>(operationapplicationexception E) {LOGE (TAG,<span class="hljs-string"><span class="hljs-string">"Error clearing all sessions from Google Calendar"</span></span>, e); } }<span class="hljs-keyword"><span class="hljs-keyword">Else</span></span>{<span class="hljs-keyword"><span class="hljs-keyword">return</span></span>; }<span class="hljs-keyword"><span class="hljs-keyword">Final</span></span>Uri uri = Intent.getdata ();<span class="hljs-keyword"><span class="hljs-keyword">Final</span></span>Bundle Extras = Intent.getextras ();<span class="hljs-keyword"><span class="hljs-keyword">if</span></span>(uri = =<span class="hljs-keyword"><span class="hljs-keyword">NULL</span></span>|| Extras = =<span class="hljs-keyword"><span class="hljs-keyword">NULL</span></span>|| ! Prefutils.shouldsynccalendar (<span class="hljs-keyword"><span class="hljs-keyword"></span> this</span>)) {<span class="hljs-keyword"><span class="hljs-keyword">return</span></span>; }<span class="hljs-keyword"><span class="hljs-keyword">Try</span></span>{resolver.applybatch (calendarcontract.authority, Processsessioncalendar (resolver, getcalend Arid (intent), isaddevent, uri, extras.getlong (extra_session_start), Extras.getlong (extra_session_end), extras.getstring (extra_session_title), Extras.getstring (extra_session_room)); }<span class="hljs-keyword"><span class="hljs-keyword">Catch</span></span>(remoteexception E) {LOGE (TAG,<span class="hljs-string"><span class="hljs-string">"Error Adding session to Google Calendar"</span></span>, e); }<span class="hljs-keyword"><span class="hljs-keyword">Catch</span></span>(operationapplicationexception E) {LOGE (TAG,<span class="hljs-string"><span class="hljs-string">"Error Adding session to Google Calendar"</span></span>, e); } }<span class="hljs-comment"><span class="hljs-comment">//...</span></span>}</code></pre><p><p>As you can see, Sessioncalendarservice invokes the helper method that will be defined Later. Once we take these helper methods and the field declarations of the classes into account as Well. The Service class has more than 400 lines of Code.</p></p><p><p>It's not easy to hold on to the business logic that's happening in such a huge class, and it's as we saw in the previous article blog Post. It is impossible to do a unit test in Sessioncalendarservice.</p></p><p><p></p></p><p><p>Now let's see what it would be like to use Square Dafa to implement its Code. Let me emphasize again: Square Dafa requires that we migrate the business logic within the Android class into a business Object.</p></p><p><p>here, the corresponding business object of Sessioncalendarservice is Sessioncalendarupdater. Detailed code such as the Following:</p></p><pre class="prettyprint"><code class="language-java hljs "><span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-class"><span class="hljs-class"> <span class="hljs-keyword">class</span> <span class="hljs-title">sessioncalendarupdater</span> {</span></span> <span class="hljs-comment"><span class="hljs-comment">//...</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Private</span></span>Sessioncalendardatabase msessioncalendardatabase;<span class="hljs-keyword"><span class="hljs-keyword">Private</span></span>Sessioncalendaruserpreferences msessioncalendaruserpreferences;<span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-title"><span class="hljs-title">Sessioncalendarupdater</span></span>(sessioncalendardatabase sessioncalendardatabase, sessioncalendaruserpreferences SessionC Alendaruserpreferences) {msessioncalendardatabase = sessioncalendardatabase; Msessioncalendaruserpreferences = sessioncalendaruserpreferences; }<span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">Updatecalendar</span></span>(calendarupdaterequest Calendarupdaterequest) {<span class="hljs-keyword"><span class="hljs-keyword">Boolean</span></span>Isaddevent =<span class="hljs-keyword"><span class="hljs-keyword">false</span></span>; String action = calendarupdaterequest.getaction ();<span class="hljs-keyword"><span class="hljs-keyword">Long</span></span>CalendarID = Calendarupdaterequest.getcalendarid ();<span class="hljs-keyword"><span class="hljs-keyword">if</span></span>(action_add_session_calendar.equals (ACTION)) {isaddevent =<span class="hljs-keyword"><span class="hljs-keyword">true</span></span>; }<span class="hljs-keyword"><span class="hljs-keyword">Else</span></span> <span class="hljs-keyword"><span class="hljs-keyword">if</span></span>(action_remove_session_calendar.equals (ACTION)) {isaddevent =<span class="hljs-keyword"><span class="hljs-keyword">false</span></span>; }<span class="hljs-keyword"><span class="hljs-keyword">Else</span></span> <span class="hljs-keyword"><span class="hljs-keyword">if</span></span>(action_update_all_sessions_calendar.equals (ACTION) && Msessioncalendaruserpreferences.shouldsyncca Lendar ()) {<span class="hljs-keyword"><span class="hljs-keyword">Try</span></span>{msessioncalendardatabase.updateallsessions (calendarid); }<span class="hljs-keyword"><span class="hljs-keyword">Catch</span></span>(remoteexception | Operationapplicationexception E) {LOGE (TAG,<span class="hljs-string"><span class="hljs-string">"Error Adding all sessions to Google Calendar"</span></span>, e); } }<span class="hljs-keyword"><span class="hljs-keyword">Else</span></span> <span class="hljs-keyword"><span class="hljs-keyword">if</span></span>(action_clear_all_sessions_calendar.equals (ACTION)) {<span class="hljs-keyword"><span class="hljs-keyword">Try</span></span>{msessioncalendardatabase.clearallsessions (calendarid); }<span class="hljs-keyword"><span class="hljs-keyword">Catch</span></span>(remoteexception | Operationapplicationexception E) {LOGE (TAG,<span class="hljs-string"><span class="hljs-string">"Error clearing all sessions from Google Calendar"</span></span>, e); } }<span class="hljs-keyword"><span class="hljs-keyword">Else</span></span>{<span class="hljs-keyword"><span class="hljs-keyword">return</span></span>; }<span class="hljs-keyword"><span class="hljs-keyword">if</span></span>(!shouldupdatecalendarsession (calendarupdaterequest, MSESSIONCALENDARUSERPREFERENCES)) {<span class="hljs-keyword"><span class="hljs-keyword">return</span></span>; }<span class="hljs-keyword"><span class="hljs-keyword">Try</span></span>{calendarsession calendarsessiontoupdate = calendarupdaterequest.getcalendarsessiontoupdate ();<span class="hljs-keyword"><span class="hljs-keyword">if</span></span>(isaddevent) {msessioncalendardatabase.addcalendarsession (calendarid, calendarsessiontoupdate); }<span class="hljs-keyword"><span class="hljs-keyword">Else</span></span>{msessioncalendardatabase.removecalendarsession (calendarid, calendarsessiontoupdate); } }<span class="hljs-keyword"><span class="hljs-keyword">Catch</span></span>(remoteexception | Operationapplicationexception E) {LOGE (TAG,<span class="hljs-string"><span class="hljs-string">"Error Adding session to Google Calendar"</span></span>, e); } }<span class="hljs-keyword"><span class="hljs-keyword">Private</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Boolean</span></span> <span class="hljs-title"><span class="hljs-title">shouldupdatecalendarsession</span></span>(calendarupdaterequest calendarupdaterequest, Sessioncalendaruserpreferenc Es sessioncalendaruserpreferences) {<span class="hljs-keyword"><span class="hljs-keyword">return</span></span>Calendarupdaterequest.getcalendarsessiontoupdate () = =<span class="hljs-keyword"><span class="hljs-keyword">NULL</span></span>|| !sessioncalendaruserpreferences.shouldsynccalendar (); }}</code></pre><p><p>I would like to highlight some of the key points: first of all, it needs Attention. We don't have to use any new keywords at all, and because the business Object's dependencies are injected, it doesn't use the new keyword at all, which is the key to making the class Unit Test. Secondly. You will notice that the class does not depend exactly on the Android SDK, because the business object relies on android-agnostic implementations of Android-independent Interfaces. Therefore it does not need to rely on the Android SDK.</p></p><p><p></p></p><p><p>So how are these dependencies added to the Sessioncalendarupdater class? is injected through the Sessioncalendarservice class:</p></p><pre class="prettyprint"><code class="language-java hljs "><span class="hljs-javadoc"><span class="hljs-javadoc">/** * Background {@link android.app.Service} that adds or removes session Calendar events through * the {@link Calenda rcontract} API available in Android 4.0 or Above. */</span></span><span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-class"><span class="hljs-class"> <span class="hljs-keyword">class</span> <span class="hljs-title">sessioncalendarservice</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">intentservice</span> {</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Private</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Static</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Final</span></span>String TAG = Makelogtag (sessioncalendarservice.class);<span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-title"><span class="hljs-title">Sessioncalendarservice</span></span>() {<span class="hljs-keyword"><span class="hljs-keyword">Super</span></span>(TAG); }<span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword">protected</span></span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">onhandleintent</span></span>(Intent Intent) {<span class="hljs-keyword"><span class="hljs-keyword">Final</span></span>String action = intent.getaction (); LOG.D (TAG,<span class="hljs-string"><span class="hljs-string">"Received intent:"</span></span>+ action);<span class="hljs-keyword"><span class="hljs-keyword">Final</span></span>Contentresolver resolver = Getcontentresolver (); Broadcaster BROADCASTER =<span class="hljs-keyword"><span class="hljs-keyword">New</span></span>Androidbroadcaster (<span class="hljs-keyword"><span class="hljs-keyword"></span> this</span>); Sessioncalendardatabase sessioncalendardatabase =<span class="hljs-keyword"><span class="hljs-keyword">New</span></span>Androidsessioncalendardatabase (resolver, broadcaster); Sharedpreferences defaultsharedpreferences = preferencemanager.getdefaultsharedpreferences (<span class="hljs-keyword"><span class="hljs-keyword"></span> this</span>); Sessioncalendaruserpreferences sessioncalendaruserpreferences =<span class="hljs-keyword"><span class="hljs-keyword">New</span></span>Androidsessioncalendaruserpreferences (defaultsharedpreferences); Sessioncalendarupdater Sessioncalendarupdater =<span class="hljs-keyword"><span class="hljs-keyword">New</span></span>Sessioncalendarupdater (sessioncalendardatabase, sessioncal endaruserpreferences); Accountnamerepository accountnamerepository =<span class="hljs-keyword"><span class="hljs-keyword">New</span></span>Androidaccountnamerepository (intent,<span class="hljs-keyword"><span class="hljs-keyword"></span> this</span>); String AccountName = Accountnamerepository.getaccountname ();<span class="hljs-keyword"><span class="hljs-keyword">Long</span></span>CalendarID = Sessioncalendardatabase.getcalendarid (accountname); Calendarsession calendarsessiontoupdate = calendarsession.fromintent (intent); Calendarupdaterequest calendarupdaterequest =<span class="hljs-keyword"><span class="hljs-keyword">New</span></span>Calendarupdaterequest (action, calendarid, calendarsessiontoupdate); Sessioncalendarupdater.updatecalendar (calendarupdaterequest); }}</code></pre><p><p>It is worth noting that the changed sessioncalendarservice are everywhere new KEYWORDS. however, These keywords do not cause any problems in the class. Let's say we take a few seconds to skim the main point to make this clear: no business logic is in the Sessioncalendarservice class, so the Sessioncalendarservice class no longer needs to be unit Tested. Just to make sure that the Updatecalendar () method in the Sessioncalendarupdater class is called in sessioncalendarservice, the Sessioncalendarservice The only thing that can happen is a compile-time error.</p></p><p><p>We have absolutely no need to implement a test unit for this Purpose. Because this is the work of the Compiler. Nothing to do with Us.</p></p><p><p>Because of the reasons I mentioned in the top two posts, splitting our service class into such a way makes it very easy to unit test the business logic, such as the code of our unit test for the Sessioncalendarupdater class can be written as Follows:</p></p><pre class="prettyprint"><code class="language-java hljs "><span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-class"><span class="hljs-class"> <span class="hljs-keyword">class</span> <span class="hljs-title">sessioncalendarupdatertests</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">TestCase</span> {</span></span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">testshouldclearallsessions</span></span>()<span class="hljs-keyword"><span class="hljs-keyword">throws</span></span>remoteexception, operationapplicationexception {sessioncalendardatabase sessioncalendardatabase = mock (SessionCale ndardatabase.class); Sessioncalendaruserpreferences sessioncalendaruserpreferences = mock (sessioncalendaruserpreferences.class); Sessioncalendarupdater Sessioncalendarupdater =<span class="hljs-keyword"><span class="hljs-keyword">New</span></span>Sessioncalendarupdater (sessioncalendardatabase, sessioncalendaruserpreferences); Calendarupdaterequest calendarupdaterequest =<span class="hljs-keyword"><span class="hljs-keyword">New</span></span>Calendarupdaterequest (sessioncalendarupdater.action_clear_all_sessions_calendar,<span class="hljs-number"><span class="hljs-number">0</span></span>,<span class="hljs-keyword"><span class="hljs-keyword">NULL</span></span>); Sessioncalendarupdater.updatecalendar (calendarupdaterequest); Verify (sessioncalendardatabase). clearallsessions (<span class="hljs-number"><span class="hljs-number">0</span></span>); }}</code></pre>Conclusion<p><p>In order to be able to test the Unit. I think the changed code becomes easier to read and easier to Maintain. To be sure, there are a number of ways we can make the code even Better. But in the process of allowing the code to do a unit test, I wanted to make the changed code similar to the Pre-change style as much as possible, so I didn't make any other changes. In the next blog post, I will teach you how to refactor the UI components of your application using Square Dafa (e.g., Fragment and Activity).</p></p><p><p></p></p> <p><p>It's hard for Android to do cell testing.-part3</p></p></span>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.