<?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 Kadir Altınok on Medium]]></title>
        <description><![CDATA[Stories by Kadir Altınok on Medium]]></description>
        <link>https://medium.com/@kadiraltinok?source=rss-f7473ef0a1a8------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*QqWzSqEkzRP6jkwOFwXlqA.jpeg</url>
            <title>Stories by Kadir Altınok on Medium</title>
            <link>https://medium.com/@kadiraltinok?source=rss-f7473ef0a1a8------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 18:13:44 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@kadiraltinok/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[Trendyol Android Team Unit Test Practice]]></title>
            <link>https://medium.com/trendyol-tech/trendyol-android-team-unit-test-practice-907cf8d0346?source=rss-f7473ef0a1a8------2</link>
            <guid isPermaLink="false">https://medium.com/p/907cf8d0346</guid>
            <category><![CDATA[mobile]]></category>
            <category><![CDATA[unit-testing]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Kadir Altınok]]></dc:creator>
            <pubDate>Tue, 09 Feb 2021 11:29:42 GMT</pubDate>
            <atom:updated>2021-02-15T19:51:35.056Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gfoRLXZlHW3Pw8p23-hhWw.jpeg" /></figure><p>We have been writing unit tests for about two and a half years’ as Android Team and we have experience at this time. In this article, I will talk about the unit test practice of the Trendyol Android Team.</p><p>Firstly before talking about the unit test practice, I recommend you read Trendyol Android App Architecture. The links are below:</p><ul><li><a href="https://medium.com/trendyol-tech/guide-to-trendyol-android-app-arch-d2a6a300d841">Trendyol Android App Architecture: — Part 1</a></li><li><a href="https://medium.com/trendyol-tech/trendyol-android-app-architecture-deep-dive-part-2-b00108485831">Trendyol Android App Architecture: Deep Dive — Part 2</a></li><li><a href="https://medium.com/trendyol-tech/guide-to-trendyol-android-app-unit-testing-b4beebb5665b">Guide to Trendyol Android App Unit Testing</a></li></ul><p>The classes we are writing unit tests are colored parts</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8t2fBXJ53FUZ42Mjlk6fDw.png" /></figure><h3>Template Of Unit Test Methods</h3><ul><li>Think of all the <strong>possible test cases</strong></li><li>Every unit test must start with <strong>@Test</strong> annotation</li><li>Divide the test -using comments- into three sections <strong>given</strong> for preparation, <strong>when</strong> for execution, <strong>then</strong> for verification.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/360/1*tQsY4q4_trn0bw3o4ZnYxQ.png" /></figure><p>We use mockK library to verify the method, capturing, mock class, and a lot of things.</p><p>We have unit test rules to swap the background executor used by the Architecture Components with a different one that executes each task synchronously.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ltYUJPddX-BNugTpd-3Zlw.png" /></figure><h3><strong>ViewModel</strong></h3><p>We write a unit test for three separate cases in ViewModel :</p><ul><li>Logical State</li><li>Error State</li><li>Verify method</li></ul><p><strong>Logical State</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/601/1*9-LfYJ9S_s8ynFjnlMFONw.png" /></figure><p>There is a ‘temizle’ button on the filter screen. The button is active when the filter is in an initial state, the button is passive when the filter isn’t in an initial state. If any filter is applied by the user then the state changes to not initial. The button on the left image is passive, the button on the right image is active.</p><p>How can I write it :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rLwNq4_OibmyWBBZNP5i0g.png" /></figure><p>There is FilterToolbarViewState and it has isRightTextEnabled field. The field determines if/whether the state is initial or not. Also there is a LiveData’s type of FilterToolbarViewState in MainFilterViewModel and its value is set in setToolbarClearButtonEnableState method. The method calls from the <em>doOnSuccess extension in the </em>applyCurrentFilters method. Now, I want to test if filterToolbarViewStateLiveData is set with correct data when the applyCurrentFilter method is called. And unit test in that :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nAzOuWO-ldQTr9-cz_EVdQ.png" /></figure><p><strong>Note</strong>: If you mock a class with the MockK library, MockK wants to know what that specific method inside the class returns. I define it to MockK with <strong>every {} returns</strong>. For example in the code above, I create searchUseCase with MockK and I used its method so I declare what the method should return to MockK using the code below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kFWZ2RMn_59s80tg7LBuvQ.png" /></figure><p><strong><em>preparation</em></strong>: The unit test above isn’t initial state. I start the unit test with Given using a comment. After that, I start to prepare for the unit test. Firstly I say the return of <em>fetchFilters </em>method, and then I create a filter model, also ViewModel, and initialize it. isCurrentRequestInitial return of method is false. Then applyCurrentFilters method return filter model. At the end of preparation, I create an observer to observe related liveData. Preparation is over.</p><p><strong><em>execution</em>:</strong> I call the method: applyCurrentFilters</p><p><strong><em>verification</em>:</strong> I<strong> </strong>take the first value of observer because one time observed it. And then I check if isRightTextEnabled true because the unit test isn’t initial state. And My unit test passed.</p><p><strong>Error State</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/247/1*K48wuc0BA1lDJPfuV-oCgA.png" /></figure><p>You can take an error from API sometimes. For example, I took an error on the Collection page when adding a new product to my collection. Now, I want to write a unit test for this situation. Before the unit test how can I write code:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bTNR-5MwDR3xBwMxf9Y4oA.png" /></figure><p>This class is My CollectionDetailViewModel. There are an errorDialogEvent and fragment observe it. errorDialogEvent set value in doOnThrowable extensions in followCollection method. When a user wants to add a new product to his/her collection, followCollection method is called. Now, I want to test is errorDialogEvent set with correct data when followCollection method called And unit test in that :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0sqR1FxdrYUkbEzG2Cul2A.png" /></figure><p><strong><em>preparation</em></strong>: Firstly I created retrofitException because the unit test for error state. And I called followColletion method and I gave retrofitException as a return. And then I create an observer. Finally, I added expectedValue</p><p><strong><em>execution</em>:</strong> Called the method : followColection(collectionId)</p><p><strong><em>verification</em>: </strong>I<strong> </strong>take again the first value of observer because one time observed it. And then I checked does retrofit message of the observer equal the expected message. And the unit test passed.</p><h4>Verify Method</h4><p>You want to make an API call after the success case of another API call in sometimes. In this case, you want to test it. For example: when our product detail screen opens, we make a lot of API calls. But the name of the main API call is fetchProductDetail. If fetchProductDetail returns from API call with success, then we make another API calls.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DGDdOiHvnTK6TSqGOXlZyQ.png" /></figure><p>You see ProductDetailViewModel class. FetchRelatedProducts API call is making after fetchProductDetail API call in success state. In this case, I want to check did the FetchRelatedProducts API call made after success? And unit test in that :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mClOPHpY3haAkTHKPJPe9g.png" /></figure><p><strong><em>preparation</em></strong>: Mocked product and productId. productResource was set product with success state because the unit test for the success of the unit test. And then fetchProductDetail method called and returned productResource.</p><p><strong><em>execution</em>:</strong> Called the method : fetchProductDetail(productId)</p><p><strong><em>verification</em>: </strong>I verify did fetchRelatedProducts API call made</p><h3>UseCase</h3><ul><li>Verify method</li></ul><p><strong>Verify method</strong></p><p>Verifying method unit test in UseCase class, same as ViewModel verify.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LYufRKwR8jIcGUU6ARlN_g.png" /></figure><p>This is UseCase class. There is a logic in fetchSellerComponent method : isSellerComponentEnabled. This logic comes from the A/B test. isSellerComponentEnabled returns true in B case returns false in A case. if the method returns true, fetchAbVersionComponent method is called, else it returns false then fetchAsIsVersionComponent method is called. While made 3 API calls in the fetchAbVersionComponent method, made 2 API calls in the fetchAsIsVersionComponent method. In this case, I want to check have the correct API calls been made? And unit test in that :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pFTX4mex7nl9xnxSlffBpw.png" /></figure><p><strong><em>preparation</em></strong>: A/B test returns ‘A’ in this case.</p><p><strong><em>execution</em>: </strong>Called the method : fetchSellerComponent(…)</p><p><strong><em>verification</em>: </strong>Two API calls should be made in this case: fetchProductDetailSellerQuestionsInfo and fetchSellerStoreAvailability so the top two verify the test passed. The third one is productSearch shouldn’t call so it exactly is 0. And also the last one passed.</p><h3>ViewState</h3><ul><li>Logical State</li></ul><p>There is simple logic in ViewState like the visibility of any view sometimes. We write unit tests for it every time.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-RERkOEt6rTHfBEOml5DzA.png" /></figure><p>There are two images above. These images are from the Trendyol product detail screen. The title of the view is ‘Dahili Hafiza’ and the view shows product options. Options of the left view are 64GB and 128GB. There is an info-title in the upper right corner. Info title is determined by the count of options. So, the title is ’2 Farkli Dahili Hafiza’ in the image on the left. But the title is no visible in the image on the right. Because there is a logic in the visibility of the title. The logic is that: if the count of options is bigger than one, then the title is visible, but it equal one, then the title is not visible.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*R-fjiWqg2-2HLQhANT9q2Q.png" /></figure><p>This class is ViewState class and logic(are Count of options bigger than 1) is in shouldShowInfoTitle method. And unit test in that :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vqN17D9M3mUnmN41RiGBTw.png" /></figure><p><strong><em>preparation</em></strong>: I provide ViewState with two options: 32 GB and 64 GB, for this reason, I expect visibility of title is visible and I set expectedValue is true.</p><p><strong><em>execution</em>: </strong>Called the method : shouldShowInfoTitle</p><p><strong><em>verification</em>: </strong>I<strong> </strong>verify actualValue is equal expectedValue and unit test passed.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ACSr_4k7tYE6uVdvPQdUTg.png" /></figure><p><strong><em>preparation</em></strong>: I provide ViewState with one option: 32 GB, for this reason, I expect visibility of title isn’t visible and I set expectedValue as false.</p><p><strong><em>execution</em>: </strong>Called the method : shouldShowInfoTitle</p><p><strong><em>verification</em>: </strong>I<strong> </strong>verify actualValue is equal expectedValue and unit test passed.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/504/1*p-rfVYySUjvmnoWgVc4DQg.png" /></figure><p>Now we will talk about the different unit tests as a logical state. There are four product cards in the Image above. The product cards have a background depending on the situation. For example, the background of the first two product cards is passive, the third one is selectable, the last one is selected. How can write code:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SqtbWavuuLpZ31NeS1HeiQ.png" /></figure><p>I have enum class and you see three states: selectable, selected, passive. ProductAttributeBackgroundViewState has getBackground method. The method returns drawable according to state. And unit test in that :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*urqi8C8lGlXTrzm0GaVvnQ.png" /></figure><p><strong><em>preparation</em></strong>: This unit test is for a selectable state. I set selectable enum state to ProductAttributeBackgroundViewState constructor and I provide ViewState. I expect shape_product_detail_selectable as background and set it to expectedResult</p><p><strong><em>execution</em>: </strong>Called the method : getBackground()</p><p><strong><em>verification</em>: </strong>I<strong> </strong>verify actualResult is equal expectedResult and unit test passed.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wrbPsg7j5-_OmnlLvBTF-Q.png" /></figure><p><strong><em>preparation</em></strong>: These unit tests are: selected and passive. I provide ViewState and set expectedResult.</p><p><strong><em>execution</em>: </strong>Called the method : getBackground()</p><p><strong><em>verification</em>: </strong>I<strong> </strong>verify actualResult is equal expectedResult and unit test passed.</p><p><strong>Parameterized Test</strong></p><p>The parameterized test combines more than one method and makes them one method. The method takes parameters. In fact, it is a normal dynamic method. Using parameterized tests would be more correct sometimes. Because it can be expandable.</p><p>For example, examine unit test on above (getBackground method). There are three different options. In the future, it can be more options. In this situation, we should use parameterized test. Let&#39;s write the unit test above again as a parameterized test.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*224DEWoO-He-EN6VOUmZRA.png" /></figure><p><strong><em>preparation</em></strong>: I have one method and the method called three times. I set input and output in provideArguments method. I create the ViewState class in the test method.</p><p><strong><em>execution</em>: </strong>Called the method : getBackground()</p><p><strong><em>verification</em>: </strong>I<strong> </strong>verify actualResult is equal expectedResult and unit test passed.</p><p>In the future, I add a fourth state: ABC</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/834/1*400zw7bto6I1_tXk79MMeA.png" /></figure><p>I add only input and output state in provideArguments, no need new unit test method. If I hadn’t written the unit test as a parameterized test then I should have created a new test method.</p><h3>Mapper</h3><ul><li>Input-output</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1mjtX9Un7vBuKEhABNfT5g.png" /></figure><p>We write unit tests for important domains like search - filter. mapFrom method takes two parameters as input and returns ProductSearchResponse. And unit test in that :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cpXWniT3Re5eSpGzyVNzCA.png" /></figure><p><strong><em>preparation</em></strong>: You see, I write the unit test again as a parameterized test. Because there are a lot of test cases. I write the unit test for five different cases. I create filterResponseMapper.</p><p><strong><em>execution</em>: </strong>Called the method: mapFrom()</p><p><strong><em>verification</em>: </strong>I<strong> </strong>verify actualResponse is equal expectedResponse and unit test passed.</p><h3>Decider</h3><p>Decider class always holds logic.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DIibn3nwx7_l46ANDjl1lw.png" /></figure><p>PopularFilterAttributeDecider has one method: isPopular. The method takes one parameter: FilterDisplayResponse. If the type of FilterDisplayResponse is POPULAR then return POPULAR_BRAND else return null. The decider is used for searchAttributeFieldType. And unit test in that :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*e_ZOzDT-ZoKs-eUPsV2zFA.png" /></figure><p><strong><em>preparation</em></strong>: There are two unit tests. I create a decider and also FilterDisplayResponse. I take ‘Popular’ to FilterDisplayResponse constructor for the first unit test and I take ‘Visible’ for the second one. I create expectedValue for both.</p><p><strong><em>execution</em>: </strong>Called the method : isPopular()</p><p><strong><em>verification</em>: </strong>I<strong> </strong>verify actualValue is equal expectedValue and unit test passed.</p><p>That’s all! I have tried to explain the unit test practice of the Trendyol Android Team. I hope the article has been useful. If you have any questions, feel free to contact me.</p><p>Stay healthy 😊</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=907cf8d0346" width="1" height="1" alt=""><hr><p><a href="https://medium.com/trendyol-tech/trendyol-android-team-unit-test-practice-907cf8d0346">Trendyol Android Team Unit Test Practice</a> was originally published in <a href="https://medium.com/trendyol-tech">Trendyol Tech</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>