<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: kapil</title>
    <description>The latest articles on DEV Community by kapil (@kapil_aggarwal1).</description>
    <link>https://dev.to/kapil_aggarwal1</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F15898%2F932cf54d-8127-41ba-bc9a-b3de2ebb7a1f.jpg</url>
      <title>DEV Community: kapil</title>
      <link>https://dev.to/kapil_aggarwal1</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kapil_aggarwal1"/>
    <language>en</language>
    <item>
      <title>Test Environments- What &amp; Why</title>
      <dc:creator>kapil</dc:creator>
      <pubDate>Tue, 12 Jan 2021 12:28:16 +0000</pubDate>
      <link>https://dev.to/kapil_aggarwal1/test-environments-what-why-e2a</link>
      <guid>https://dev.to/kapil_aggarwal1/test-environments-what-why-e2a</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--FllDhS4w--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2AC1r08yCygU4__sFiIEsGtA.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--FllDhS4w--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2AC1r08yCygU4__sFiIEsGtA.jpeg" alt=""&gt;&lt;/a&gt;Photo by &lt;a href="https://unsplash.com/@henrikfotograf?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Henrik Berg&lt;/a&gt; on &lt;a href="https://unsplash.com/s/photos/aurora?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Unsplash&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Why do we have so many/less test environments, why is volume of test data so small on dev environment, where am I going to test critical bug in production without disturbing BAU development, How about testing db cluster setup; where should I test those? All valid questions? Without a further ado let’s get on to answer few of them.&lt;/p&gt;

&lt;p&gt;First thing first: What is test environment&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Test environment is a set of hardware on which application under test(AUT) and supporting software are deployed in order to assist in testing.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The good part of working in my current organisation is that you get a chance to work on different projects in a very short span of time. Different project means different requirements, different setup, different personalities and different arguments for the same topic. Lately I have been talking to different people about, what all environments do we need in a given project?&lt;/p&gt;

&lt;p&gt;Short answer to the question is: “it depends” on the project and current setup of the organisation infrastructure. While this is true but for simplicity we can categorise project in below 3 categories. There are difference Devops maturity model your project can be in. For further reading, refer state of Devops over &lt;a href="https://services.google.com/fh/files/misc/state-of-devops-2019.pdf"&gt;here&lt;/a&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Following waterfall SDLC&lt;/li&gt;
&lt;li&gt;Projects transitioning to Agile &amp;amp; CI/CD&lt;/li&gt;
&lt;li&gt;Project already running in CI/CD&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;As of now I have worked in 2 different types of projects, yet to work on a project following Continuous delivery.&lt;/p&gt;

&lt;p&gt;Ok coming back to test environments; with Devops it feels very tempting to have to too many environments and at the same time without Devops we try to manage in whatever test environment is available to us. Both of the approaches comes with pros &amp;amp; cons. Too many test environment comes with overhead of maintaining, deploying application and infrastructure cost. Less test environment might increase testing time and impact quality of scenarios one wants to perform in a project.&lt;/p&gt;

&lt;p&gt;Another way to look at the test environment requirement is the number of testing/quality gates and where they will run for successful and timely delivery of software.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Some of the testing activities in typical project will be; Dev testing, Infra testing, Automation testing, Qa Exploratory testing, Integration testing, Business user testing, Compatibility testing, Security testing, Performance testing, Hotfix bug testing/Signoff etc&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The bare minimum environment for a typical enterprise project which I have seen so far are Dev, QA, UAT, Perf environments apart from Production but again it varies from project to project(yes, it depends). There are other optional environments also like CI(Automation), Infra, SIT.&lt;/p&gt;

&lt;p&gt;One question that comes to mind after seeing all such environments is, do we need so many environments? Lemme know your thoughts in the comments section&lt;/p&gt;

&lt;p&gt;Let's get down to what &amp;amp; why of all these major test environments.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Dev environment&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;Primary owner&lt;/em&gt;: Developers&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Purpose &amp;amp; Activities&lt;/em&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Used for dev testing and assist in development activities, can choose to run automated E2E testing&lt;/li&gt;
&lt;li&gt;Automated security scans[Static code does not require any environment though]&lt;/li&gt;
&lt;li&gt;Authentication/Authorisation are relaxed to assist in testing&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Environment Setup&lt;/em&gt;:&lt;/p&gt;

&lt;p&gt;Trimmed down version of production, if there are multiple cluster in production this will typically have 1 cluster setup&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Test Data&lt;/em&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Small size with good enough variation of test data&lt;/li&gt;
&lt;li&gt;Can be mocked for better stability and different scenarios creations&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Deployed artefact version&lt;/em&gt;: Always latest&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;QA environment&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;Primary owner&lt;/em&gt;: QAs/ QA Lead&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Purpose &amp;amp; Activities:&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Acceptance testing &amp;amp; Story Sign off&lt;/li&gt;
&lt;li&gt;Design/UI testing&lt;/li&gt;
&lt;li&gt;Exploratory testing&lt;/li&gt;
&lt;li&gt;Can be used to run automated regression suite&lt;/li&gt;
&lt;li&gt;Bug bash&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Environment Setup&lt;/em&gt;:&lt;/p&gt;

&lt;p&gt;Trimmed down version of production, if there are multiple cluster in production this will typically have 1 cluster setup&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Test Data:&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Should be always integrated with stable backend&lt;/li&gt;
&lt;li&gt;Different shape &amp;amp; size of test data from production with masked PII&lt;/li&gt;
&lt;li&gt;Test data pipeline to update test data with a certain frequency [like once in a month etc]&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Deployed artefact version&lt;/em&gt;: Runs behind or equivalent to Dev environment&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;SIT Environment&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;This environment is usually optional as all of these activities can also be performed in QA environment but typically in enterprise we do have separate SIT environment for smooth working of different teams.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Primary owner&lt;/em&gt;: Release manager/ QA Lead&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Purpose &amp;amp; Activities:&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Integration testing of releasable artefacts&lt;/li&gt;
&lt;li&gt;All other environments usually connects with dependent artefacts of this environment.&lt;/li&gt;
&lt;li&gt;Manual backward compatibility testing e.g. new backend and older front end [mobile apps]&lt;/li&gt;
&lt;li&gt;Manual regression suite [if applicable]&lt;/li&gt;
&lt;li&gt;Manual Security/Penetration testing&lt;/li&gt;
&lt;li&gt;Roll back strategy testing&lt;/li&gt;
&lt;li&gt;Verbiage verification if any&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Environment Setup&lt;/em&gt;:&lt;/p&gt;

&lt;p&gt;Trimmed down version of production, if there are 5 node setup . This environment will have 3 nodes.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Test Data:&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Should be always integrated with stable backend&lt;/li&gt;
&lt;li&gt;Different shape &amp;amp; size of test data&lt;/li&gt;
&lt;li&gt;No Mocking&lt;/li&gt;
&lt;li&gt;Test data pipeline to update test data with a certain frequency [like once in a month etc]&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Deployed artefact version:&lt;/em&gt; Typically runs behind QA environment&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;UAT Environment&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;Primary owner&lt;/em&gt;: Release Manager/ QA Lead&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Purpose &amp;amp; Activities:&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Stands for user acceptance testing or SME/Business owner testing: mimicking actual business scenarios.&lt;/li&gt;
&lt;li&gt;Usually requirement miss etc are feedbacks from this environment&lt;/li&gt;
&lt;li&gt;Business owner bug bashes etc&lt;/li&gt;
&lt;li&gt;Hotfix bug testing&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Test Data:&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Replica of production with PII masked.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Deployed artefact version:&lt;/em&gt; Typically runs behind SIT by a version&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Perf Environment&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;As the name suggests, this environment is used to run various performance &amp;amp; benchmarking tests.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Primary owner&lt;/em&gt;: QA lead, Release Manager or Performance team if separate&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Purpose &amp;amp; Activities:&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Benchmarking with simulating different load scenarios for certifying speed, scalability &amp;amp; stability of application&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Environment Setup&lt;/em&gt;: Same as production or very close to production&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Test Data:&lt;/em&gt; Similar to production in shape &amp;amp; size.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Deployed artefact version:&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Run behind by SIT by a version or equivalent to SIT.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Above list is my experience of working in enterprise accounts, do let me know your setup in comments section. I have seen few variation e.g. having separate automation environment or separate Infra environment for testing roll back strategy or infra scripts but typically above is good enough list. You can find different names of testing environment in different organisation but once we know the purpose we can always reason about the same. Also there are numerous hacks we can put in order to assist in test environment. But general rule of thumb is; all access and setup of test environment should be as close as possible to production; be it opening to internet to test data setup and should assist in verification of Application under test.&lt;/p&gt;

&lt;p&gt;Happy Testing!&lt;/p&gt;

</description>
      <category>infrastructure</category>
      <category>testing</category>
      <category>devops</category>
      <category>testenvironment</category>
    </item>
    <item>
      <title>One Failing Unit Test</title>
      <dc:creator>kapil</dc:creator>
      <pubDate>Sun, 10 Jun 2018 12:59:42 +0000</pubDate>
      <link>https://dev.to/kapil_aggarwal1/one-failing-unit-test-368</link>
      <guid>https://dev.to/kapil_aggarwal1/one-failing-unit-test-368</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1024%2F1%2Aq1dabCG6YzaqeLtf5ZZmgw.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1024%2F1%2Aq1dabCG6YzaqeLtf5ZZmgw.jpeg"&gt;&lt;/a&gt;unit&lt;/p&gt;

&lt;p&gt;Currently I am working in a project as QA where we are doing re-model of Sql database to new shiny Graph db and writing a new rest service on top of it.&lt;/p&gt;

&lt;p&gt;Greenfield “yay” . I have been hearing a lot about &lt;a href="https://martinfowler.com/bliki/TestPyramid.html" rel="noopener noreferrer"&gt;Test Pyramid&lt;/a&gt; for quite sometime and now is the time when will be witnessing how it is built with all ups and downs.&lt;/p&gt;

&lt;p&gt;In test pyramid most of the automated test should be covered at unit level, then some at integration and few for end to end journeys.&lt;/p&gt;

&lt;p&gt;Unit testing for most of my career was black box. Also as a gate keeper of App(this statement is debatable, lets park this for future), trusting test cases which are majority in number, which you pretend to understand and also have not written, is not very comfortable position to be in.&lt;/p&gt;

&lt;p&gt;Hence I wanted to keep my eyes ear open for anything which is not caught by unit test. And my wish was answered very soon.&lt;/p&gt;

&lt;p&gt;It all started with a small ad hock change request from a BA in our team.&lt;/p&gt;

&lt;h3&gt;
  
  
  Requirement
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;BA&lt;/em&gt;: Hey can we please change the “id” to “key” in the response.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Dev&lt;/em&gt;: Sure.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;QA&lt;/em&gt;: will be tested in a jiffy.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1024%2F1%2Ak2I1T8faygpFoOGZQoyxJg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1024%2F1%2Ak2I1T8faygpFoOGZQoyxJg.png"&gt;&lt;/a&gt;Requirement&lt;/p&gt;

&lt;p&gt;Lets look at some pseudo code&lt;/p&gt;

&lt;h3&gt;
  
  
  Implementation
&lt;/h3&gt;

&lt;p&gt;Lets assume we are building simple App which returns Employee details. Pseudo code will look something like below.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Scala case class as model&lt;/li&gt;
&lt;li&gt;Serialization and deserialization&lt;/li&gt;
&lt;li&gt;Neo4j connection&lt;/li&gt;
&lt;li&gt;Query to get Employee
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;**case class** Employee(id: String, firstName:String, lastName:String)
**implicit val** employeeFormat = jsonFormat3(Employee)


def getEmployee(id:String): Employee {
//read neo4j data and return case class 
query = "match (e:Employee {id:id})

//some code to parse query result and make Employee model
return Employee(e)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Minimal test case &amp;amp; seed test data for above code will be.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;**def** testEmployee():Unit={
//spin in-memory neo4j
//add create (e1:Employee {id:'1', firstName:'Joe', lastName:'Doe')
//val employee = getEmployee("1")
//assert employee model with "key"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Everything is good so far, developer actually failed test cases by changing “id” to “key” in assertion of testEmployeModel test case, fixed code and “Yay” TDD.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Problem
&lt;/h3&gt;

&lt;p&gt;While doing e2e testing we loaded data in test db.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;create (e1:Employee {key:’1', firstName:’Joe’, lastName:’Doe’)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Hit the service and “internal server error”.&lt;/p&gt;

&lt;p&gt;Though the fix for this bug was pretty straightforward, but it kept me thinking what could have gone wrong here. We have written unit test case for this requirement, even development was done TDD way, now how come this bug?&lt;/p&gt;

&lt;p&gt;If you know the answer then continue else go through the implementation one more time and see if you can figure out the problem.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Fix
&lt;/h3&gt;

&lt;p&gt;Change query in getEmployee method to&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;query = "match (e:Employee {key:id}) return Employee(e)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  The Miss
&lt;/h3&gt;

&lt;p&gt;There are 2 fold miss in this.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Requirement understanding&lt;/li&gt;
&lt;li&gt;Test Fixture&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Requirement from BA was never understood/explained clearly as implementation was easy and never discussed the “why” behind it. Consumer of the service wants “key” instead of “id” but also it was the database value which was going to change in the data-ingestion part.&lt;/p&gt;

&lt;p&gt;If some more effort was spent in understanding the test case, and test data in particular for a unit test case before making any change to code. This problem must have been caught and fixed then and there itself.&lt;/p&gt;

&lt;p&gt;But now imagine once the project matures, these test fixtures would be present in thousands of test how would someone know what to fix/update.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Its better to have no test case then a test case giving false positive results.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Have you ever experienced this problem of updating or forgetting to update hundreds of test fixture for any change in functionality, if yes any solution/strategy to handle. Do share in comment section.&lt;/p&gt;

</description>
      <category>programming</category>
      <category>testing</category>
      <category>unittesting</category>
    </item>
    <item>
      <title>API Mocks and why should we care</title>
      <dc:creator>kapil</dc:creator>
      <pubDate>Tue, 13 Feb 2018 06:33:51 +0000</pubDate>
      <link>https://dev.to/kapil_aggarwal1/api-mocks-and-why-should-we-care-38po</link>
      <guid>https://dev.to/kapil_aggarwal1/api-mocks-and-why-should-we-care-38po</guid>
      <description>

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--uFWLZ65z--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2A66KbxEMsRw0w8I_qcTT0pg.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--uFWLZ65z--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2A66KbxEMsRw0w8I_qcTT0pg.jpeg" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Service virtualization, Fakes, Stubs, Mocks, dummies etc are all different categories of Test Doubles which on broader level return &lt;em&gt;a response for a given request in a controlled manner.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;A request can be a method call, Object creation , database interaction or an webAPI interaction.&lt;/p&gt;

&lt;p&gt;In below article we will be talking about API mocking and try to discuss some of &lt;strong&gt;what, why, how, when&lt;/strong&gt; of API Mocks and also some &lt;strong&gt;challenges&lt;/strong&gt; with them. If you are more concerned about the difference amongst all different types of Test Doubles then please head over &lt;a href="https://www.infoq.com/articles/stubbing-mocking-service-virtualization-differences"&gt;here&lt;/a&gt; and &lt;a href="https://martinfowler.com/articles/mocksArentStubs.html"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Enough said lets get started&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;What is Mocking&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;Mocks is nothing but an imitation(of what?). It simulates behaviour of real API but in a more a controlled manner.&lt;/p&gt;

&lt;p&gt;A simple mock server consist of a server, which on matching certain request returns pre-defined response and other parameters associated such as response code, headers etc.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Why API Mocking&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;Getting quick and consistent feedback for a test are some of the key principles of continuous integration and API Mocks really help in facilitating this.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Quick feedback&lt;/em&gt; is very obvious, but lets understand.&lt;/p&gt;

&lt;p&gt;Quicker the feedback of testing(either manual /automation) earlier bugs are caught and fixed. Have you been in a situation where a single test scenario take 15 min to complete with flaky payment service (roughly around 12 API calls), If yes embrace API Mocks.&lt;/p&gt;

&lt;p&gt;What do we mean by &lt;em&gt;“consistent/deterministic results”&lt;/em&gt;?.&lt;/p&gt;

&lt;p&gt;Consider an airline booking application where we have to check whether user is able to&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Search for flight from PUN-DEL&lt;/li&gt;
&lt;li&gt;Select Seats for the passenger&lt;/li&gt;
&lt;li&gt;Add trip extras like “in-flight wifi” etc&lt;/li&gt;
&lt;li&gt;Complete Purchase&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Over here search results, availability of seats and Trip Extras will vary depending on the current state of the flight, hence non deterministic tests.&lt;/p&gt;

&lt;p&gt;But if we have mocked API responses for seat availability, then seat 25A will always be available.&lt;/p&gt;

&lt;p&gt;Other benefits of API mocks are&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If we are doing visual comparison of screen, then data painted on each screen also needs to be consistent in order for visuals comparison to work.&lt;/li&gt;
&lt;li&gt;Validating manually scenarios when test environment is not available by pointing to Mocks.&lt;/li&gt;
&lt;li&gt;Validating issues(prod/new issue) with different version of apps by pointing to same mock environment.&lt;/li&gt;
&lt;li&gt;If backend and UI development are happening in parallel then UI needs a contract/dummy response to work with.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;What to Mock?&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;As this article is pretty much about mocking backend, so yes backend should be mocked, but what if your team is owning some bit of backend(1 or 2 service) and UI.&lt;/p&gt;

&lt;p&gt;Question to ask is&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Should we mock backend APIs that we own or all the backend APIs to UI should be mocked.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--C3_lWNWx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2AMnngq59LoQMeacfmPhIleg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--C3_lWNWx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2AMnngq59LoQMeacfmPhIleg.png" alt=""&gt;&lt;/a&gt;Backend Mocked&lt;/p&gt;

&lt;p&gt;If you are looking for a straight answers then “We should mock all the backend APIs of components which are owned by Team”. agree?&lt;/p&gt;

&lt;p&gt;Let say team somehow agreed to approach where everything backend to UI will be mocked. Refer below&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--fj9WdroJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2AZ24cwTHzTA5BC_80RYvdQg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--fj9WdroJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2AZ24cwTHzTA5BC_80RYvdQg.png" alt=""&gt;&lt;/a&gt;UI Backend Mocked&lt;/p&gt;

&lt;p&gt;One fine day if SHIM layer owned by team is changed without making changes to UI then your UI test case will &lt;strong&gt;NOT&lt;/strong&gt; raise any flags as they are always working with the mocked response and test will be returning false positives.&lt;/p&gt;

&lt;p&gt;False positive is the last thing which you want in environment where confidence in automation test case is really crucial.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Food for thought&lt;/em&gt; : Is this problem with every API mocking(Let me know your thoughts in comments section, if yes how we can keep a check?)&lt;/p&gt;

&lt;h4&gt;
  
  
  How to API mock
&lt;/h4&gt;

&lt;p&gt;First thing First : For mocking API, we need configuration in our app which can selectively point to mocks environment or live services based on the switch. It can be a javascript function which can change host &amp;amp; port or setting page in test builds for mobile app where one can select environment.&lt;/p&gt;

&lt;p&gt;For writing mocks there are many open source tools available, personally I never felt the need to go for the paid solution for mocks or service virtualization.&lt;/p&gt;

&lt;p&gt;Some of the popular open source solutions are &lt;a href="http://wiremock.org/"&gt;wiremock&lt;/a&gt;, &lt;a href="http://www.mbtest.org"&gt;MounteBank&lt;/a&gt;. There is good documentation and “Getting Started” help available on respective tools website, but just to give glimpse how easy is to write mock for GET request, here is a snippet.&lt;/p&gt;

&lt;p&gt;A simple mock GET request for “greet” in wiremock looks as follows&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class Test {

public static void main(String[] args){
        stubFor(get(urlEqualTo("/greet"))
                .willReturn(aResponse().withBody("Hello World")));
    }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If responses are big then storing them in file and returning file content is something to consider.&lt;/p&gt;

&lt;p&gt;Also mocked response can be templatized to include date sensitive responses like greet users with “todays date”.&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public static void main(String[] args){
    stubFor(get(urlEqualTo("/greet"))
            .willReturn(aResponse().withBody("Hello World, today is:"+ DateTime.now())));
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h4&gt;
  
  
  &lt;strong&gt;When to API mocks&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;It again depends upon the project, if we have agreed on why part of it, based on the automation need, it should be early in the project also if any of the backend service is flaky or slow then those should be mocked.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Challenges&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;As we have seen above API mocks really comes handy for UI automation and other manual scenarios but there are some challenges associated with it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;States in Mocks&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Mocking gets more complex if we have states in our app for example : /greeting api will return “Hello World” and then on subsequent /greeting we need to return “Hello Universe”.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Great powers comes with great responsibilities&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Also, this prevents parallelization of test cases as one fine day states will interfere and test will fail and debugging will take ages.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Stale mocks&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;As we discussed in what to mocks section, there are chances mocks will become stale and there is no clear way to know when they have become stale(unless clubbed with Contract tests) &lt;strong&gt;.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Templates&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--QA97KjWP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/225/1%2A-bQMHPTZfCp-Pm4n9ilskA.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--QA97KjWP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/225/1%2A-bQMHPTZfCp-Pm4n9ilskA.jpeg" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Templatization issues for Date time needs to be sorted very carefully, if UI tests and mocks are running in different timezones.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Broken Request&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Consider a request with all mandatory objects&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "action": "greet",
  "name": "kapil",
  "date": "2018-02-01"
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In order to make mocks simple, quite many times mocks mapping for the above request would be something like&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;stubFor(post(urlEqualTo("/greet"))
        .withRequestBody(containing("greet"))
        .withRequestBody(containing("kapil"))
        .willReturn(aResponse().withBody("Hello Kapil!")));
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now consider a scenario where there is a bug and mandatory “date” object is not coming in request, our UI automation test case will always pass again giving false positives.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Effort for mock updates&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Last but not the least, effort required to update mocks(or automated way to do that) when backend changes is something to consider upfront and this is coming from an experience of working in project that has ~3000 mock responses.&lt;/p&gt;

&lt;p&gt;And thats pretty much it, I hope this article has given insights in API mocking. Will be discussing about some challenges in follow up post. Do let me know your feedback in comment section, and if you like the article do share.&lt;/p&gt;


</description>
      <category>testing</category>
      <category>automation</category>
      <category>mobiletesting</category>
      <category>api</category>
    </item>
  </channel>
</rss>
