<br />
<b>Warning</b>:  Declaration of Jetpack_IXR_Client::query() should be compatible with IXR_Client::query(...$args) in <b>/www/wwwroot/tech.aqarmap.com/wp-content/plugins/jetpack/vendor/automattic/jetpack-connection/legacy/class-jetpack-ixr-client.php</b> on line <b>54</b><br />
{"id":36,"date":"2019-08-08T06:57:45","date_gmt":"2019-08-08T06:57:45","guid":{"rendered":"https:\/\/tech.aqarmap.com\/?p=36"},"modified":"2019-11-11T07:59:25","modified_gmt":"2019-11-11T07:59:25","slug":"android-how-to-unit-test-part-1","status":"publish","type":"post","link":"https:\/\/tech.aqarmap.com\/?p=36","title":{"rendered":"Android: How to Unit Test? \u2014 Part 1"},"content":{"rendered":"\n<p>When you start developing Android apps your main target will always be delivering a working application. But when your application became bigger it also became harder to test. You will lose your confidence that the functionality you added didn\u2019t affect any other part.<\/p>\n\n\n\n<p>At this point you start to think if there is something that can help you be confident that any added new features doesn\u2019t break your app. The answer is&nbsp;<strong>Testing<\/strong>.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\"><p><em>Test code is just as important as production code. It is not a second-class citizen. It requires thought, design, and care. It must be kept as clean as production code.<br><\/em><strong><em>\u2013 Robert C. Martin, Clean Code<\/em><\/strong><\/p><\/blockquote>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"695\" src=\"https:\/\/tech.aqarmap.com\/wp-content\/uploads\/2019\/11\/1_f6sxzZfvFKHSiK0pZDKwiQ-1024x695.png\" alt=\"\" class=\"wp-image-37\" srcset=\"https:\/\/tech.aqarmap.com\/wp-content\/uploads\/2019\/11\/1_f6sxzZfvFKHSiK0pZDKwiQ-1024x695.png 1024w, https:\/\/tech.aqarmap.com\/wp-content\/uploads\/2019\/11\/1_f6sxzZfvFKHSiK0pZDKwiQ-300x204.png 300w, https:\/\/tech.aqarmap.com\/wp-content\/uploads\/2019\/11\/1_f6sxzZfvFKHSiK0pZDKwiQ-768x521.png 768w, https:\/\/tech.aqarmap.com\/wp-content\/uploads\/2019\/11\/1_f6sxzZfvFKHSiK0pZDKwiQ-1600x1086.png 1600w, https:\/\/tech.aqarmap.com\/wp-content\/uploads\/2019\/11\/1_f6sxzZfvFKHSiK0pZDKwiQ.png 2000w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\">Testing is divided into 3 categories:<\/h4>\n\n\n\n<p><strong>1. Unit tests:<\/strong><\/p>\n\n\n\n<ul><li>Small tests that validate your app\u2019s behavior by testing one class per time.<\/li><li>Testing of units of code.<\/li><li>Each unit test tests specific feature.<\/li><li>Requires JVM environment<\/li><\/ul>\n\n\n\n<p><strong>2. Integration tests:<\/strong><\/p>\n\n\n\n<ul><li>Medium tests that validate interactions between levels of the stack within a module, or interactions between related modules.<\/li><li>Testing of how pieces being put together<\/li><li>Tests application behavior<\/li><li>Often involves testing of UI<\/li><li>Requires Android environment (emulator\/physical device)<\/li><\/ul>\n\n\n\n<p><strong>3. UI tests:<\/strong><\/p>\n\n\n\n<ul><li>Large tests that validate user journeys spanning multiple modules of your app.<\/li><li>Slower and are less isolated so are not run as often and can be more flaky than integration and unit tests.<\/li><li>Requires Android environment (emulator\/physical device)<\/li><\/ul>\n\n\n\n<p>We will be talking about&nbsp;<strong>Unit Tests<\/strong>, and how to make sure that your logic do what should be done.<\/p>\n\n\n\n<p>You will find the unit tests at the&nbsp;<strong>test&nbsp;<\/strong>folder, Each unit test is a separate method annotated with @Test. This for example is asserting that 2+2 = 4. Assert used to indicate expected results. And fails test when expectation not met.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">class ExampleUnitTest {<br>    @Test<br>    fun addition_isCorrect() {<br>        assertEquals(4, 2 + 2)<br>    }<br>}<\/pre>\n\n\n\n<p>Tests grouped in classes for organization convenience and that allows execution grouping, and setup\/teardown grouping.<\/p>\n\n\n\n<p>There are times when we need to run specific operations before\/after each test, Thus we can use @Before and @After annotations.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">@Before<br>fun setUp() {<br>    \/\/ Test pre-processing (for ex. initialization)<br>}<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">@After<br>fun tearDown() {<br>    \/\/ Test post-processing (for ex. reset values)<br>}<\/pre>\n\n\n\n<p>And If you need to run operations once per class before\/after all tests you have @BeforeClass, and @AfterClass annotations.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\"><p><em>Tests preserve and enhance the flexibility, maintainability, and reusability of the production code. If you let the tests rot, then your code will rot too. Keep your tests clean.<br><\/em><strong><em>\u2013 Robert C. Martin, Clean Code<\/em><\/strong><\/p><\/blockquote>\n\n\n\n<figure class=\"wp-block-embed-youtube aligncenter wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"Build Testable Apps for Android (Google I\/O&#039;19)\" width=\"688\" height=\"387\" src=\"https:\/\/www.youtube.com\/embed\/VJi2vmaQe6w?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\">References:<\/h4>\n\n\n\n<ol><li>Clean Code [CH 9: Unit Tests]<\/li><li><a href=\"https:\/\/developer.android.com\/training\/testing\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/developer.android.com\/training\/testing<\/a><\/li><li><a href=\"https:\/\/codelabs.developers.google.com\/codelabs\/android-testing\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/codelabs.developers.google.com\/codelabs\/android-testing<\/a><\/li><\/ol>\n","protected":false},"excerpt":{"rendered":"<p>When you start developing Android apps your main target will always be delivering a working application. But when your application&hellip;<\/p>\n","protected":false},"author":2,"featured_media":44,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","footnotes":""},"categories":[7],"tags":[4,6,5],"jetpack_featured_media_url":"https:\/\/tech.aqarmap.com\/wp-content\/uploads\/2019\/08\/android-testing.png","jetpack-related-posts":[],"amp_validity":null,"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/tech.aqarmap.com\/index.php?rest_route=\/wp\/v2\/posts\/36"}],"collection":[{"href":"https:\/\/tech.aqarmap.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tech.aqarmap.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tech.aqarmap.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/tech.aqarmap.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=36"}],"version-history":[{"count":2,"href":"https:\/\/tech.aqarmap.com\/index.php?rest_route=\/wp\/v2\/posts\/36\/revisions"}],"predecessor-version":[{"id":40,"href":"https:\/\/tech.aqarmap.com\/index.php?rest_route=\/wp\/v2\/posts\/36\/revisions\/40"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/tech.aqarmap.com\/index.php?rest_route=\/wp\/v2\/media\/44"}],"wp:attachment":[{"href":"https:\/\/tech.aqarmap.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=36"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tech.aqarmap.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=36"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tech.aqarmap.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=36"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}