<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Fábio on Medium]]></title>
        <description><![CDATA[Stories by Fábio on Medium]]></description>
        <link>https://medium.com/@fabiocbl?source=rss-625fee40caa9------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*61B6K9vTITWDG_Qs6jqtVg.png</url>
            <title>Stories by Fábio on Medium</title>
            <link>https://medium.com/@fabiocbl?source=rss-625fee40caa9------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 16 Nov 2026 05:37:03 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@fabiocbl/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Launching Dark Mode at N26]]></title>
            <link>https://medium.com/insiden26/launching-dark-mode-at-n26-469f665f0c63?source=rss-625fee40caa9------2</link>
            <guid isPermaLink="false">https://medium.com/p/469f665f0c63</guid>
            <category><![CDATA[product-design]]></category>
            <category><![CDATA[online-banking]]></category>
            <category><![CDATA[dark-mode]]></category>
            <category><![CDATA[technology]]></category>
            <category><![CDATA[n26]]></category>
            <dc:creator><![CDATA[Fábio]]></dc:creator>
            <pubDate>Wed, 15 Jul 2020 12:03:44 GMT</pubDate>
            <atom:updated>2020-07-15T12:11:16.493Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*CJao2EzHhwX1NCjb" /></figure><p>Dark Mode was in N26 mind for quite some time. However, when we first started working on it we just didn’t have the needed framework to make it happen quickly. Both the Android and iOS apps had no reliable design framework which allowed us to have that flexibility. Since then, we have been taking small steps every week, building a powerful design system and spreading its adoption through the app. At the beginning of this year, we were finally in a good position. All we needed was a small stretch. And then Get Dark Mode Done Days (GDMDD) happened: 3 days where more than 50 persons joined forces to get Dark Mode ready to release.</p><h3>MOBILE</h3><p>On the tech front, the “Dark plan” was delivered through a joint effort of 24 Android and iOS engineers, as well as some other engineers from Backend and SRE. The work started a few weeks before the GDMDD — with people from all the teams estimating the work needed and noting potential issues we could face with some legacy flows. Then, during the GDMDD, we decided to split the technical tasks into several groups, so that each of them had enough freedom to work on their topics without clashing on each other. At the same time, we had a smaller group of persons just focused on unblocking others, guaranteeing alignment, fixing any bugs, or unpredicted requests that popped out during those 3 days. The final result? More than 120 Pull Requests related to the initiative, a ready-to-ship app, and a lot of fun and hard work by everyone!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*dVO5ABn4aGTr9zzd" /></figure><h3>DESIGN</h3><p>Like the Engineering teams, the design team started a few weeks before. The first design mission was to check the status quo. To help us with that, our engineering teammates prepared a test build and we reviewed which screens are not design system compliant. This also meant that those screens were not dark mode ready. Non-compliant screens were one of our main focus during GDMDD, so we noted them down on a big Figma board, alongside with other observations.</p><p>At the same time, we created a few dark mode variations for most-used screens and tried to cover almost all components. The winning version was the one that felt the most aligned with the N26 brand (when the lights go out) and dark mode best practices. Since 50 people needed to propagate and implement the final design, very clear guidelines had to be made. Colors, icons, typography, graphics, button states, and other elements needed to have a dark mode preview, while Figma file organisation had to enable people to know where the source of truth is and what is their playing area.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*U3URFV2quvSNDcf4" /></figure><p>Together with PM Alex, we’ve set the stage for 3 intensive days (almost like a festival). We used Figma as our main remote board alongside Confluence, Jira and Hangouts. The Figma file had a page for guidelines called “Ready for development” and for each main section of the app, we also prepared one page. The team was organised so that each person worked on the part of the app they know the best (or based on the skill they are the best, e.g. iconography).</p><p>The Design team organised one touchpoint the week before. In that meeting, we talked about file organisation, design guidelines, and what is our strategy for GDMDD. During these three days, we had Dark Mode — Design Room on Hangouts to be able to announce changes, ask questions, align on open topics, or to work together remotely. The key ingredients for success were a great team, motivation, good task delegation, file structure, and design system.</p><h3>SOFTWARE QUALITY</h3><p>During GDMDD we were all extremely active. As proof, you can see a clear peak in the chart below. At N26, we foster the <a href="https://medium.com/insiden26/from-gatekeepers-of-quality-to-enabling-teams-software-quality-engineering-at-n26-2ac0c7aca7c4">quality</a> culture in every team, thus in this project, we were all testing. We managed to raise all together 71 issues related to the Dark Mode, 43 of which were reported by the QE team volunteers.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*tY1SEO23FYFZT94X" /></figure><p>The Dark Mode initiative was then one of the most successful bootstrap projects boosted by our GDMDD days. It was amazing to see how more than persons were able to quickly organise and move towards a shared goal, with a high-focus on just focusing on delivering user value.</p><h4>Follow us on <a href="https://www.linkedin.com/company/n26/">LinkedIn</a>, <a href="https://www.instagram.com/peopleofn26/">Instagram</a> and <a href="https://twitter.com/N26Careers">Twitter</a> to keep in touch with us and learn more about Life at N26!</h4><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=469f665f0c63" width="1" height="1" alt=""><hr><p><a href="https://medium.com/insiden26/launching-dark-mode-at-n26-469f665f0c63">Launching Dark Mode at N26</a> was originally published in <a href="https://medium.com/insiden26">InsideN26</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Testing Espresso Matchers]]></title>
            <link>https://proandroiddev.com/testing-espresso-matchers-1c3c587d7d39?source=rss-625fee40caa9------2</link>
            <guid isPermaLink="false">https://medium.com/p/1c3c587d7d39</guid>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[androiddev]]></category>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[android-app-development]]></category>
            <dc:creator><![CDATA[Fábio]]></dc:creator>
            <pubDate>Tue, 23 Jun 2020 12:48:12 GMT</pubDate>
            <atom:updated>2020-06-23T12:48:12.057Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OV875k3KK_Rl25redZ0_3Q.png" /></figure><p>If you are using instrumentation tests in your Android app, it is likely that you are using Espresso— allowing you to define expectations about your app screens and how to interact with them.</p><p>During your day-by-day as a developer <strong>you</strong> <strong>will eventually implement some custom views</strong>. However, Espresso won’t have the ability to assert the specific behaviors of your custom view out of the box. To make it possible you will have to create your own Espresso matchers. In this article<strong> I want to show you how to create a simple Matcher and how to run tests for it in the JVM.</strong></p><p>The example we will follow is the implementation of a custom Button that has a loading state. I will break it down into 3 steps: naive implementation of the LoadingButton , creation of an appropriate Matcher and the test for that Matcher . At the end I will just showcase a simple usage of our new <strong>tested Matcher.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/758/1*hmzXjMlju1UbLyAQhhDukw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/750/1*J-e8WJlOKyvEr8PI759clQ.png" /><figcaption>The `LoadingButton` in non-loading and loading states</figcaption></figure><ol><li><strong>Creating the </strong><strong>LoadingButton</strong></li></ol><p>For the sake of simplicity I’m not going to focus on the implementation of the LoadingButton. The only relevant part is that we would have a setter/getter for the <strong>loading state.</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/aa0c23c520f24e6433c5f30ffad316bb/href">https://medium.com/media/aa0c23c520f24e6433c5f30ffad316bb/href</a></iframe><p><strong>2. Creating the </strong><strong>WithButtonLoading Matcher</strong></p><p>When creating a custom Matcher for a custom property of your View you should extend a BoundedMatcher&lt;T, S extends T&gt; . When extending it you will have to override two methods:</p><ul><li>describeTo(description: Description) — here you should add a relevant message to description so that you can understand better when this matcher assertion fails.</li><li>matchesSafely(item: S): Boolean — you should return a Boolean stating whether or not the given item respects your logic.</li></ul><p>In our example we would do as follows:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ca9e41feff86c52efe2c7b3a849883fb/href">https://medium.com/media/ca9e41feff86c52efe2c7b3a849883fb/href</a></iframe><p>In this case we would use describeTo to describe our component (a button in a loading state) and the matchesSafely to assert that the given item is in the desired loading state.</p><p>An important point is that I actually made this class internal as this exposes an implementation detail. It is an observed good practice on Espresso matchers to have public wrappers of the internal matchers that provide friendlier/more readable APIs.</p><p>We would then define a public ButtonMatchers that provides methods to create matchers that check if the button is loading or not loading.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bcb9a952c4f2d18480c3c88adab5518b/href">https://medium.com/media/bcb9a952c4f2d18480c3c88adab5518b/href</a></iframe><p><strong>3. Creating the Test</strong></p><p>In order to test this matcher in with a fast approach and with no need to run instrumentation tests, we will leverage FragmentScenario which are able to run on top of JVM. This will create reliable and fast tests when compared with the instrumentation tests.</p><p>The idea is that we will use FragmentScenario + Roboelectrict to launch a Fragment that inflates our LoadingButton , so that we can interact with it and perform assertions regarding the Matcher behavior.</p><p>Below you can see that we are testing the behavior of ButtonMatchers.isLoading matcher: if the Matcher behaves correctly when the button is loading and not loading.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e8ebb62d810f3fd9ff5dd78a3560b930/href">https://medium.com/media/e8ebb62d810f3fd9ff5dd78a3560b930/href</a></iframe><p>The magic happens on launchViewInFragment , which takes a function that creates the View that will be inflated in the Fragment .</p><p>The internals of launchViewInFragment make use of FragmentScenario to setup and launch the test.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8f3fd1d541c596490443e8f404692938/href">https://medium.com/media/8f3fd1d541c596490443e8f404692938/href</a></iframe><p>If we then run our tests and, the output on Android Studio will be the following:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*W4GduIHfWZDaYVph8811bQ.png" /></figure><p>You can see that it has a initial overheard time spent, but after that the tests run really fast. And the more tests you have the less the overhead will be noticeable in the full test run time.</p><p>To finalize I just wanted to briefly show how we would use our new Matchers in an instrumentation test. In this example, we assume the button loading state is toggled everytime you click it. You can see the matchers being used on the assertButtonIsLoading and assertButtonIsNotLoading .</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c975dd51cb512d7b72b984865c538919/href">https://medium.com/media/c975dd51cb512d7b72b984865c538919/href</a></iframe><p>You can find a <strong>sample project describing this example </strong><a href="https://github.com/fabiocarballo/matchers-sample"><strong>here</strong></a><strong>.</strong></p><p><strong>References:</strong></p><ul><li><a href="https://developer.android.com/training/testing/espresso">Espresso</a></li><li><a href="https://developer.android.com/training/basics/fragments/testing">FragmentScenario</a></li><li><a href="https://medium.com/@thiagolopessilva/creating-custom-viewmatcher-for-espresso-75dde62dd173">Creating a CustomViewMatcher</a> by <a href="https://medium.com/u/a90dde4db206">Thiago Lopes Silva</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1c3c587d7d39" width="1" height="1" alt=""><hr><p><a href="https://proandroiddev.com/testing-espresso-matchers-1c3c587d7d39">Testing Espresso Matchers</a> was originally published in <a href="https://proandroiddev.com/">ProAndroidDev</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Testing your first Android lint rule]]></title>
            <link>https://proandroiddev.com/testing-your-first-android-lint-rule-bb78c1f2d2bd?source=rss-625fee40caa9------2</link>
            <guid isPermaLink="false">https://medium.com/p/bb78c1f2d2bd</guid>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[androiddev]]></category>
            <category><![CDATA[kotlin]]></category>
            <dc:creator><![CDATA[Fábio]]></dc:creator>
            <pubDate>Fri, 14 Feb 2020 10:40:12 GMT</pubDate>
            <atom:updated>2020-02-14T11:08:54.122Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/820/1*iQZUD6WlsWi1mGzTYODxxQ.jpeg" /></figure><p>In the previous article in this series on making use of custom Lint rules, we covered what is Lint and what we have to do to create a simple custom Lint rule. If you haven’t read it yet, I would <strong>highly recommend </strong>that you take a look to get some more context:</p><p><a href="https://proandroiddev.com/implementing-your-first-android-lint-rule-6e572383b292">Implementing your first Android lint rule</a></p><p>In this article, we will see what we have to do to perform <strong>unit tests to our lint rule</strong>. This will ensure non-flaky lint rules, avoiding false positives in the CI pipeline.</p><h4>Context</h4><p>In the previous article, we saw how to create a rule that would detect usages of the android.util.Log and would flag this as an error (since we “had decided” that we had an AmazingLog that we should use instead).</p><p>It is important to remember that our lint rule is defined in the rules module</p><h3>Getting ready to test the rules</h3><p>With that said, the first thing we need to do is to add a specific lint testing dependency:</p><pre>dependencies {<br>  // these ones for creating the rules <br> compileOnly &quot;com.android.tools.lint:lint-api:lint-version&quot;<br> compileOnly &quot;com.android.tools.lint:lint-checks:lint-version&quot;</pre><pre>// this one for testing<br> testImplementation &quot;com.android.tools.lint:lint-tests:lint-version&quot;</pre><pre>... // junit4 or junit5<br>}</pre><h3>Using lint-tests</h3><p>With the testing dependency defined, we now have access to specific tools that will allow us to easily test our rule.</p><p>The first thing we have to do is to create a test which extends LintDetectorTest . This class mainly gives you methods to define the Issue that you want to use in the tests, helpers to create the files that you will use to verify if the rules are working, and ways to create a fake project file structure.</p><p>For the sake of simplicity we will do the bare minimum:</p><ul><li>Define our detector as AndroidLogDetector</li><li>Define the issue we want to check as AndroidLogDetector.ISSUE</li></ul><p>Now, we can focus on writing our test in small steps. This test should detect if we are using the “forbidden” android.util.Log</p><p>When thinking of Lint tests you should have the following structure in mind:</p><ol><li>Define the “stub” file that on which we will try to detect the presence/absence of the Issue</li><li>Run the lint checks on that stub file.</li><li>Verify expectations in terms of the lint check result.</li></ol><p>Let’s go over each one of these.</p><h4>Defining the stub file</h4><p>First, we need to define which is the type of file that we want to use. In this case, it would be either appropriate a Java or Kotlin file. We will go with a Kotlin one.</p><p>You can easily create a file is using the TestFile#kotlin method. This method takes a string that should contain the source code you want to test your Issue on.</p><p>For example, we could write:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/712/1*SCuFfFO1PXiJh6F_apCHiQ.png" /><figcaption>Your stub source code correctly highlighted in the IDE!</figcaption></figure><p>The amazing benefit of this API is that you can have some structured, readable stub file and to which the IDE correctly highlights as if it was actual code.</p><h4>Producing the lint check results</h4><p>Now we just need to run lint on that stub file, so that later we can inspect the produced results. For that, we use the TestLintTask#lint method in this way:</p><pre>val lintResult = lint()<br>    .files(<em>ANDROID_LOG_IMPL_JAVA</em>, stubFile)<br>    .run()</pre><p>We pass the needed files for the execution and run the inspection!</p><p><strong>Important: </strong>We should note that that we not only passed our created stubFile but also a ANDROID_LOG_IMPL_JAVA file. This is crucial so that our tests work correctly (for this specific case) since it defines a stub for the android.util.Log class, which Lint currently doesn’t have access.</p><p>To define it, we follow the same idea as above, but this time using TestFile#java .</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/926/1*PK2xBUZu6-kJEPXzYbJ3OQ.png" /></figure><p><strong>Tip: </strong>Another really neat feature is that if your code does not compile you will see that listed in the IDE. In the example below, you can see the error highlight on the package declaration as we didn’t put the ending ;</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/890/1*u5euOJ1bOkfqTPQ3KByJzQ.png" /><figcaption>The IDE detects wrong code</figcaption></figure><h4>Inspecting the results</h4><p>Finally, it is time for us to validate our expectations using the LintResult. Some simple assertions cover most of the cases.</p><p><strong>Assertions on the number of issues</strong></p><ul><li>expectWarningCount(count) — when we expect count warnings to be found</li><li>expectErrorCount(count) — when we expect count errors to be found</li><li>expectCount(expectedNumberOfIssues, severity) — when we expect N amount ofissues with a specific severity</li><li>expectClean() — when we expect no issues to be found</li></ul><p><strong>Assertions on the result message:</strong></p><ul><li>expectMatches(regex) — expects that the given regex finds anything in the resulting output</li><li>expect(expectedText) — to assert that the given message corresponds to the resulting output.</li></ul><p>For our case, this would be enough:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f13099a501a3f9d78572cd72af7fd2bf/href">https://medium.com/media/f13099a501a3f9d78572cd72af7fd2bf/href</a></iframe><p>With this, we would end up with the following test class:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2b7e187c8ee7607e8601fcfe4aef1594/href">https://medium.com/media/2b7e187c8ee7607e8601fcfe4aef1594/href</a></iframe><p>To run the tests you just have to run:</p><pre>./gradlew rules:test</pre><p>The sample project that this post is based on, is available <a href="https://github.com/fabiocarballo/lint-sample">here</a>.</p><h3>What’s next?</h3><p>In the following post I will go over topics:</p><ul><li>How to create automatic correction suggestions (as seen in the IDE)</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bb78c1f2d2bd" width="1" height="1" alt=""><hr><p><a href="https://proandroiddev.com/testing-your-first-android-lint-rule-bb78c1f2d2bd">Testing your first Android lint rule</a> was originally published in <a href="https://proandroiddev.com/">ProAndroidDev</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Implementing your first Android lint rule]]></title>
            <link>https://proandroiddev.com/implementing-your-first-android-lint-rule-6e572383b292?source=rss-625fee40caa9------2</link>
            <guid isPermaLink="false">https://medium.com/p/6e572383b292</guid>
            <category><![CDATA[static-analysis]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[androiddev]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Fábio]]></dc:creator>
            <pubDate>Thu, 02 Jan 2020 14:44:40 GMT</pubDate>
            <atom:updated>2020-02-14T10:42:28.906Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*dcurRH0rtO7XPFHP3hVD-g.png" /></figure><p>By the end of this article you should be able to write your first Android lint rule. But first …</p><h3>What is lint?</h3><p>Lint is a static analyser, which has the goal of finding bugs on our source code without the need of compiling or running it.</p><p>And even if you are not totally sure what I am talking about, you have definitely seen it in action during your Android development work. Are this type of messages familiar to you?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/582/1*47pqWvv0N-O3IcwTeKhVcA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/880/1*E59eAnM36wNkwQYvqgx0Qg.png" /></figure><p>If so, you saw <strong>Lint in Action!</strong></p><p>We can then understand the power of these rules, and how they can help developers to quickly identify and fix bugs in a systematic way.</p><p>However, the default set of lint rules is limited and in some situations we could benefit from creating custom rules that identify issues that are specific to our projects. We will now go over the process of creating and integrating a <strong>Lint</strong> rule.</p><h3>Creating your rules module</h3><p>We start off by defining a separate Java/Kotlin module where our rules will be declared. This module is the rules module. We then go to the rules/build.gradle and add the following dependencies:</p><pre>dependencies {<br> compileOnly &quot;com.android.tools.lint:lint-api:lint-version&quot;<br> compileOnly &quot;com.android.tools.lint:lint-checks:lint-version&quot;<br>}</pre><p><strong>Important</strong>: For historical reasons, your lint version should correspond to the Android Gradle Plugin version + 23. This is, if your AGP is 3.5.1, then your lint version should be 26.5.1</p><p>In order to integrate our custom rules with the app module, we then add the following to our app/build.gradle:</p><pre>dependencies {<br>       ...<br>       lintChecks project(path: &#39;:rules&#39;)<br>}</pre><p>This way, the lint rules existing in the rules module will be compiled into the final lint.jar that your app uses.</p><h3>Issues and Detectors</h3><p>After the initial setup, we can now look into how to actually write custom Lint rules. For that we need to understand two fundamental concepts: Issues and Detectors.</p><h3>What is an Issue?</h3><p>As per the documentation, an Issue <strong>is a potential bug in an Android application.</strong> It is the way you declare the bug your rule should tackle.</p><p>An issue has the following base structure:</p><ul><li>id — in order to identify uniquely this issue</li><li>briefDescription — summarised description of the issue</li><li>explanation — should be a more in-depth description of the issue and ideally on how it should be solved</li><li>category — identifies the type of the issue. There are a lot of possible categories such as CORRECTNESS, USABILITY, I18N, COMPLIANCE, PERFORMANCE, ...</li><li>priority — a number between 1 and 10, where the bigger the number the more serious the issue is</li><li>severity — it can be one of the following values: FATAL, ERROR, WARNING, INFORMATIONAL and IGNORE . <strong>Note:</strong> If the severity is either FATAL or ERROR then running lint will fail and you will have to address the issue.</li><li>implementation — the class responsible for analysing files and detecting the issue</li></ul><h3>What is a Detector?</h3><p>A Detector is a class that is able to find one/more specific issues. Depending on the Issue, we can use different types of Detector in order to provide better interfaces to different file types:</p><ul><li>SourceCodeScanner - a detector specialised in Java/Kotlin source files</li><li>XmlScanner - a detector specialised in XML files</li><li>GradleScanner - a detector specialised in Gradle files</li><li>ResourceFolderScanner - a detector specialised in resource folders (not the files that it contains)</li></ul><h3>Putting it into practice!</h3><p>Our first example will be a really simple one. The goal is to create a rule that detects usages of android.util.Log and replace them by the “amazing” logger com.fabiocarballo.lint.AmazingLog .</p><p>For that let’s first define the AndroidLogDetector :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/83ff011c977013c4cf1d56f1c9fa643b/href">https://medium.com/media/83ff011c977013c4cf1d56f1c9fa643b/href</a></iframe><p>Regarding the class signature:</p><ul><li>It extends Detector so that it can be used by Android Lint to detect an issue</li><li>It extends SourceCodeScanner since we need to inspect both Kotlin and Java files.</li></ul><p>Regarding the body of the class:</p><ul><li>getApplicableMethodNames - filters only the method signatures that exist in android.util.Log</li><li>visitMethodCall - uses the evaluator to guarantee that the method is being called by the android.util.Log and not by any other class. For example, the AmazingLog has the exact same methods and shouldn&#39;t be flagged.</li><li>reportUsage - used to report an issue when found.</li></ul><p>With the detector defined we are just left to define the Issue . We will do that inside a companion object in the AndroidLogDetector .</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b3c93d9bdc1b07f6e6361582a7f2e72a/href">https://medium.com/media/b3c93d9bdc1b07f6e6361582a7f2e72a/href</a></iframe><h3>Issue Registry</h3><p>Since we have created our first Issue and corresponding Detector, then we have to make it visible to the Android Lint engine. The way we do it is via a IssueRegistry, which has the sole responsibility of defining the issues and their detectors.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e2ba0ea5b16528bd8923f7860cde6beb/href">https://medium.com/media/e2ba0ea5b16528bd8923f7860cde6beb/href</a></iframe><p><strong>However</strong>, in order to Android Lint to discover our IssueRegistry we have to declare a <em>Service Locator</em>. This <em>Service Locator</em> is defined in a specific location in our resources folder. To define it, we create this file entry:</p><p><strong>rules/src/main/resources/META-INF/services/com/android/tools/lint/client/api/IssueRegistry</strong></p><pre>com.fabiocarballo.rules.IssueRegistry</pre><h3>Running the rules</h3><p>Finally, all the boxes needed to run our lint rule are checked:</p><ul><li>[x] Issue and Detector (AndroidLogDetector and AndroidLogDetector.ISSUE)</li><li>[x] Issue registry declares the issues</li><li>[x] Service locator is pointing to our IssueRegistry implementation</li><li>[x] app consumes the lint rules in rules through lintCheck</li></ul><p>We can now detect errors such as this one:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/318b4fd1f35e54b0e33f3d3d0a8a9fbc/href">https://medium.com/media/318b4fd1f35e54b0e33f3d3d0a8a9fbc/href</a></iframe><p>As we can see, this class is using the <strong>forbidden </strong>android.util.Log . Now, we just need to run: ./gradlew app:lintDebug</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XMAJLkshkSC6HJhjguFuYg.png" /><figcaption>Output of app:lintDebug</figcaption></figure><p>You can see which is the <strong>issue</strong> found and its <strong>description</strong>. You also have the option to check in more detail either in the generated XML/html report.</p><p>Moreover, this issue is also visible in Android Studio as shown below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*b-zOhP2zadlIeeAsep28zg.png" /></figure><p><strong>P.s: </strong>You can access the sample code at this <a href="https://github.com/fabiocarballo/lint-sample">repository</a>.</p><h3>What’s next</h3><p>In the following posts I will go over more topics:</p><p><a href="https://medium.com/@fabiocarballo/testing-your-first-android-lint-rule-bb78c1f2d2bd">Testing your first Android lint rule</a></p><ul><li>How to create automatic correction suggestions (as seen in the IDE)</li></ul><h3>Resources</h3><p>These are useful resources I have found during my Lint learnings:</p><p><strong>Documentation:</strong></p><ul><li><a href="https://developer.android.com/studio/write/lint">Lint</a></li></ul><p><strong>Talks:</strong></p><ul><li><a href="https://www.youtube.com/watch?v=ffH-LD5uP4s">Getting the Most Out of Android Lint (Android Dev Summit’ 18)</a></li><li><a href="https://www.youtube.com/watch?v=jCmJWOkjbM0">Coding in Style: Static Analysis with Custom Lint Rules (Android Dev Summit ’19)</a></li></ul><p><strong>Repositories:</strong></p><ul><li><a href="https://github.com/tikurahul/lint-experiments">@tikurahul Lint Experiments</a></li><li><a href="https://github.com/vanniktech/lint-rules">@vanniktech lint rules</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6e572383b292" width="1" height="1" alt=""><hr><p><a href="https://proandroiddev.com/implementing-your-first-android-lint-rule-6e572383b292">Implementing your first Android lint rule</a> was originally published in <a href="https://proandroiddev.com/">ProAndroidDev</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Improve your tests with Kotlin in Android — (Pt. 3)]]></title>
            <link>https://proandroiddev.com/improve-your-tests-with-kotlin-in-android-pt-3-104e1c9f77cc?source=rss-625fee40caa9------2</link>
            <guid isPermaLink="false">https://medium.com/p/104e1c9f77cc</guid>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[androiddev]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[testing]]></category>
            <dc:creator><![CDATA[Fábio]]></dc:creator>
            <pubDate>Tue, 24 Oct 2017 15:06:32 GMT</pubDate>
            <atom:updated>2017-11-05T20:57:52.817Z</atom:updated>
            <content:encoded><![CDATA[<p>Hey there! Here we are to the third part of this series (check <a href="https://proandroiddev.com/improve-your-tests-with-kotlin-in-android-pt-1-6d0b04017e80">part 1</a> and <a href="https://proandroiddev.com/improve-your-tests-with-kotlin-in-android-pt-2-f3594e5e7bfd">part 2</a> first if you want to). This time we will look into how we can improve our test assertions using one of two libraries: <a href="https://github.com/MarkusAmshove/Kluent"><strong>Kluent</strong></a> and <a href="https://github.com/winterbe/expekt"><strong>Expekt</strong></a>, that help you to write assertions in a natural language as fluent english sentences.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*YK5PLgDKciZ0J66Ilvb9wQ.png" /></figure><p>This time, let’s take a look into a simple <strong>User</strong> class, with basic info such as name, age, friends and whether or not it can legally drink.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0a7b5048c4520ea4468d8ec6bcba0f4c/href">https://medium.com/media/0a7b5048c4520ea4468d8ec6bcba0f4c/href</a></iframe><p>Now let’s take a look into a standard Kotlin test for the logic behind the fullName value:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3d55fdd7898c82882f6d96a26cda929c/href">https://medium.com/media/3d55fdd7898c82882f6d96a26cda929c/href</a></iframe><p>If you notice, we are using the standard assertEquals to compare the expected value with the actual value. While this works, my opinion is that the tests should <strong>always</strong> be the most readable possible. This way, if we use any of the fluent assertion libraries, either <strong>Kluent</strong> or <strong>Expekt</strong>, we can get a test that looks more elegant.</p><p>For instance, if we start of with <strong>Kluent</strong> we can use the power of Kotlin and infix functions to achieve this assertion:</p><p>user.fullName `should equal` &quot;Fábio Carballo&quot;</p><p>As you can see, this approach follows the natural english speech, becoming easier to read.</p><p><strong>Breaking down the source code.</strong></p><p>This method has a fairly simple implementation. Let’s take a look at the source code:</p><pre>infix fun Any.`should equal`(theOther: Any) = assertEquals(theOther, this)</pre><p>What is happening here? A <strong>`should equal`extension function</strong> is being declared. Kotlin has this power to provide new functionality to a function without using inheritance or other decoration patterns.</p><p>So, by prefixing the <strong>should equal</strong> method on the receiver type <strong>Any</strong> (think about Object in Java), we are making it possible to call this method to any object in Kotlin.</p><p>But how can we call this method without using any parentheses or dots?Because it’s possible to use the <strong>infix notation</strong> (usually used in logical formulae and statements) with this type of methods.</p><p>This <strong>infix notation</strong> can be applied on functions where:</p><ul><li>They are member functions or <a href="https://kotlinlang.org/docs/reference/extensions.html">extension functions</a>;</li><li>They have a single parameter;</li><li>They are marked with the infix keyword.</li></ul><p>In fact, according to the Kotlin documentation, we indeed could get rid of the infix keyword and just declare it as an extension function:</p><pre>fun Any.`should equal`(theOther: Any) = assertEquals(theOther, this)</pre><p><strong>Back to the power of Kluent and Expekt</strong></p><p>If we were to write the same test using Expekt, it would be like this:</p><pre>user.fullName.<em>should</em>.equal(&quot;Fábio Carballo&quot;)</pre><p>As you can see, it is also more readable. However, the author opted for another style of tests which follow the dot notation (which allowed him to provide some extra behaviour, that we will still talk about in a bit).</p><p>With Expekt, you also have the option to describe the test in terms of the expectations such as:</p><pre><em>expect</em>(user.fullName).to.equal(&quot;Fábio Carballo&quot;)</pre><p><strong>Collection Assertions</strong></p><p>But this is not all … Both libraries also provide collection assertions. Let’s see how we would write with regular assertions.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/65e01cff3790bfcb11976b1ced39d74d/href">https://medium.com/media/65e01cff3790bfcb11976b1ced39d74d/href</a></iframe><p>We have some options, but neither of them seems the right way to go honestly. Let’s see how we could do it with both libraries:</p><p>With Kluent with would be like this:</p><pre>rui.friends <em>`should contain` </em>fabio</pre><p>While with Expekt we could achieve by adding the dots and parentheses:</p><pre>rui.friends.<em>should</em>.contain(fabio)</pre><p>However, as I said above, Expekt has more power and we could be more precise on our assertions:</p><pre>fabio.friends.<em>should</em>.have.all.elements(rui).and.have.size(1)</pre><p>This is just a glimpse of what Expekt can do. Please go to their <a href="https://github.com/winterbe/expekt">github</a> and see more examples if you got curious.</p><p><strong>Exception Assertions</strong></p><p>Let’s introduce exception assertions by adding a simple <strong>drink </strong>method to our User class. The user can only drink if age is ≥ 18, if not we throw an <strong>UnderageDrinkingException.</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/92381b643300a8d1908c606fc3407859/href">https://medium.com/media/92381b643300a8d1908c606fc3407859/href</a></iframe><p>If we use <strong>JUnit</strong> assertions one of our options would be to annotate the expected exception. (See the code below).</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/878f1aee574f58e46724724fe912f5f2/href">https://medium.com/media/878f1aee574f58e46724724fe912f5f2/href</a></iframe><p>By using Kluent, we could get rid of the annotation processing and declare our expectation right in the test method.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/23cd7ba32d600b11f3612af9d331b7db/href">https://medium.com/media/23cd7ba32d600b11f3612af9d331b7db/href</a></iframe><p>Once again, we would follow the same style, applying the <strong>should throw </strong>function with the infix notation.</p><p>You could argue that having to wrap the action that triggers the exception inside a lambda is not very cool, and in fact, I agree with that. Although, I think is better than having to declare the expected exception in an annotation. But well, I think it comes to personal taste now.</p><p>Kluent also enables you to be more precise and test if the exception is thrown with a specific message:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/54931f9e1bdb443a0b1174a65c6ebff8/href">https://medium.com/media/54931f9e1bdb443a0b1174a65c6ebff8/href</a></iframe><p>So when it comes to assertions, Kluent is the winner (since Expekt doesn’t have any behavior to approach this).</p><p>Well, that was it for now. Stay tuned to part 4 :)</p><p>Thank you once again for all the feedback and support.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fupscri.be%2Ff20c9a%3Fas_embed%3Dtrue&amp;dntp=1&amp;url=https%3A%2F%2Fupscri.be%2Ff20c9a%2F&amp;image=https%3A%2F%2Fe.enpose.co%2F%3Fkey%3DdRXnS9Gplk%26w%3D700%26h%3D425%26url%3Dhttps%253A%252F%252Fupscri.be%252Ff20c9a%252F%253Fenpose&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=upscri" width="800" height="400" frameborder="0" scrolling="no"><a href="https://medium.com/media/87c27c6bd6c8dea21c9726d9564b27d6/href">https://medium.com/media/87c27c6bd6c8dea21c9726d9564b27d6/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=104e1c9f77cc" width="1" height="1" alt=""><hr><p><a href="https://proandroiddev.com/improve-your-tests-with-kotlin-in-android-pt-3-104e1c9f77cc">Improve your tests with Kotlin in Android — (Pt. 3)</a> was originally published in <a href="https://proandroiddev.com/">ProAndroidDev</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Improve your tests with Kotlin in Android — (Pt.2)]]></title>
            <link>https://proandroiddev.com/improve-your-tests-with-kotlin-in-android-pt-2-f3594e5e7bfd?source=rss-625fee40caa9------2</link>
            <guid isPermaLink="false">https://medium.com/p/f3594e5e7bfd</guid>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[androiddev]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[kotlin]]></category>
            <dc:creator><![CDATA[Fábio]]></dc:creator>
            <pubDate>Mon, 09 Oct 2017 12:45:44 GMT</pubDate>
            <atom:updated>2017-11-19T21:46:26.060Z</atom:updated>
            <content:encoded><![CDATA[<p>Hello! This is the second part of my series on how to use Kotlin to improve your tests on Android development. In the <a href="https://proandroiddev.com/improve-your-tests-with-kotlin-in-android-pt-1-6d0b04017e80">first part</a>, I briefly went through on what are the problems when starting testing using Kotlin and what are the options to overcome them.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0S2V9Vt6xRBpPkV4Y3zj9g.jpeg" /></figure><p>This time, I will go through a simple example with the help of the Kotlin magic, as well as the essential <a href="https://github.com/nhaarman/mockito-kotlin"><strong>mockito-kotlin</strong></a>, a Kotlin wrapper around Mockito.</p><p>Let’s start off with a simple example of a regular <strong>TemperaturePresenter</strong> class:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/970234ebc03cf5a69f968cd21492d200/href">https://medium.com/media/970234ebc03cf5a69f968cd21492d200/href</a></iframe><p>In the code above, we have a simple <strong>TemperaturePresenter</strong> that could be easily seen in an Android application, whose responsibility is to fetch and display the temperature in Celsius degrees kfor the current location.</p><p>However, in this class we have the particular behaviour of <strong>first trying to display a temperature to the user with the last known location</strong>, which we can get quickly,<strong> and only then try to fetch the current location</strong> (which can take some seconds) and display the corresponding temperature.</p><p>One possible test in the traditional Java-style Mockito would be this one:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dcf1194428b97711aaf8cb89602707e8/href">https://medium.com/media/dcf1194428b97711aaf8cb89602707e8/href</a></iframe><p>So, let’s go step by step on transforming this into a more readable and light test.</p><p><strong>Convert to Kotlin and use its quick-wins.</strong></p><p>This is an easy one. Kotlin by definition is way less verbose than Java, and just by having to type and read less you get a better life :)</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5069a7293645107ba36cdda252d17b9e/href">https://medium.com/media/5069a7293645107ba36cdda252d17b9e/href</a></iframe><p>However, in this particular test you don’t get that much besides getting rid of some “<strong>;</strong>”. But we still have some work to do.</p><p>By converting to Kotlin, you gain the ability to make your test names more readable. If you notice, right now our test name is:</p><pre>fun <strong>testInexactTemperatureIsDisplayedFollowedByExactTemperature</strong>()</pre><p>This is just a bunch of letters all glued ones onto the others. With Kotlin you can use the backticks just to describe your test names with “regular” writing:</p><pre>fun `<strong>display temperature for last known location first, and then for exact location</strong>`()</pre><p>With this you can see right away what is the goal of your test and don’t need to mingle the code to understand what is going on.</p><p>Another place where we can improve our code with the power of Kotlin is in the usage of the <strong>inOrder.</strong></p><p>We can make it mode readable and context related with the usage of the <strong>apply </strong>method. With it, you can avoid referencing to the inOrder variable every time you want to do a verification.</p><pre>Mockito.inOrder(temperatureProvider, view).<em>apply </em><strong>{<br>      </strong>verify(temperatureProvider).getCelsiusTemperatureAt(lastLocation)<br>verify(view).displayTemperature(18f)<br>   verify(temperatureProvider).getCelsiusTemperatureAt(currentLocation)<br>verify(view).displayTemperature(21f)<br><strong>}</strong></pre><p>Let’s stop and take a look at our current test:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/25b6f4509e86514270b6607a2c3d0480/href">https://medium.com/media/25b6f4509e86514270b6607a2c3d0480/href</a></iframe><p><strong>Leverage the power of mockito-kotlin</strong></p><p>So, <strong>mockito-kotlin</strong> is a Kotlin wrapper library around the popular Java Framework Mockito: <em>“It provides top-level functions to allow for a more idiomatic approach while using Mockito in Kotlin.”</em></p><p>In this test, we can use it to improve a couple of things. If you notice, we have to annotate every dependency with the `<strong>@Mock</strong>` and then in the method that runs before every test (the <strong>setUp</strong>) we would have to call a <strong>Mockito</strong> method to setup the dependencies classes that are annotated.</p><p>One way to improve it is to use <strong>mockito-kotlin </strong>to use an inline declaration of the mock, like this:</p><pre>val temperatureProvider: TemperatureProvider = <strong><em>mock</em>()</strong></pre><p>This code, allows you to:</p><ul><li>Remove of the ‘<strong>@Mock</strong>’ annotation and the call to <strong>MockitoAnnotations.initMocks(this)</strong></li><li>Remove the <strong>lateinit</strong> modifiers and make the variables final (immutability, I’m checking you out).</li><li>We also removed the <strong>setUp</strong> method, since that now the mocked objects variables are initialized right away, which allows us to initialize the <strong>TemperaturePresenter</strong> object with the variable declaration.</li></ul><p>Behind the scenes, this method is just wrapping the call to the parameterized method <strong>Mockito.&lt;T&gt;mock(T object)</strong></p><p>Other thing that we could do is to use the <strong>mockito-kotlin </strong>wrapper to define the mock expectations. Once again it is really simple, and we could just use:</p><pre><strong>whenever</strong>(temperatureProvider.getCelsiusTemperatureAt(lastLocation))<br>        .thenReturn(18f)</pre><p>It is a really small change, but it makes it a bit more readable (IMHO).</p><p>If for instance, you have a mocking expectation that you need to set in every test you can declare it in a idiomatic way when you declare the mock as well. So, to achieve it in our small example we would do it like this:</p><pre>private val lastLocation = Coordinate(41.5, 8.9)<br>private val currentLocation = Coordinate(38.7, 9.7)<br><br>val locationProvider: LocationProvider = <strong><em>mock</em></strong><em> </em><strong>{<br>    on</strong> <strong>{ </strong>getLastKnownLocation() <strong>}</strong>.<strong>then</strong> <strong>{ </strong>lastLocation <strong>}<br>    on</strong> <strong>{ </strong>getExactLocation() <strong>}</strong>.<strong>then</strong> <strong>{ </strong>currentLocation <strong>}<br>}</strong></pre><p>Can you see the Kotlin magic working here? :) What really happens is that this library is leveraging the use of high-order functions.</p><p>Internally, the wrapper works out so that the <strong>on</strong> method (for our example) can be seen like this:</p><pre>fun &lt;R&gt; on(methodCall: <strong>LocationProvider</strong>.() -&gt; R): OnGoingStubbing&lt;R&gt;</pre><p>You can notice the <strong>methodCall </strong>is in fact a method of <strong>LocationProvider</strong> that returns a generic type. This is the reason why we can call the <strong>getLastKnownLocation()</strong> method directly without referring to our locationProvider variable. Kotlin infers all the types and does its magic :)</p><p>To get a better idea of it the <strong>on </strong>method would break down in the following: (this can be seen in the <strong>mockito-kotlin</strong> source code):</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6174e9757bebcbdb3214458318cc0575/href">https://medium.com/media/6174e9757bebcbdb3214458318cc0575/href</a></iframe><p>Finally, let’s see how our Kotlin test ended up with some small changes:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d52d92353b2d85ff64a2272f1de29d19/href">https://medium.com/media/d52d92353b2d85ff64a2272f1de29d19/href</a></iframe><p>This is just a small glimpse of what you can do to improve the tests by using Kotlin and<strong> mockito-kotlin</strong>. I would advise you to look up on the source code and see how the magic happens.</p><p>Thanks for reading until the end :)</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fupscri.be%2Ff20c9a%3Fas_embed%3Dtrue&amp;dntp=1&amp;url=https%3A%2F%2Fupscri.be%2Ff20c9a%2F&amp;image=https%3A%2F%2Fe.enpose.co%2F%3Fkey%3DdRXnS9Gplk%26w%3D700%26h%3D425%26url%3Dhttps%253A%252F%252Fupscri.be%252Ff20c9a%252F%253Fenpose&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=upscri" width="800" height="400" frameborder="0" scrolling="no"><a href="https://medium.com/media/87c27c6bd6c8dea21c9726d9564b27d6/href">https://medium.com/media/87c27c6bd6c8dea21c9726d9564b27d6/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f3594e5e7bfd" width="1" height="1" alt=""><hr><p><a href="https://proandroiddev.com/improve-your-tests-with-kotlin-in-android-pt-2-f3594e5e7bfd">Improve your tests with Kotlin in Android — (Pt.2)</a> was originally published in <a href="https://proandroiddev.com/">ProAndroidDev</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Improve your tests with Kotlin in Android — (Pt.1 )]]></title>
            <link>https://proandroiddev.com/improve-your-tests-with-kotlin-in-android-pt-1-6d0b04017e80?source=rss-625fee40caa9------2</link>
            <guid isPermaLink="false">https://medium.com/p/6d0b04017e80</guid>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[androiddev]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Fábio]]></dc:creator>
            <pubDate>Mon, 25 Sep 2017 10:22:36 GMT</pubDate>
            <atom:updated>2017-10-20T10:50:09.492Z</atom:updated>
            <content:encoded><![CDATA[<p>Hey there! Since I’ve started playing with Kotlin, I quickly fell in love with the language and soon understood how it would improve my productivity and readability of my code. Following that idea (and thanks to an awesome open-source community) I was able to improve the tests of my Android project using Kotlin.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rRBDobIFt9rpMLtab6Rbsw.jpeg" /></figure><p>This will be a series that will overlook on how to start testing your Java/Kotlin code using Kotlin and which are the must-have libraries and framework to do it.</p><p>When I was doing my first unit tests in Kotlin, I started off by adding the needed dependencies:</p><ul><li><strong>Mockito</strong>, the popular Java mocking library, so that I could mock my interactions and define the behaviors needed for my tests.</li></ul><pre>testCompile &#39;org.mockito:mockito-core:2.8.9&#39;</pre><ul><li><strong>Kotlin Junit Assertions, </strong>which is the standard Junit Kotlin library, providing us with the needed assertion methods.</li></ul><pre>testCompile org.jetbrains.kotlin:kotlin-test-junit:${kotlinVersion}</pre><p>I followed the old principle of isolating the class under test and mock the interaction with any of the dependencies. However, I quickly found out that there were some issues to overcome when integrating it with Kotlin.</p><p>For instance, if I have this class:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b8de388653b7c248a365911e5848fbd7/href">https://medium.com/media/b8de388653b7c248a365911e5848fbd7/href</a></iframe><p>And the following test:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/083ef4c259f0cdf8d02eba0659ec66c7/href">https://medium.com/media/083ef4c259f0cdf8d02eba0659ec66c7/href</a></iframe><p>We will be facing the first error, which is:</p><pre>org.mockito.exceptions.base.MockitoException: <br>Cannot mock/spy class testing.fabiocarballo.com.myapplication.User<br>Mockito cannot mock/spy because :<br> - final class</pre><p><strong>Mockito 1 is only able to mock interactions of <em>non-final</em> classes and methods, whereas Kotlin has <em>final</em> methods and classes as default.</strong></p><p>To avoid this you have some options:</p><p><strong>Make your class-under-test dependencies to be interfaces.</strong> In Java the interface’s methods are not <em>final </em>because they have to be overriden when the interface is implemented by a concrete class. That way, Mockito will be able to mock the interactions with that interface.</p><p><strong>Make your Kotlin classes and methods non-final by “<em>opening” </em>them.</strong> As I said above, by design Kotlin makes its classes final by default. However, it doesn’t limit the classes to that behavior and you can make them non-final by adding the openkeyword to the class and methods that you want to mock. However, I must say that changing the source code for the sake of testing is not the best practice at all.</p><p>In this scenario our User class would look like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4118f262e2412a72e175bfa798f57fce/href">https://medium.com/media/4118f262e2412a72e175bfa798f57fce/href</a></iframe><p><strong>Use</strong><a href="https://github.com/dpreussler/kotlin-testrunner"><strong> Kotlin Test Runner</strong></a><strong>.</strong> This runner by <a href="https://medium.com/@dpreussler">Danny Preussler</a> will do the magic work of removing the final modifiers of classes and methods during the test in runtime. Read more about it by the creator <a href="https://medium.com/@dpreussler/never-say-final-mocking-kotlin-classes-in-unit-tests-314d275b82b1">here</a>.</p><p>You can accomplish this just by adding the proper test runner in your test class.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cf390df86aafa4c58a28b91cf0c4ddc1/href">https://medium.com/media/cf390df86aafa4c58a28b91cf0c4ddc1/href</a></iframe><p><strong>Tweak Mockito 2.X and mock everything. </strong>If you use the newest version of Mockito (2 and above) you are able to mock final classes and methods just by creating the file named org.mockito.plugins.MockMaker under the resources/mockito-extensionsdirectory in your test folder, with only a single line of content that is: mock-maker-inline</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/842/1*LnrfeE4AtpGit7DMWS1H0g.png" /><figcaption>The structure you should follow to enable mocking of final classes in Kotlin</figcaption></figure><p><strong>Mockito matchers rely on null-values as the return values as in </strong><strong>any()or </strong><strong>anyObject(), whereas Kotlin was designed to avoid null types.</strong></p><p>Regarding this problem, you can do your own workarounds, but I really advise everyone to <a href="https://github.com/nhaarman/mockito-kotlin">mockito-kotlin</a> which solves this problem and offers a lot more functionality with it.</p><p>To make your tests work you just have to replace this code:</p><pre><a href="http://twitter.com/Test">@Test</a><br>fun englishGreetIsCorrect() {<br>  <strong>Mockito.`when`</strong>(user.fullName()).thenReturn(&quot;Fábio Carballo&quot;)</pre><pre>  assertEquals(<br>       &quot;Hello, Fábio Carballo!&quot;, <br>       tested.getEnglishGreeting())<br>}</pre><p>By this:</p><pre>@Test<br>fun englishGreetIsCorrect() {<br>    <strong><em>whenever</em></strong>(user.fullName()).thenReturn(&quot;Fábio Carballo&quot;)<br><br>    assertEquals(<br>         &quot;Hello, Fábio Carballo!&quot;, <br>         tested.getEnglishGreeting())<br>}</pre><p>As you can see, it follows the same idiomatic flow and becomes easier to read.</p><p>However, this is just one of the thing that <a href="https://github.com/nhaarman/mockito-kotlin">mockito-kotlin</a> improves .. On the next story, I will further explore this library and how it improved the quality of my tests.</p><p>If you want to, check the <a href="https://proandroiddev.com/improve-your-tests-with-kotlin-in-android-pt-2-f3594e5e7bfd">part 2</a> as well. Thanks! 😄</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6d0b04017e80" width="1" height="1" alt=""><hr><p><a href="https://proandroiddev.com/improve-your-tests-with-kotlin-in-android-pt-1-6d0b04017e80">Improve your tests with Kotlin in Android — (Pt.1 )</a> was originally published in <a href="https://proandroiddev.com/">ProAndroidDev</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Know when your slow Gradle build finishes.]]></title>
            <link>https://medium.com/@fabiocbl/know-when-your-slow-gradle-build-finishes-fd4a72d7f167?source=rss-625fee40caa9------2</link>
            <guid isPermaLink="false">https://medium.com/p/fd4a72d7f167</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[gradle]]></category>
            <dc:creator><![CDATA[Fábio]]></dc:creator>
            <pubDate>Thu, 07 Sep 2017 13:21:20 GMT</pubDate>
            <atom:updated>2017-09-07T13:21:20.162Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/413/1*J-1MC3QGbIuwq4tb-yr-iA.png" /><figcaption>XCKD view on overcoming slow compilations.</figcaption></figure><p>Hello there! If you are an Android developer in a medium-large project you probably know the pain when it comes to wait for your slow build to compile.</p><p>At that time you probably go check something in the internet (Twitter, Medium, you name it), get lost in it, and when you notice your build had already finished 5 minutes ago.</p><p>To overcome this, I added a small tweak to my assemble command:</p><p>./gradlew app:assembleSlowBuild &amp;&amp; say build finished</p><p>By using the <strong>say</strong> command, you will hear a robotic voice coming out of your computer/headphones saying the message defined by you.</p><p>And there you go, no more wasted time spent on the internet because you thought your build was still running.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fd4a72d7f167" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Hide your crashes gracefully (and still report them)]]></title>
            <link>https://proandroiddev.com/hide-your-crashes-gracefully-and-still-report-them-9b1c85b25875?source=rss-625fee40caa9------2</link>
            <guid isPermaLink="false">https://medium.com/p/9b1c85b25875</guid>
            <category><![CDATA[crashlytics]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[mobile-app-development]]></category>
            <category><![CDATA[crash]]></category>
            <dc:creator><![CDATA[Fábio]]></dc:creator>
            <pubDate>Thu, 31 Aug 2017 17:06:15 GMT</pubDate>
            <atom:updated>2020-01-02T14:18:48.121Z</atom:updated>
            <content:encoded><![CDATA[<p>A couple of months ago at <a href="https://play.google.com/store/apps/details?id=com.hole19golf.hole19.beta&amp;hl=en">Hole19</a>, we dedicated some time to improve the overall experience of the users in our app. Since our app is meant to be used by golfers, any crash that happens during their round of golf should be considered very serious.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/194/1*Bkl1fLvhYZJ2IS6PMVNwAg.jpeg" /><figcaption>Our motto ❤</figcaption></figure><p>Unfortunately, we had recently encountered a very mysterious crash in our courses images screen which affected some particular group of users. This, as we know, confronts them with that hideous system crash dialog; something that breaks the user experience and hinders their progress. Since we’d still yet to discover the cause, we thought that we could at least improve the users’ experience by detecting the crash and try to re-open the app gracefully in the same screen (this was greatly inspired by this <a href="https://www.google.pt/search?q=crash+fast+realm&amp;oq=crash+fast+realm&amp;aqs=chrome..69i57j69i60l2j0l3.2926j0j4&amp;sourceid=chrome&amp;ie=UTF-8">Pierre-Yves Ricau talk</a>).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/398/1*ZIjTbkjb8UpGi62d68W4Yw.png" /><figcaption>One of the default system crash dialogs in our “crashing” screen.</figcaption></figure><p>Our task had the following goals:</p><ul><li>Remove the system crash dialog</li><li>Gracefully recover from the crash by re-entering the screen where the crash happened; however, if the same crash happened a second time we allow the system to handle it by stopping the process and showing the dialog.</li><li>Log the crash to our crash reporting tool (in our case, it is <a href="http://www.fabric.io/">Fabric</a>)</li></ul><p>So, to start it off we first should handle this in our Application class with a method that sets up the crash handling:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f1ccaf41f119e2c0d66bfbcf831f4881/href">https://medium.com/media/f1ccaf41f119e2c0d66bfbcf831f4881/href</a></iframe><p>The idea here is that we should get access to both the android default exception handler and the Crashlytics handler so that we can use it in our custom exception handler. Bear in mind that it is crucial to set a dummy exception handler before setting up Crashlytics. Why? Because when the app crashes, Crashlytics reports it first and then let the system handle the error by calling the default exception handler. We still want Crashlytics to report the crash, but don’t want the system to handle it, so we need to override the default exception handler.</p><p>In our AppExceptionHandler , we start off by registering an ActivityLifecycleCallback so that we can keep track which Activity is in foreground (if any). This will be crucial to help us to decide on how to handle each crash.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7ccb49286cb0aaf2f3d1699f62ccc5b7/href">https://medium.com/media/7ccb49286cb0aaf2f3d1699f62ccc5b7/href</a></iframe><p>So, when a crash happens the uncaughtException method will be run. Here’s the logic:</p><p>First, we check if there is an activity in foreground or not. If there is no activity then the crash probably happened in a background component, as a service, or in the Application class. For that reason we have no other option than to log the exception to Crashlytics and then default to the Android exception handler.</p><p>If there is indeed an Activity in foreground there are three possible scenarios/outcomes:</p><ol><li>It is the first time that crash happened in the activity — We log the crash to our reporting tool and we restart the activity with a special <strong>RESTARTED</strong> flag (so that we can identify later if this activity was previously “gracefully” restarted or not), and with the <strong>LATEST_EXCEPTION</strong> storing the exception that originated the crash (so that we can compare new exception with it).</li><li>The <strong>RESTARTED </strong>flag is already set and the latest caught exception is of the same type of the new one; this way we assume that it is the second time that the crash happened in the activity (so we may be in a potential loop). When this happens our decision is to default to the Android exception handler (presenting the system crash dialog and killing the app).</li><li>The <strong>RESTARTED</strong> flag is already set, but the caught exception is different from the one that caused the first restart of the app. For that reason, we follow the same logic as in the scenario 1.</li></ol><p>Hopefully, this can help you in providing a better experience to your users; Reach me out if you think this can be improved (I’m sure it can 😅).</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9b1c85b25875" width="1" height="1" alt=""><hr><p><a href="https://proandroiddev.com/hide-your-crashes-gracefully-and-still-report-them-9b1c85b25875">Hide your crashes gracefully (and still report them)</a> was originally published in <a href="https://proandroiddev.com/">ProAndroidDev</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>