<?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 Abhi Nandan on Medium]]></title>
        <description><![CDATA[Stories by Abhi Nandan on Medium]]></description>
        <link>https://medium.com/@nandan.abhi10?source=rss-9c9d8982ec2d------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*7SXgG8w2KxIEseb8jLscdQ@2x.jpeg</url>
            <title>Stories by Abhi Nandan on Medium</title>
            <link>https://medium.com/@nandan.abhi10?source=rss-9c9d8982ec2d------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 16:54:19 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@nandan.abhi10/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[Effective Microservice Testing — Building Things Right]]></title>
            <link>https://medium.com/@nandan.abhi10/effective-microservice-testing-building-things-right-92d32df0f273?source=rss-9c9d8982ec2d------2</link>
            <guid isPermaLink="false">https://medium.com/p/92d32df0f273</guid>
            <category><![CDATA[ecommerce]]></category>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[microservices]]></category>
            <category><![CDATA[software-engineering]]></category>
            <dc:creator><![CDATA[Abhi Nandan]]></dc:creator>
            <pubDate>Thu, 27 Jun 2024 09:27:18 GMT</pubDate>
            <atom:updated>2024-06-27T09:27:18.673Z</atom:updated>
            <content:encoded><![CDATA[<h3>Effective Microservice Testing — Building Things Right</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BEDSIwpXVYW5_tQuUXxwUQ.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@alinnnaaaa?utm_content=creditCopyText&amp;utm_medium=referral&amp;utm_source=unsplash">Alina Grubnyak</a> on <a href="https://unsplash.com/photos/low-angle-photography-of-metal-structure-ZiQkhI7417A?utm_content=creditCopyText&amp;utm_medium=referral&amp;utm_source=unsplash">Unsplash</a></figcaption></figure><p>At Emirates Group, we are dedicated to staying at the forefront of technological innovation and excellence. Over the past decade, software testing has advanced significantly, with new tools and methodologies continually emerging. Despite these innovations, effectively testing applications within a distributed system, such as our microservice architecture, remains a complex and challenging endeavour. This article outlines the design principles and best practices we follow at Emirates Group to ensure robust and reliable testing in a microservice architecture, reflecting our commitment to quality and efficiency.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gyhbg42p2HtMgLZM34E1wQ.png" /><figcaption>From Monolith to Microservices</figcaption></figure><h3>The Importance of Microservices</h3><p>Architecture is crucial because it influences product quality, particularly in terms of software delivery speed, maintainability, extensibility, and testability. In line with <a href="https://www.manning.com/books/microservices-patterns?a_aid=microservices-patterns-chris&amp;a_bid=2d6d8a4d">Chris Richardson’s “Microservices Patterns”</a> and inspired by Martin Abbott and Michael Fisher’s three-dimensional scalability model — <a href="https://learning.oreilly.com/library/view/building-microservices-with/9781785887833/fd2b2899-d396-4fdb-8b7b-08e8eb97a71a.xhtml">the scale cube</a> — we at Emirates Group embrace three approaches to scaling applications:</p><ol><li><strong>X-axis scalability (horizontal scaling)</strong>: Running multiple identical instances of an application behind a load balancer.</li><li><strong>Y-axis scalability (functional decomposition)</strong>: Breaking down monolithic applications into granular, loosely coupled components with bounded contexts.</li><li><strong>Z-axis scalability (data partitioning)</strong>: Scaling based on data partitioning, where each instance handles a subset of data.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/797/1*UDS0hJLzv2SCGF7120W8Ig.png" /><figcaption>Scale Cube</figcaption></figure><p>Modularity and scalability are central to our microservice architecture. As the number of microservices grows, and asynchronous messaging, caching, and other elements are introduced, the architecture becomes more complex. Initially, our testing focuses on ensuring readiness for production, but as the application goes live, the line between development and production activities becomes blurred.</p><h3>The Challenge of Microservice Testing</h3><p>The push for faster software delivery has transitioned monolithic architectures to microservices, increasing the emphasis on testing and observability, and advocating for more automated testing. However, designing an effective test strategy with comprehensive automation is challenging. Mike Cohn’s test pyramid is a widely accepted best practice for microservice testing, but its implementation often falls short. The primary difficulty lies in designing the correct set of tests and placing them appropriately. Common issues we encounter include:</p><h4><strong>Good in isolation but inconclusive when integrated</strong></h4><p>Integration testing with numerous granular services requires thorough knowledge of all services in context, demanding increased coordination and potentially slowing down delivery.</p><h4><strong>Stubs are useful but not fully trustworthy</strong></h4><p>To alleviate integration test issues, we often use test doubles. While this reduces dependency on other teams, managing mocks and stubs can become overwhelming, and their validity is often unverified, leading to reliance on end-to-end tests.</p><h4><strong>Building the right thing vs. building things right</strong></h4><p>Proper implementation of layered test suites is challenging. Many teams have the capability to implement testing strategies but often misplace tests, leading to costly and brittle rightward test phases. The debate centers on short-term versus long-term benefits. While lower-level tests are costly in terms of development time, they provide significant long-term rewards, including reduced technical debt and better application maintainability.</p><h4><strong>Knowing what went wrong but not where</strong></h4><p>Tests often fail to trace problems to their root cause. Aggregating application logs and test reports into a single visualization is difficult, requiring extra effort to find the root cause. Asynchronous message-driven communication in microservices adds further complexity.</p><h4><strong>Test environments do not replicate production</strong></h4><p>Microservices are easier to test in isolation than monolithic systems, but setting up a new test environment can be complex, involving multiple service deployments, databases, messaging systems, load balancers, and gateways. Without the capability to create on-demand test environments, some issues may only be detected in pre-production or production stages.</p><h3>Our Approach at Emirates Group</h3><p>Despite the difficulties, implementing an effective microservice testing strategy is achievable. Many tech influencers extensively use microservice architecture, with numerous case studies available. Here are some recommendations:</p><h4>Stick to the Basics</h4><p>While adopting new tools and technologies, we adhere to fundamental principles. Lisa Crispin and Janet Gregory’s “<a href="https://lisacrispin.com/2011/11/08/using-the-agile-testing-quadrants/">Agile Testing</a>” modifies Brian Marick’s <strong>Testing Quadrants</strong> to classify different types of tests, which is a great starting point for segregating tests into appropriate phases. Technology-facing tests are ideal for automation, while business-focused tests determine feature readiness.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/872/1*m601BbZ5UDzvGnMtfkB6pg.png" /><figcaption>Testing Quadrants</figcaption></figure><h4><strong>Data-Driven Approach with Metrics and KPIs</strong></h4><p>At Emirates Group, metrics and KPIs play a crucial role in shaping our testing strategy. By adopting a data-driven approach, we continuously monitor and refine our processes to enhance efficiency and quality. Our primary focus is on improving DORA metrics (Deployment Frequency, Lead Time for Changes, Time to Restore Service, and Change Failure Rate), reducing time to market, enhancing product quality by minimizing defects that escape into production and reduce software debts.</p><p>We collect and analyze data from various stages of the software development lifecycle to identify bottlenecks and areas for improvement. This approach allows us to make informed decisions, ensuring that our testing strategy aligns with our business goals. By leveraging these insights, we have improved our delivery pipeline, ensuring faster, more reliable releases with higher quality.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/447/1*hiFOISitkGP3iow_XgqO-Q.png" /><figcaption>Quality Triangle</figcaption></figure><h4><strong>A Holistic View of Microservice Testing</strong></h4><p>The scale-cube model helps in designing a testing strategy that aligns with the three axes:</p><ol><li><strong>Y-axis testing</strong>: Functional decomposition tests, including unit, component, contract, and integration tests. Lower-level tests use test doubles, while higher-level tests are end-to-end and require an integrated environment.</li><li><strong>X-axis testing</strong>: Tests for horizontal scaling capabilities, such as performance, load, stress testing, and chaos engineering, assess auto-scaling and self-healing properties.</li><li><strong>Z-axis testing</strong>: Tests for behavior consistency across data partitions, ensuring correct routing to data-holding servers, assessing data consistency, and SLA adherence.</li></ol><h4><strong>Building the Right Things</strong></h4><p>A solid understanding of fundamentals and the big picture aids in creating an effective testing strategy, supported by robust test design principles. This requires discipline, continuous communication, and both technical and cultural changes.</p><ul><li><strong>Lower-level tests are cheaper <br></strong>Teams must internalise that lower-level tests are cost-effective and provide faster feedback. These tests identify problems at the function/unit/component level, making them easier to fix. Adhering to the test pyramid helps maintain low-cost tests and reduces the cost of fixing defects.</li><li><strong>Code coverage is not insufficient on its own<br></strong>While important, code coverage is not the sole indicator of quality. Achieving 100% code coverage can miss functional behavior. Unit tests should be designed with a business-focused approach, requiring close collaboration between developers and quality engineers.</li><li><strong>Use reliable stubs <br></strong>A feedback loop for test stubs is often missing, leading to loss of confidence over time. Innovative stub solutions should mix static and dynamic stubbing strategies and include automated periodic validation. Contract testing can provide confidence in integration and reliable stubbing.</li><li><strong>Plan automation from the start<br></strong>Lack of planning in test automation leads to anti-patterns. Agile emphasises testing and learning, so we start with a plan and strong measurement capabilities from day one. Constant measurement helps diagnose and mitigate process problems, ensuring a robust, scalable, and reliable automation framework.</li><li><strong>Ensure traceability<br></strong>Automated tests should incorporate mechanisms to trace issues back to their source, either by combining application logs and test results or using unique identifiers for event tracking. This enhances observability and reduces analysis effort.</li></ul><h4><strong>Fostering Communication and Interaction</strong></h4><p>Agile emphasizes conversation and fast feedback loops, which should extend to tests. Integration problems often arise from the lack of automated interaction processes between parties. Consumer-Driven Contract testing bridges this gap by automatically sharing consumer expectations with providers, validating stubs, and preventing integration breakdowns.</p><h4><strong>Creating On-Demand Environments</strong></h4><p>Shared environments in development can be problematic due to several factors. When multiple teams or individuals use the same environment, changes made by one party can inadvertently affect others, leading to instability and unpredictable behavior. Version upgrades of software or dependencies can cause compatibility issues, disrupting workflows and causing failures in previously working systems. Additionally, maintaining data consistency becomes challenging, as incorrect or outdated data can be introduced, leading to unreliable test results. These issues can create bottlenecks, slow down development and testing cycles, and make it difficult to isolate and diagnose problems.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DEY3XmXOXA4AQY_QAHVHew.png" /><figcaption>Testing dependencies in shared environments can fail due to incorrect data, configuration or version upgrades</figcaption></figure><p>X-axis scaling emphasizes the importance of horizontal capability in microservice design. Modern microservice systems, often containerized, should automate deployment on new infrastructure, enabling on-demand environment creation and faster testing cycles.</p><blockquote>“A journey is a person in itself; no two are alike.” — John Steinbeck</blockquote><p>Perfection is a journey. Software architecture evolves rapidly, and maintaining quality with effective test automation design is challenging. Observability is key — <strong>watch, discover, act</strong>. This journey brings rewards, making the system as close to perfect as possible. At Emirates Group, we are committed to this journey, striving for excellence in our software quality. Happy testing!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=92d32df0f273" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Beyond Unit Testing: How Mutation Testing Helps to Improve Code Quality and Reliability]]></title>
            <link>https://medium.com/@nandan.abhi10/beyond-unit-testing-how-mutation-testing-helps-to-improve-code-quality-and-reliability-c8dc49737492?source=rss-9c9d8982ec2d------2</link>
            <guid isPermaLink="false">https://medium.com/p/c8dc49737492</guid>
            <category><![CDATA[unit-testing]]></category>
            <category><![CDATA[mutation-testing]]></category>
            <category><![CDATA[shiftleft]]></category>
            <category><![CDATA[continuous-delivery]]></category>
            <dc:creator><![CDATA[Abhi Nandan]]></dc:creator>
            <pubDate>Wed, 31 May 2023 03:31:43 GMT</pubDate>
            <atom:updated>2023-05-31T03:31:43.711Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*b0FGVBEAtPZm4LgHJJm-EA.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@photoholgic?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Photoholgic</a> on <a href="https://unsplash.com/photos/_y4LGVTeBwQ?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>Faster software delivery has become critical in today’s competitive business landscape. Organisations that can deliver high-quality software faster than their competitors gain a significant advantage.</p><blockquote>“We see continued evidence that software speed, stability, and availability contribute to organizational performance (including profitability, productivity, and customer satisfaction). Our highest performers are twice as likely to meet or exceed their organizational performance goals.” <a href="https://services.google.com/fh/files/misc/state-of-devops-2019.pdf">DORA, Accelerate: State of DevOps 2019</a></blockquote><p>By integrating automation testing into continuous delivery journeys, organisations can accelerate their software delivery, improve the quality of their code, and gain a competitive advantage. At the Emirates Group, we are highly focused on automating tests, and integrating it for faster developer feedback.</p><p>While automation testing is an essential tool for us to achieve faster software delivery, over time we have had to face the challenges that come with it. One of the biggest difficulties of having a highly automated test suite is ensuring the quality of tests and maintaining sufficient test coverage. With many automated tests, our engineers had to assess which tests were providing the most value and which tests were redundant or unnecessary.</p><h3>Unit testing — Key to high-quality software development</h3><p>Unit testing plays a critical role for us in achieving faster software delivery by allowing developers to catch bugs at the lowest level, early in the development process. To make sure there is enough coverage through unit tests, we had been measuring <strong>code coverage</strong> as the key metric and included the same checks in our continuous testing journeys to ensure minimum code coverage percentage. Over the years we observed code coverage is not enough on its own to ensure comprehensive testing. Code coverage simply measures <strong><em>the percentage of what has been executed by tests, without considering </em></strong>its <strong><em>effectiveness in identifying bugs. </em></strong>It is possible to have high code coverage but still miss critical defects in the code.</p><h3>Mutation testing comes to the rescue!</h3><p>Here’s a bit of history. According to <a href="https://en.wikipedia.org/wiki/Mutation_testing#cite_note-mutation2000-8">Wikipedia</a>, the concept of mutation testing was first introduced by Richard Lipton in the 1970s, and it has since gained popularity among software developers and testers. Mutation testing is a valuable tool for identifying the gaps in test coverage. It has grown in popularity lately due to the growing complexity of software systems, and the availability of new tools and frameworks that make it easier to implement.</p><h3>How Mutation Testing works</h3><p>The basic process of mutation testing involves generating minor changes, or mutations, to the source code of a program. These mutations are designed to simulate common programming errors, such as changing a logical operator from || to &amp;&amp; or swapping the order of two lines of code. The idea is to introduce defects into the code to see if the test cases can find them.</p><p>Once the mutations are introduced, the test suite is run against the mutated source code. If the test cases can identify the mutation, it is considered “<strong>killed</strong>”. If the mutation goes undetected, it is considered “<strong>survived</strong>”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XPqidbTYGvUHgg-kM82Kyg.png" /><figcaption>How Mutation Testing works</figcaption></figure><p><em>Here is an example to explain how mutation testing works. </em>Suppose we have the following code:</p><pre>def divide_(x,y):<br>  return x/y</pre><p>We also have a test suite containing one test case that checks that the function works correctly for valid input –</p><pre>def test_divide_():<br>  assert divide_(4,2) == 2</pre><p>If we introduce a mutation by changing the division operator to a addition operator, the code will look like:</p><pre>def divide_(x,y):<br>  return x+y</pre><p>If we run the test case against this mutated code, it should fail, as the expected result of dividing 4 by 2 is 2, not 6, which means the mutant is <strong>killed</strong>.</p><p>Consider another mutation by changing the division operator to a minus operator:</p><pre>def divide_(x,y):<br>  return x-y</pre><p>If we run the test case against this mutated code, it should pass, as the result of subtracting 2 from 4 is also 2, which means the mutant is <strong>survived</strong>.</p><p><strong>Mutation Score</strong> is the metric that shows the quality of a test suite. It is calculated as follows -</p><p>Mutation Score = (Killed mutants/Total mutants) x 100%, where -</p><p><strong><em>Killed Mutants</em></strong><em>: the number of mutants that were detected and killed by the test suite.</em></p><p><strong><em>Total mutants</em></strong><em>: the total number of mutants that were generated by the mutation testing tool.</em></p><h3>How Mutation Testing is helping us to write better code</h3><p>Introducing mutation testing in our continuous integration journey opened a huge potential for identifying the scope of improvement, fill in the testing gaps and measure the quality of the unit tests. The quantitative approach allows the developers to write better tests, which are more functionally focused. The engineers get more confidence in the code changes due to improved code quality.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ySvHTsdO_HDXiFAaE4IUqg.png" /><figcaption>Introducing mutation testing provided us with several benefits</figcaption></figure><p>While mutation testing helps us to get benefits of having more insight and work on top of it, it also comes with its own complexity. One of the greatest challenges we face is that this type of testing is <strong>computationally expensive</strong>. The process of running mutation testing on a complex and large repository can take many hours, which poses a challenge to add such testing to continuous integration journey. It also adds a complexity of <strong>overfitting</strong>. It can suffer from overfitting if the test suite is optimised to pass the specific mutants, rather than being representative of the functionality of the code. This can lead to <strong>false positives</strong>.</p><h3>The practical mutation testing approach</h3><p>While mutation testing is a great technique to improve the quality gaps, it is challenging to find out a balanced approach to bring out the most value by implementing it. Here is our approach to make the mutation testing effective –</p><ol><li>Selecting appropriate mutant operators,</li><li>Defining minimum mutation score,</li><li>Running mutation tests frequently</li><li>Prioritising detected mutants and</li><li>Using mutation testing <strong>to guide test cases development</strong>.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*sLm2VLa2ElZosYygXNO3mg.png" /><figcaption>Practical Mutation testing strategy</figcaption></figure><h3>Pitest — the mutation testing tool</h3><p>There are several mutation testing tools available in the market, each with its own pros and cons. Some mutation testing tools are designed for specific programming languages, while others are more versatile. <strong>No single mutation testing tool is perfect for all use cases</strong>. While choosing the right mutation testing tools, we considered factors such as ease of speed, use, and the range of mutations that the tool can detect. We decided to use <a href="https://pitest.org/">Pitest</a>, as it is highly aligned to the tech stack (Java Spring Boot) on which most of our middleware services are written.</p><h3>Configuring PIT with Spring Boot microservices –</h3><p>Setting up Pitest for Spring Boot based microservices is fairly straightforward. You need to include the Pitest plugin and dependencies to the pom.xml file –</p><pre>&lt;plugin&gt;<br>    &lt;groupId&gt;org.pitest&lt;/groupId&gt;<br>    &lt;artifactId&gt;pitest-maven&lt;/artifactId&gt;<br>    &lt;version&gt;LATEST&lt;/version&gt;<br>    &lt;configuration&gt;<br> &lt;targetClasses&gt;<br>     &lt;param&gt;com.example.service.**&lt;/param&gt;<br> &lt;/targetClasses&gt;<br> &lt;targetTests&gt;<br>     &lt;param&gt;com.example.service.**&lt;/param&gt;<br> &lt;/targetTests&gt;<br>    &lt;/configuration&gt;<br>&lt;/plugin&gt;</pre><p>To generate the Pitest report, you need to execute the following command –</p><pre>mvn clean test org.pitest:pitest-maven:mutationCoverage</pre><p>This will run all the unit tests and generate a mutation coverage report for the Spring Boot service. Once the command is executed, an HTML report should be generated under target/pit-reports.</p><p>To exclude certain test classes from Pitest analysis, all the exclusion classes can be added to &lt;excludedTestClasses&gt; configuration element to the configuration section of the Pitest plugin in the pom.xml file.</p><h3>Configuring mutants in Pitest</h3><p>When performing mutation testing, it is essential to focus on mutants that are likely to find out defects in the code. To configure the mutants in Pitest, a configuration file can be used that specifies the mutation operators, their parameters, and the classes and methods to be mutated. The configuration file should be in XML or YAML format and placed in the root directory of the project.</p><pre>mutators:<br>  - INCREMENTS<br>  - CONDITIONALS_BOUNDARY<br>  - NEGATE_CONDITIONALS<br>  - RETURN_VALS<br>  - VOID_METHOD_CALLS<br><br>targetClasses:<br>  - com.example.TargetClass<br><br>targetTests:<br>  - com.example.TargetTest</pre><p>To use this configuration file with Pitest, the path to the file needs to be specified in the Pitest plugin configuration in pom.xml –</p><pre>&lt;build&gt;<br>  &lt;plugins&gt;<br>    &lt;plugin&gt;<br>      &lt;groupId&gt;org.pitest&lt;/groupId&gt;<br>      &lt;artifactId&gt;pitest-maven&lt;/artifactId&gt;<br>      &lt;version&gt;LATEST&lt;/version&gt;<br>      &lt;configuration&gt;<br>        &lt;outputFormats&gt;<br>          &lt;format&gt;XML&lt;/format&gt;<br>          &lt;format&gt;HTML&lt;/format&gt;<br>        &lt;/outputFormats&gt;<br>        &lt;mutatorsFile&gt;pitest.yml&lt;/mutatorsFile&gt;<br>      &lt;/configuration&gt;<br>    &lt;/plugin&gt;<br>  &lt;/plugins&gt;<br>&lt;/build&gt;</pre><h3>Running mutation coverage on code changes</h3><p>scmMutationCoverage is a feature in Pitest that allows to calculate the percentage of mutations that have been covered by the tests based on the changes made to the codebase since the last commit. This feature is useful in measuring the effectiveness of the tests and identifying areas of the code that need more focus for testing. It also allows to configure the coverage threshold — a minimum threshold can be set for a project using the following configuration:</p><pre>&lt;build&gt;<br>  &lt;plugins&gt;<br>    &lt;plugin&gt;<br>      &lt;groupId&gt;org.pitest&lt;/groupId&gt;<br>      &lt;artifactId&gt;pitest-maven&lt;/artifactId&gt;<br>      &lt;version&gt;LATEST&lt;/version&gt;<br>      &lt;configuration&gt;<br>        &lt;scmMutationCoverage&gt;<br>          &lt;minimum&gt;85&lt;/minimum&gt;<br>        &lt;/scmMutationCoverage&gt;<br>      &lt;/configuration&gt;<br>    &lt;/plugin&gt;<br>  &lt;/plugins&gt;<br>&lt;/build&gt;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vrNzq_4Ab7Qt8exBqY-xVw.png" /><figcaption>Adding Mutation Tests in CI — with scmMutationCoverage on change on the codebase</figcaption></figure><p>In conclusion, mutation testing is a valuable tool for the teams looking to deliver more reliable, efficient software to the customers. By intentionally introducing small changes to the code and measuring the ability of unit test suites to detect the changes, mutation testing can help to identify faults in the code and optimise testing efforts. Ultimately, this can lead to faster software delivery, as bugs or other issues are caught early in the development process and addressed before they cause significant problems. I hope you enjoyed reading this blog. Happy testing!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c8dc49737492" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Testing Reactive Microservice in Spring Boot — Test Automation Design Patterns]]></title>
            <link>https://medium.com/@nandan.abhi10/testing-reactive-microservice-in-spring-boot-test-automation-design-patterns-cedd91c0c373?source=rss-9c9d8982ec2d------2</link>
            <guid isPermaLink="false">https://medium.com/p/cedd91c0c373</guid>
            <category><![CDATA[microservices]]></category>
            <category><![CDATA[automation-testing]]></category>
            <category><![CDATA[reactive-programming]]></category>
            <category><![CDATA[continuous-integration]]></category>
            <category><![CDATA[spring-boot]]></category>
            <dc:creator><![CDATA[Abhi Nandan]]></dc:creator>
            <pubDate>Tue, 22 Sep 2020 16:44:54 GMT</pubDate>
            <atom:updated>2020-09-22T16:44:54.210Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*P3WFK0x8rKzCuMClaaDGWw.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@umby?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Umberto</a> on <a href="https://unsplash.com/s/photos/inside-a-computer?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>In the <a href="https://medium.com/@nandan.abhi10/testing-reactive-microservice-in-spring-boot-integration-component-testing-bb3577f97d95">previous blog</a> in this series, we discussed Integration &amp; Component Tests, that meets most of the functional requirements. The volume of the tests could increase over time, leading the test suites to become ungovernable. It is important to own a fitting testing strategy, that can be sustainable over time. In this part, we will discuss a few indicative approaches for maintaining large test suites.</p><h3>It is NOT an easy task!</h3><p>Developing an efficient and reliable automated test suite can be extremely challenging. The microservice architecture patterns encourage designing small services, bound to address specific functionalities. <br>This pattern allows the developers to plan a small independent test suite for each microservice. However, a large number of microservices need to work together accurately to address a larger functional requirement. These interdependencies among the microservices impose high complexity in designing efficient automated test suites.</p><h4>Reliance on Test Doubles</h4><p>Component Testing advocates investigating microservices in isolation. This is essential in developing efficient test suites. It also adds complexity as any external dependencies need to be stubbed. The dependencies can either be a persistence layer or a downstream service. It could be another microservice as well.</p><p>Test doubles often result in inefficiencies in the test maintenance, as any change in the interacting services could result in hundreds of test failures. In addition to this, covering different functional requirement might lead to creating several mocks for any interacting entity. If the stubs are maintained statically, it could lead to managing several of those. Any change in the contract could be a nightmare from a maintenance perspective.</p><h4><strong>To Adapt BDD or Not!</strong></h4><p>Well, this is a debatable topic, and there are different opinions among the developers about the same! Some prefer to adapt Gherkin for writing the functional tests, while some prefer to use core concepts behind it (<em>given, when, then</em>) without introducing the gherkin layer. While it certainly depends a lot on the context, one thing that is almost unanimously agreed, that at a high level an automated test should be —</p><ul><li>Readable,</li><li>Data-Driven, and</li><li>Fluent in design</li></ul><h4><strong>Where to start and where to end</strong></h4><p>One of the biggest challenges in a layered testing approach is figuring out the scope of tests in a particular phase and utilize it wisely. There is always a risk of repeating the same tests in different layers, without adding any value. The key to managing an efficient test suite is highly dependent on good test design.</p><h3>Bringing it Back to the Context</h3><p>While there are a lot of hurdles in designing and managing an efficient test automation suite, a few careful design choices can make life easier. We are discussing Spring Boot Reactive microservices in this context and let us see what are a few tools and designs we can use to write effective tests.</p><h4>JUnit 5 — the ultimate choice as test framework</h4><p><a href="https://junit.org/junit5/docs/current/user-guide/">JUnit 5</a> has introduced a lot of interesting features, that can help in managing large test suites. Some of these are —</p><p><strong>Parameterized Test — </strong>ParameterizedTest is extremely useful in data-driven test design. It comes up with several options like ValueSource, MethodSource, EnumSource, NullSource, EmptySource etc. In the following code snippets, you can find the usage of it -</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b39f8cec8bdfdba9cf81361f1c17fc57/href">https://medium.com/media/b39f8cec8bdfdba9cf81361f1c17fc57/href</a></iframe><p>The above example shows the use of ValueSource and NullAndEmptySource. MethodSource allows passing more than one argument to a test -</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/802a59eb6c1c51da083acc9c43a0a06e/href">https://medium.com/media/802a59eb6c1c51da083acc9c43a0a06e/href</a></iframe><p><strong>Group Assertion </strong>— One more key feature to the Junit 5 is Assertions.<em>assertAll</em>. It allows in having single failure with all the failed asserts. Below is an example of it -</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/29e40417ab0b251946ee2796f1b1652e/href">https://medium.com/media/29e40417ab0b251946ee2796f1b1652e/href</a></iframe><p>When this test is executed, it generates a result with all failed assertion points-</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*soqxquvpYCy-_XbNZs7Czg.png" /><figcaption>Multiple Assertions, Single Failure</figcaption></figure><h4>WireMock — API Stubbing</h4><p><a href="http://wiremock.org/">WireMock</a> is a popular API mocking tool for fast, robust, comprehensive testing. Writing test stubs using WireMock is relatively simple, however, managing the test stubs could be tricky. There are 2 approaches in the use of test stubs -</p><ul><li><strong>static stubbing</strong> — the test doubles are created from static content, generally stored in files. This type of stubbing technique is easy to manage when there are relatively fewer mocks needs to be used.</li><li><strong>dynamic stubbing </strong>— the test doubles are created from dynamic content, which is generated at the runtime by programming. This type of stubbing techniques are relatively complex and reduces the readability of the tests. This can be useful when there are a high number of test doubles need to be managed.</li></ul><p>Both the stubbing techniques has its pros and cons, and stubbing strategy is highly dependent on context. For most of the cases, it is wise to use a <strong><em>hybrid approach</em></strong> to minimize the use of static mock response files, so that it is relatively easy to manage in a long run.</p><h4><strong>Wait, is it reliable to test against the mocks?</strong></h4><p>High reliance on test doubles allows designing robust tests, however, it also imposes a need for validating the authenticity of the mocks. <strong><em>Contract Testing</em> </strong>can play a pivotal role here<strong>. </strong>There are some awesome tools like Atlassian provided open-source <a href="https://bitbucket.org/atlassian/wiremock-pact-generator/src/master/">wiremock-pact-generator</a>, that can come handy to generate pacts for the WireMock stubs, and subsequently, get validated against the provider services.</p><h4><strong>Mocking Persistence Layer</strong></h4><p>Mocking persistence layer using <a href="https://site.mockito.org/">Mockito</a> or similar tools could become complex for Component Tests, as these tests simulate the end to end scenarios. A single test can interact with the persistence layer for several times. Using in-memory database can eliminate the overhead of mocking. Spring Boot supports several in-memory SQL databases like <a href="https://www.h2database.com/html/main.html">H2</a>, <a href="http://hsqldb.org/web/hsqlDocsFrame.html">HSQLDB</a>, and <a href="http://db.apache.org/derby/docs/10.9/devguide/cdevdvlpinmemdb.html">Derby</a>. There are also some available <a href="https://github.com/flapdoodle-oss/de.flapdoodle.embed.mongo">embedded solutions</a> for NoSQL databases like MongoDB.</p><h4><strong>Designing Tests</strong></h4><p>Test design plays an important role in managing large test suites. A test must be readable at first glance. <a href="https://datacadamia.com/code/design_pattern/fluent_interface">Method chaining</a> provides great readability of the tests at a high level. A clean test might look like -</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/30857ba1ed4a4f9b74e29fc6a9c1ea1a/href">https://medium.com/media/30857ba1ed4a4f9b74e29fc6a9c1ea1a/href</a></iframe><p>The above test imitates BDD pattern, using plain Java. Steps can further manage the underlying functionalities for carrying out the tests. A large test suite might introduce lots of complexity in test maintenance in long run. This might necessitate the implementation of advanced design patterns.</p><h3>Conclusion</h3><p>Designing an effective test automation suite can be profoundly challenging. It needs technical competence and a disciplined approach to test design. It also demands high collaboration between the developers and the quality engineers. In the next blog post, we will discuss on the Contract Testing Strategy for Reactive Spring Boot Microservices. Stay tuned, happy testing!</p><h3>A Complete Example</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6ba0e2d5af88ca9b58085028eb67b47a/href">https://medium.com/media/6ba0e2d5af88ca9b58085028eb67b47a/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/00a1cd44399f874c8c99d293bc4ca1df/href">https://medium.com/media/00a1cd44399f874c8c99d293bc4ca1df/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/50b96628bc09cfe1f02533ec396bbdb3/href">https://medium.com/media/50b96628bc09cfe1f02533ec396bbdb3/href</a></iframe><p>The complete code can be found at — <a href="https://github.com/Dev2AtWork/spring-webflux-reactive-rest-api-demo">https://github.com/Dev2AtWork/spring-webflux-reactive-rest-api-demo</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cedd91c0c373" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Testing Reactive Microservice in Spring Boot — Integration & Component Testing]]></title>
            <link>https://medium.com/@nandan.abhi10/testing-reactive-microservice-in-spring-boot-integration-component-testing-bb3577f97d95?source=rss-9c9d8982ec2d------2</link>
            <guid isPermaLink="false">https://medium.com/p/bb3577f97d95</guid>
            <category><![CDATA[automation-testing]]></category>
            <category><![CDATA[continuous-delivery]]></category>
            <category><![CDATA[reactive-programming]]></category>
            <category><![CDATA[spring]]></category>
            <category><![CDATA[microservices]]></category>
            <dc:creator><![CDATA[Abhi Nandan]]></dc:creator>
            <pubDate>Fri, 17 Jul 2020 14:12:26 GMT</pubDate>
            <atom:updated>2020-09-23T03:36:06.840Z</atom:updated>
            <content:encoded><![CDATA[<h3>Testing Reactive Microservice in Spring Boot — Integration &amp; Component Testing</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Fb_kzx3gjKBmsIwavOVCvQ.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@_louisreed?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Louis Reed</a> on <a href="https://unsplash.com/s/photos/component?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>In the <a href="https://medium.com/@nandan.abhi10/testing-reactive-microservice-in-spring-boot-unit-testing-fe453887ffa1">previous blog</a>, we discussed some of the Unit Testing approaches Spring Boot Reactive stack using JUnit. Till this point, we have achieved the first phase of our Continuous Testing journey. In this part, we are going to discuss the next two phases -</p><ul><li>Integration Testing, and</li><li>Component Testing</li></ul><p>So, let’s get started!</p><h4><strong>Why do you need Integration &amp; Component Testing?</strong></h4><p>Unit testing takes care of validating the implementation of the functional blocks (unit), however, it does not provide guarantee about the behavior of the system. To verify that each module correctly collaborates with its interacting modules more coarse-grained testing is required.</p><p><strong>Defining Integration Tests</strong></p><p>As <a href="https://martinfowler.com/articles/microservice-testing/#testing-integration-introduction">Martin Fowler defines</a>, “<em>An integration test verifies the communication paths and interactions between components to detect interface defects”</em>. In microservice architectures, integration tests are typically used to <strong>verify interactions between layers of integration code and the external components to which they are integrating</strong>.</p><p>An example of an integration test in our Reactive Microservice context could be calling the Application Router function and validate the integration between the different blocks (handler, processor etc.), and the external data source. Integration with data stores and external components provides early feedback on the system behavior.</p><p><strong>Defining Component Tests</strong></p><p>While the integration tests validate the interactions of the external modules &amp; persistence layer (data-store), however, these tests do not take care of the business logic validations. The Component Tests are responsible <strong>to validate the business logic in the microservices</strong>. Although this can be achieved by the end-to-end system integration tests, SIT tests are slow and highly dependent on the external systems. The primary objective of the Component Tests is examining the system behavior in isolation. Separating the components from each other provides the advantage of thoroughly acceptance test the behavior of the component.</p><h4><strong>The thin line between Integration &amp; Component Testing</strong></h4><p>While we will be discussing more around the implementation of the two testing phases, you’ll find that the implementation principles are very similar, however, it needs careful test designs to avoid duplication. <strong><em>While the integration tests focus more on validation of the integration interface code with the persistent layer and external systems, the component tests are focused towards the business functionality validations and its different variations.</em></strong></p><p>Let us illustrate this by the following example.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YCxT6aR5X96PjRbj3iEkXQ.png" /></figure><p>A payment microservice interacts with a third-party payment gateway. The third-party service contract provides two different types of response structures (<em>success and error response schema</em>). In this case, the integration test should be validating the behavior of the system with one success and one error response, but when it comes to the component tests, the number of test cases will vary depending on the business logic that applies to different types of successful or failure responses.</p><p><strong>Tools</strong></p><p>In this series, we are discussing the Spring-boot 5 reactor tech stack, that supports reactive programming. The integration &amp; component test phases require the following features from the test automation framework perspective -</p><ul><li>Test Framework — <a href="https://junit.org/junit5/docs/current/user-guide/">JUnit 5</a></li><li>Reactive Rest Client — <a href="https://docs.spring.io/spring/docs/current/spring-framework-reference/pdf/testing-webtestclient.pdf">WebTestClient</a></li><li>Mocking framework — Mockito</li><li>Stubbing framework — WireMock</li></ul><h3>Designing Integration Tests</h3><p>Let’s consider that there’s a <strong>DatabaseService</strong> class that implements the persistence layer. It also exposes a method called <em>findById(id),</em> that returns the document from the datastore. The following is an example of a MockBean injection to mimic the persistence layer in your test.</p><pre>public class IntegrationTests {</pre><pre>@MockBean<br>    private DatabaseService databaseService;</pre><pre>@BeforeEach<br>    void mockCouch() {<br>        when(databaseService.findById(any()))<br>            .thenReturn(Mono.just(JsonDocument.create(&quot;Document_key&quot;,<br>                JsonObject.fromJson(&quot;{your_json_response}&quot;))));<br>    }<br>}</pre><p><strong>Time to write the Integration Test!</strong></p><p>To write an integration test, we will need a WebTestClient, to call the microservice. In this example test, we’ll call the microservice Router Function and validate that the response is received as OK. Here is how we can write the test -</p><pre>@Test<br>@DisplayName(&quot;Should Get Success on valid request&quot;)<br>public void validRequestIntegrationTest() {<br>    <br>    WebTestClient<br>        .bindToRouterFunction(<br>            applicationRouter.mainRouterFunction(applicationHandler, errorHandler, serviceLogger))<br>        .build()<br>        .post()<br>        .uri(&quot;/base-path&quot;)<br>        .accept(MediaType.APPLICATION_JSON)<br>        .contentType(MediaType.APPLICATION_JSON)<br>        .exchange()<br>        .expectStatus()<br>        .isOk()<br>        .expectBody(ApplicationResponseDTO.class)<br>        .consumeWith(serverResponse -&gt;<br>            assertNotNull(serverResponse.getResponseBody()));<br>}</pre><p>Let’s go through this in details. This integration test calls the application router function of the microservice under test and validates the response received. For calling the service, a WebTestClient is instantiated with <em>bindToRouterFunction</em> method. This will create the POST request to the service and get the response back from the application. The response received is parsed to get the response body and validate further. This test achieves the validation of the integration interfaces with the datastore.</p><h4><strong>Last bit of the Integration Test!</strong></h4><p>To instantiate the ApplicationRouter class, the microservice application needs to be initialized. @SpringBootTest annotation can do the magic here!. So finally, the test looks like -</p><pre>@SpringBootTest(classes = Application.class)<br>@ExtendWith(SpringExtension.class)<br>@ActiveProfiles(&quot;integration&quot;)<br>public class IntegrationTests {</pre><pre>@Autowired private ApplicationRouter applicationRouter;</pre><pre>@Autowired private ApplicationHandler applicationHandler;</pre><pre>@Autowired private ErrorHandler errorHandler;</pre><pre>@Autowired private ServiceLogger serviceLogger;</pre><pre>@MockBean private DbService dbService;</pre><pre>@BeforeEach<br>    void mockCouch() {<br>        when(dbService.findById(any()))<br>            .thenReturn(Mono.just(JsonDocument.create(&quot;Document_key&quot;,<br>                JsonObject.fromJson(&quot;{your_json_response}&quot;))));<br>    }</pre><pre>@Test<br>    @DisplayName(&quot;Should Get Success on valid request&quot;)<br>    public void validRequestIntegrationTest() {<br>        <br>        WebTestClient<br>            .bindToRouterFunction(<br>                applicationRouter.mainRouterFunction(applicationHandler, errorHandler, serviceLogger))<br>            .build()<br>            .post()<br>            .uri(&quot;/base-path&quot;)<br>            .accept(MediaType.APPLICATION_JSON)<br>            .contentType(MediaType.APPLICATION_JSON)<br>            .exchange()<br>            .expectStatus()<br>            .isOk()<br>            .expectBody(ApplicationResponseDTO.class)<br>            .consumeWith(serverResponse -&gt;<br>                assertNotNull(serverResponse.getResponseBody()));<br>    }<br>}</pre><h3>Designing Component Tests</h3><p>The test design principles of the Component Test is already discussed, and in this phase of testing the primary objective is validating the business logic of the microservice in isolation. The toolset for Component Test remains more or less the same. In this section, we will also touch upon the concepts of WireMock for stubbing.</p><p>The approach of writing the tests and design principles are highly subjective and depends largely on the context. The Component Tests can be implemented in the same way as the Integration Tests by design principle. We can also configure WebTestClient automatically to bind the application context and run the tests. This would provide more of a black-box approach to the testing, where the component under test is fed with the input data and response is functionally validated. The component testing phase is <strong>business-critical, as the objective is more functional.</strong></p><h4>Associating Profiles with the testing phase</h4><p><em>Profile</em> is one of the key features of the Spring framework, and it allows us to map beans to the different profiles (<em>in loose terms can be called as environments as well</em>). This feature enables us to create a component test profile that can have the configuration to stub all the external systems. This will provide us with the advantage of isolating our system under test from any external dependency.</p><h4>Time to write Component Tests</h4><p>A component test can have a lot of prerequisites. This can include</p><ol><li>Starting Spring Application</li><li>Registering Junit 5 extensions</li><li>Configuring WebTestClient</li><li>Configuring WireMock</li></ol><p>We can create an annotation to complete all the above mentioned tasks before the beginning of the test. This can look like -</p><pre>@Target({ ElementType.TYPE, ElementType.METHOD })<br>@Retention(RetentionPolicy.RUNTIME)<br>@Tag(&quot;ComponentTest&quot;)<br>@ExtendWith(SpringExtension.class)<br>@SpringBootTest(classes = Application.class)<br>@ActiveProfiles(&quot;component&quot;)<br>@AutoConfigureWebTestClient(timeout = &quot;30000&quot;)<br>@TestInstance(TestInstance.Lifecycle.PER_CLASS)<br>@AutoConfigureWireMock(port = 9900)<br>public @interface ComponentTest {<br>}</pre><p>So let’s write a very simple component test using the <strong><em>@ComponentTest</em></strong> annotation -</p><pre>@ComponentTest<br>public class ComponentTests {<br>    @Autowired WebTestClient webTestClient;<br><br>    @BeforeAll<br>    void initStubs() {<br>        WireMock.<em>stubFor</em>(<em>post</em>(&quot;/downstream/system&quot;)<br>            .willReturn(<em>aResponse</em>()<br>                .withStatus(200)<br>                .withHeader(&quot;Content-Type&quot;, MediaType.<em>APPLICATION_JSON_VALUE</em>)<br>                .withBody(&quot;{\&quot;status\&quot;: \&quot;success\&quot;}&quot;)<br>                .withBodyFile(&quot;ping-response.json&quot;)<br>            ));<br>    }<br><br>    @Test<br>    void shouldGetSuccessOnValidCalls() {<br>        MultiValueMap&lt;String, String&gt; headers = new HttpHeaders();<br>        headers.add(HttpHeaders.<em>CONTENT_TYPE</em>, MediaType.<em>APPLICATION_JSON_VALUE</em>);<br>        headers.add(HttpHeaders.<em>ACCEPT</em>, MediaType.<em>APPLICATION_JSON_VALUE</em>);<br><br>        WebTestClient.ResponseSpec response = webTestClient<br>            .post()<br>            .headers(httpHeaders -&gt; httpHeaders.addAll(headers))<br>            .body(BodyInserters.<em>fromValue</em>(&quot;{\&quot;key\&quot;:\&quot;value\&quot;}&quot;))<br>            .exchange();<br>        response<br>            .expectStatus()<br>            .isEqualTo(HttpStatus.<em>OK</em>)<br>            .expectBody(ApplicationResponseDTO.class)<br>            .consumeWith(serverResponse -&gt; {<br>                ApplicationResponseDTO responseBody = serverResponse.getResponseBody();<br>                //all assertions go here<br><br>            });<br><br>    }<br>}</pre><p>So what’s happening in this test? This test first creates a stub for the external system in the <em>@BeforeAll </em>method, by running the same on port 9900 (<em>as defined in the annotation</em>). The <em>ComponentTests</em> class is a Junit class as our custom annotation <em>@ComponentTest </em>takes care of all the prerequisites. The test takes a simple payload as input and sends a request with all required headers and validates the response on execution. This test treats our component as black-box and validates the business requirements.</p><p>In this blog, we talked about integration and component test design principles. One of the key qualities behind the success of Continuous Testing is well-designed tests. Integration &amp; Component tests allow you to identify the defects in the early part of the development cycle. These two testing phases also play an important role in reducing test execution time. In the <a href="https://medium.com/@nandan.abhi10/testing-reactive-microservice-in-spring-boot-test-automation-design-patterns-cedd91c0c373">next article</a>, we will discuss some of the sample design patterns for maintaining large test suites. Stay tuned &amp; Happy testing!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bb3577f97d95" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Testing Reactive Microservice in Spring Boot — Unit Testing]]></title>
            <link>https://medium.com/@nandan.abhi10/testing-reactive-microservice-in-spring-boot-unit-testing-fe453887ffa1?source=rss-9c9d8982ec2d------2</link>
            <guid isPermaLink="false">https://medium.com/p/fe453887ffa1</guid>
            <category><![CDATA[reactive-programming]]></category>
            <category><![CDATA[microservices]]></category>
            <category><![CDATA[automation-testing]]></category>
            <category><![CDATA[unit-testing]]></category>
            <category><![CDATA[reactor]]></category>
            <dc:creator><![CDATA[Abhi Nandan]]></dc:creator>
            <pubDate>Fri, 03 Jul 2020 14:58:13 GMT</pubDate>
            <atom:updated>2020-07-17T14:13:16.790Z</atom:updated>
            <content:encoded><![CDATA[<h3>Testing Reactive Microservice in Spring Boot — Unit Testing</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZMnzmYK24GRGcv9w0A2smg.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@iurte?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Iker Urteaga</a> on <a href="https://unsplash.com/s/photos/block?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>In the <a href="https://medium.com/@nandan.abhi10/testing-reactive-microservice-in-spring-boot-understanding-reactive-choosing-test-stack-583310ebcfb5">previous blog</a> in this series, we discussed the ideas and basic principles of the reactive programming, reactive system &amp; Spring boot 5 WebFlux. We also discussed various phases of the testing lifecycle and tech stack that could be used across. In this part, I will discuss the following-</p><ul><li>Understanding Unit Testing,</li><li>Testing Reactive Streams Using Reactor 3 library,</li><li>Mocking.</li><li>Examples of unit tests in Reactive microservice</li></ul><p>So, let’s get started!</p><h4>Understanding Unit Testing</h4><p>Unit Testing is the exercise of validating the smallest piece of software to determine if it is behaving as expected. Designing unit tests accurately is core to the design of good software. The units to test should have <strong>well-defined boundaries.</strong> The definition of the unit is highly subjective and largely dependent on the application architecture. In an object-oriented world, a unit could be defined as a class, whereas functions could be considered as a unit in a procedural or functional approach.</p><p>Martin Fowler in this <a href="https://martinfowler.com/bliki/UnitTest.html">blog</a> has classified unit tests in 2 distinct patterns — <strong>Sociable unit tests</strong> &amp; <strong>Solitary unit tests</strong>. The names are pretty much self-explanatory. In most of the cases, unit tests are designed to test the units in isolation, where any interaction with another class/function is reliant on the Test Doubles. However, there could be tests, which might interact with its neighboring class/function. The core of the idea remains in the definition of the unit. There is no thumb rule as such to define a unit and it can vary. As a good test design practice, it is important to have a combination of both types of tests.</p><h4>Testing Reactive Streams Using Reactor 3 library</h4><p>As we had discussed in the previous <a href="https://medium.com/@nandan.abhi10/testing-reactive-microservice-in-spring-boot-understanding-reactive-choosing-test-stack-583310ebcfb5">blog</a>, Reactive Programming is a paradigm that enables to build applications in a non-blocking asynchronous way. We had also discussed two different data types named Flux &amp; Mono that come from the <a href="https://github.com/reactor/reactor-core">Reactor Core</a> Java 8 library. Let’s discuss a few of the functionalities of Flux &amp; Mono that could be useful to write unit tests.</p><p>Flux is a data type that can emit 0…N items &amp; then completes. Below are a few examples of how different types of Flux can be created –</p><p>To create an empty Flux –</p><pre>Flux.empty();</pre><p>Below is an example of a flux that emits one item –</p><pre>Flux&lt;String&gt; flux = Flux.just(“Sample String”);<br>Flux.subscribe(System.out::println); //prints Sample String</pre><p>Flux can also be created from Streams using <em>Flux.fromStream()</em> or from iterable (list, map etc.) using <em>Flux.fromIterable()</em>.</p><p>You can also throw error from a flux like –</p><pre>Flux&lt;String&gt; flux = Flux.error(new RuntimeException(&quot;Error Thrown&quot;));</pre><p>Flux also provides the option to create more complex functional operations. Below is an example that provides the square of the even numbers from a Flux of numbers –</p><pre>Flux&lt;Double&gt; flux = Flux<br>    .just(1,23,22,35,44)<br>    .filter(integer -&gt; integer % 2 == 0)<br>    .map(integer -&gt; (Math.pow(integer, 2)));</pre><p>Mono is one more data type, but unlike Flux, it can emit 0..1 items. Mono also provides two similar methods — <em>Mono.just()</em> &amp; <em>Mono.error()</em>.</p><p><a href="https://projectreactor.io/docs/core/release/reference/#testing">Reactor Test</a> is a library provided for testing the reactive streams. Reactor-test library can be imported to your project using the maven dependency –</p><pre>&lt;dependency&gt;<br> &lt;groupId&gt;io.projectreactor&lt;/groupId&gt;<br> &lt;artifactId&gt;reactor-test&lt;/artifactId&gt;<br> &lt;version&gt;3.3.7.RELEASE&lt;/version&gt;<br> &lt;scope&gt;test&lt;/scope&gt;<br>&lt;/dependency&gt;</pre><p>2 of the key elements in reactor tests are StepVerifier &amp; TestPublisher.</p><h4>StepVerifier</h4><p>StepVerifier provides <strong><em>a declarative way of creating verifiable steps for async publisher sequence by expressing expectations about the set of events that will eventually happen upon subscription.</em></strong> Below is an example –</p><pre>Flux&lt;String&gt; flux = Flux.fromIterable(Arrays.asList(&quot;spring 5&quot;, &quot;reactor&quot;, &quot;unit test&quot;, &quot;reactive programming&quot;));<br>StepVerifier.create(flux)<br> .expectNext(&quot;spring 5&quot;)//matches the exact value<br> .expectNext(&quot;reactor&quot;, &quot;unit test&quot;) //matches multiple value<br> .expectNextMatches(str -&gt; str.contains(&quot;programming&quot;)) //predicate<br> .verifyComplete();</pre><p>Here is another example of StepVerifier with Flux error –</p><pre>Flux&lt;String&gt; flux = Flux.error(new IOException(&quot;Error thrown&quot;));<br>StepVerifier<br> .create(flux)<br> .expectErrorMatches(throwable -&gt; throwable instanceof IOException)<br> .verify();</pre><h4>TestPublisher</h4><p>TestPublisher works <strong><em>as a publisher that can be directly manipulated by triggering onNext(), onComplete() or onError() events</em></strong>. TestPublisher can be used along with StepVerifier for testing purposes. Below is an example of a TestPublisher with StepVerifier -</p><pre>public class Converter {<br>    private final Flux&lt;String&gt; source;</pre><pre>Converter(Flux&lt;String&gt; source) {<br>        this.source = source;<br>    }</pre><pre>Flux&lt;String&gt; getConverted() {<br>        return source<br>            .map(str -&gt; str.replace(&quot; &quot;, &quot;_&quot;));<br>    }<br>}</pre><pre>@Test<br>void fluxTest1(){<br>    final TestPublisher&lt;String&gt; testPublisher = TestPublisher.create();<br>    Converter converter = new Converter(testPublisher.flux());<br>    StepVerifier<br>        .create(converter.getConverted())<br>        .then(() -&gt; testPublisher.emit(&quot;Spring 5&quot;, &quot;Reactive Programming&quot;, &quot;Reactor Test&quot;))<br>        .expectNext(&quot;Spring_5&quot;, &quot;Reactive_Programming&quot;, &quot;Reactor_Test&quot;)<br>        .verifyComplete();<br>}</pre><p>In the above example, Converter class is tested using a TestPublisher &amp; StepVerifier.</p><h4>Mocking</h4><p>Mocking or use of test double is one of the essential attributes to a good unit test design. While there are several tools available for mocking, we are going to discuss one of the most popular libraries <a href="https://github.com/mockito/mockito">Mockito</a> in this context.</p><p>Mockito allows dependency injection by inserting Mockito mocks in Spring Beans for unit testing. Mockito-core can be configured using the below maven dependency –</p><pre>&lt;dependency&gt;<br>    &lt;groupId&gt;org.mockito&lt;/groupId&gt;<br>    &lt;artifactId&gt;mockito-core&lt;/artifactId&gt;<br>    &lt;version&gt;3.3.3&lt;/version&gt;<br>    &lt;scope&gt;test&lt;/scope&gt;<br>&lt;/dependency&gt;</pre><p>Reactor Publishers (Flux &amp; Mono) can also be mocked using Mockito . Below is an example of the same –</p><pre>@BeforeAll<br>void init() {<br>    ServerRequest.Headers headers = mock(ServerRequest.Headers.class);<br>    ServerRequest serverRequest = mock(ServerRequest.class);</pre><pre>doReturn(headers)<br>        .when(serverRequest)<br>        .headers();</pre><pre>doReturn(new HttpHeaders())<br>        .when(headers)<br>        .asHttpHeaders();</pre><pre>doReturn(Mono.just(&quot;{\&quot;key\&quot;: \&quot;value\&quot;}&quot;))<br>        .when(serverRequest)<br>        .body(any());</pre><pre>}</pre><p>The above one is an example of mocking, where the Mockito stubber returns a Mono of response object on request. Here is an example of a sample <em>Handler Function</em> test using Mockito -</p><pre>public class ServiceHandlerTest {<br><br>    private static final String <em>RESPONSE_STRING </em>= &quot;{\&quot;key\&quot;: \&quot;value\&quot;}&quot;;<br><br>    private static final Supplier&lt;HttpHeaders&gt; <em>HTTP_HEADERS </em>= () -&gt; {<br>        HttpHeaders headers = new HttpHeaders();<br>        headers.setContentType(MediaType.<em>APPLICATION_STREAM_JSON</em>);<br>        headers.setAccept(Collections.<em>singletonList</em>(MediaType.<em>APPLICATION_STREAM_JSON</em>));<br>        return headers;<br>    };<br><br>    private ServiceHandler serviceHandler;<br>    private ServerRequest  serverRequest;<br><br>    @BeforeAll<br>    void init() {<br>        ServerRequest.Headers headers = <em>mock</em>(ServerRequest.Headers.class);<br>        ServiceProcessor serviceProcessor = <em>mock</em>(ServiceProcessor.class);<br>        serverRequest = <em>mock</em>(ServerRequest.class);<br><br>        <em>doReturn</em>(headers)<br>            .when(serverRequest)<br>            .headers();<br><br>        <em>doReturn</em>(<em>HTTP_HEADERS</em>.get())<br>            .when(headers)<br>            .asHttpHeaders();<br><br>        <em>doReturn</em>(Mono.<em>just</em>(<em>RESPONSE_STRING</em>))<br>            .when(serverRequest)<br>            .body(<em>any</em>());<br><br>        serviceHandler = new ServiceHandler(serviceProcessor);<br>    }<br><br>    @Test<br>    void shouldProcessRequest() {<br>        StepVerifier<br>            .<em>create</em>(serviceHandler.processRequest(serverRequest))<br>            .assertNext(Assertions::<em>assertNotNull</em>)<br>            .verifyComplete();<br><br>    }<br>}</pre><p>To summarize, in this blog we discussed some approaches of writing unit tests for Reactive microservices and unit test design patterns. One of the key qualities behind the success of Continuous Testing is well-designed tests. While code coverage provides us with certain insights, it requires a deeper understanding of the objectives and clarity on the bigger picture. In the <a href="https://medium.com/@nandan.abhi10/testing-reactive-microservice-in-spring-boot-integration-component-testing-bb3577f97d95">next article</a> in this series, I will discuss on the Integration and Component Tests — the next phases in our Continuous Testing journey. Stay tuned!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fe453887ffa1" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Testing Reactive Microservice in Spring Boot — Understanding Reactive & Choosing Test Stack]]></title>
            <link>https://medium.com/@nandan.abhi10/testing-reactive-microservice-in-spring-boot-understanding-reactive-choosing-test-stack-583310ebcfb5?source=rss-9c9d8982ec2d------2</link>
            <guid isPermaLink="false">https://medium.com/p/583310ebcfb5</guid>
            <category><![CDATA[reactive-programming]]></category>
            <category><![CDATA[microservices]]></category>
            <category><![CDATA[webflux]]></category>
            <category><![CDATA[spring-framework]]></category>
            <category><![CDATA[automation-testing]]></category>
            <dc:creator><![CDATA[Abhi Nandan]]></dc:creator>
            <pubDate>Fri, 26 Jun 2020 15:13:02 GMT</pubDate>
            <atom:updated>2020-07-03T15:00:34.837Z</atom:updated>
            <content:encoded><![CDATA[<h3>Testing Reactive Microservice in Spring Boot — Understanding Reactive &amp; Choosing Test Stack</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rjwqpt9b05ZZJkZjHUq6GQ.png" /></figure><p>Programming models are evolving fast with the introduction of the new style of programming and design patterns. This blog is an attempt to provide insights on Testing Strategy of Reactive Microservice Design pattern. In this part of the blog, I will take you through the basic concepts behind</p><ul><li>Microservice architecture,</li><li>Reactive patterns in different context, and finally</li><li>Choice of tech stack for testing microservices developed by following reactive programming paradigm.</li></ul><h3>The Microservice Revolution</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-Ux_3QVIw5j5Gk_W7TE_0A.jpeg" /><figcaption>Image Courtesy freepik</figcaption></figure><p>The shift from the huge monolith to more focused microservice-based architecture is now an industry-wide accepted pattern. With the growth in the adaptation of cloud computing, the microservice architecture pattern is getting increased popularity. According to <a href="https://tsh.io/state-of-microservices/">The Software House State of Microservices 2020</a> report, software developers around the world have a high acceptance of microservice pattern, specifically when it comes to the scalability &amp; performance aspect.</p><h3>The Reactive Manifesto</h3><p>Along with the popularity of the microservice pattern, in 2014, Jonas Bonér, Dave Farley, Roland Kuhn, and Martin Thompson authored <a href="https://www.reactivemanifesto.org/">Reactive manifesto</a>, to come up with a coherent design pattern for building software that is robust, resilient &amp; flexible. The Reactive manifesto states that Reactive Systems should be</p><ul><li>Responsive</li><li>Resilient</li><li>Elastic</li><li>Message-Driven</li></ul><h4><strong>The story of “Reactive”</strong></h4><p>In the software development world, the word “reactive” is used primarily in 3 different context –</p><ul><li>Reactive Systems (design &amp; architecture)</li><li>Reactive Programming (declarative event-based)</li><li>Functional reactive programming (FRP)</li></ul><p><strong>What is Reactive System?</strong></p><p>A reactive system is an architectural pattern that allows multiple individual applications to cohere as a single unit, reacting to its surroundings, while remaining aware of each other. This could manifest as scaling up/down, load balancing, and even taking some of these steps proactively.</p><p><strong>What is Reactive Programming?</strong></p><p>According to <a href="https://en.wikipedia.org/wiki/Reactive_programming">Wikipedia</a> “<em>In computing, reactive programming is a declarative programming paradigm concerned with data streams and the propagation of change. With this paradigm it is possible to express static (e.g., arrays) or dynamic (e.g., event emitters) data streams with ease, and also communicate that an inferred dependency within the associated execution model exists, which facilitates the automatic propagation of the changed data flow.</em>”</p><p>In an oversimplified way, reactive programming is the form of how one writes code. One does not assume that the method call will return the response, but will need to provide a callback, future object, or a promise that in the future response will be passed to this structure to continue the problem flow.</p><p><strong>What is Functional Reactive Programming?</strong></p><p>The term Functional Reactive Programming was coined by Conal Elliot in his Fran tutorial from 1998. The summary has been well explained by him on this <a href="https://stackoverflow.com/questions/1028250/what-is-functional-reactive-programming/1030631#1030631">answer</a> in StackOverflow. In short, functional reactive programming is a combination of functional and reactive techniques that can represent an elegant approach to event-driven programming with values that change over time and where the consumer reacts to the data as it comes in. RxJava library implements the concept of FRP in Java world.</p><h4>Reactive Programming in Spring Framework</h4><p>Spring Framework 5 introduced <strong>spring-webflux</strong> module, that contains support for reactive HTTP client as well as for <strong>reactive server web applications with REST</strong>.</p><p>In the server-side, the incoming HTTP requests are handled in the following way –</p><ol><li><strong>Router Functions </strong>— As the name suggests, Router function is responsible for routing the requests to the handler functions for further processing if a request matches a particular route.</li><li><strong>Handler Functions </strong>— Handler Function takes a Server Request as input and returns Mono of Server Response. The incoming server request exposes the body as Flux or Mono.</li></ol><p><em>Wait, but what is Flux &amp; Mono? </em>So, Flux &amp; Mono are the two different data types that come from the <a href="https://github.com/reactor/reactor-core">Reactor Core</a> Java 8 library, that implements the reactive programming paradigm. In a simplified explanation Flux is a datatype that can emit 0..N elements, whereas Mono can emit 0..1 element.</p><p>Webflux also includes a functional, reactive HTTP client called <strong>WebClient</strong>, which is fully non-blocking and reactive. It exposes network input &amp; output as a reactive ClientHttpRequest &amp; ClientHttpResponse. Below is an example of usage of WebClient -</p><pre>WebClient client = WebClient.<em>create</em>(&quot;http://your.domain.com&quot;);</pre><pre>Mono&lt;Response&gt; account = client.get()<br>    .uri(&quot;/path/to/endpoint&quot;)<br>    .accept(MediaType.<em>valueOf</em>(<em>APPLICATION_JSON</em>))<br>    .exchange(request)<br>    .then(response -&gt; response.bodyToMono(Response.class));</pre><h3>Testing Reactive Application</h3><p>In the era of Continuous Testing, it is of utmost importance to move tests as left as possible and carry out the majority of your testing before the deployment phase. The testing phases could vary on the different applications, but more or less should be aligned to the popular testing patterns in microservices (for this blog we will discuss the testing strategy discussed in <a href="https://martinfowler.com/articles/microservice-testing/">martinFowler.com</a>)</p><h4>Tech stack for testing in Spring Framework</h4><p><strong>Junit 5, test framework — </strong>There could be probably nothing better than using Junit as the testing framework! With Junit 5’s introduction to the several cool features like ParameterizedTest, Nested Unit tests, Lambda support, this would fit in to support all capabilities required as the test framework.</p><p><strong>WebTestClient — </strong>Spring Framework also provides a testing client that comes in the name of WebTestClient in the spring-test module. WebTestClient can be used to test WebFlux server endpoints with or without running the server. Below is a simple example use of WebTestClient -</p><pre>//create server and bind to URL<br> WebTestClient webTestClient = WebTestClient<br>     .<em>bindToServer</em>()<br>     .baseUrl(&quot;localhost:9000&quot;)<br>     .responseTimeout(Duration.<em>ofMinutes</em>(1L))<br>     .build();<br> <br> //post service call<br> WebTestClient.ResponseSpec response = webTestClient<br>     .post()<br>     .uri(&quot;/authenticate&quot;)<br>     .body(BodyInserters.<em>fromValue</em>(<em>EMPTY</em>))<br>     .exchange();<br> <br> //validate response status<br> response<br>     .expectStatus()<br>     .isOk();</pre><pre>//parse response object<br> response<br>     .expectBody(ResponseDTO.class)<br>     .returnResult()<br>     .getResponseBody();</pre><p><strong>Mockito — </strong>Mockito is a mocking framework for Junit test cases.</p><p><strong>RestAssured — </strong>WebTestClient can be mutated with RestAssured and can be used to leverage the benefits of the RestAssured library –</p><pre><em>given</em>()<br>     .standaloneSetup(webTestClient.mutate())<br>     .get(uriBuilder.buildURI(requestObject));</pre><p><strong>WireMock — </strong>WireMock can be used for stubbing any downstream interaction. <a href="https://search.maven.org/classic/#search%7Cgav%7C1%7Cg%3A%22org.springframework.cloud%22%20AND%20a%3A%22spring-cloud-contract-wiremock%22">spring-cloud-contract-wiremock</a> dependency provides the WireMock library. A sample stub code looks like –</p><pre><em>stubFor</em>(<em>post</em>(&quot;path/to/downstream&quot;)<br>    .withRequestBody(<em>equalToJson</em>(“{‘key’: ‘value’}”))<br>    .willReturn(<br>        <em>aResponse</em>()<br>            .withStatus(200)<br>            .withHeader(&quot;Content-Type&quot;, &quot;application/json&quot;)<br>            .withHeader(&quot;Accept&quot;, &quot;application/json&quot;)<br>            .withBodyFile(&quot;json/&quot; + responseFileName)));</pre><p>WireMock server can be configured with</p><pre>@AutoConfigureWireMock(port = 7777)</pre><p><strong>Spring Cloud Contract Verifier — </strong>Consumer-Driven Contract Testing can be achieved using the spring-cloud-contract-verifier project, that comes under the Spring Cloud Contract umbrella <a href="https://spring.io/projects/spring-cloud-contract">project</a>. It provides easy integration with Spring project for your microservice.</p><p>Set of tech stack mentioned here are just suggestive, and can be altered with any tools of your choice!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*chiBhSJx2GaZv1FwkP9IGw.png" /><figcaption>Testing Phase &amp; Tech Stack Mapping</figcaption></figure><h4>Summary</h4><p>So by now, you should be able to understand, what is reactive programming, why we need them and how we can choose tech stack for different test phases. In the <a href="https://medium.com/@nandan.abhi10/testing-reactive-microservice-in-spring-boot-unit-testing-fe453887ffa1">next article</a> in this series, I will provide detailed examples of testing phase implementation. Stay tuned!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=583310ebcfb5" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>