<?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: Thisul Herath</title>
    <description>The latest articles on DEV Community by Thisul Herath (@thisulherath).</description>
    <link>https://dev.to/thisulherath</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.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4256933%2F2525c638-738f-46b1-9d1a-a01944c77af3.jpg</url>
      <title>DEV Community: Thisul Herath</title>
      <link>https://dev.to/thisulherath</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/thisulherath"/>
    <language>en</language>
    <item>
      <title>Why Both Tests Passed: Testing a Mocked HTTP Client in Ballerina</title>
      <dc:creator>Thisul Herath</dc:creator>
      <pubDate>Sat, 21 Nov 2026 08:28:07 +0000</pubDate>
      <link>https://dev.to/thisulherath/why-both-tests-passed-testing-a-mocked-http-client-in-ballerina-31kk</link>
      <guid>https://dev.to/thisulherath/why-both-tests-passed-testing-a-mocked-http-client-in-ballerina-31kk</guid>
      <description>&lt;p&gt;I ran &lt;code&gt;bal test&lt;/code&gt; and got one passing test. Then I changed the test, ran it again, and got exactly the same result. If both versions passed, what had I improved?&lt;/p&gt;

&lt;p&gt;I found the answer by changing one line in the function and watching what happened.&lt;/p&gt;

&lt;h2&gt;
  
  
  What the first test checked
&lt;/h2&gt;

&lt;p&gt;I was working through Ballerina’s HTTP client mocking example. It includes a function called &lt;code&gt;getRandomJoke(name)&lt;/code&gt; that fetches a joke and replaces “Chuck Norris” with the name you give it. If I call &lt;code&gt;getRandomJoke("Alex")&lt;/code&gt;, the joke I get back should be about Alex.&lt;/p&gt;

&lt;p&gt;The example uses a mock HTTP client. That lets the test supply the same joke every time without calling a live API. The original test called the client directly:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;http:Response result = check clientEndpoint-&amp;gt;/random;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It then checked that the response contained the joke configured in the mock. That explained the first pass. It also showed me the gap: &lt;code&gt;getRandomJoke&lt;/code&gt; never ran. Whatever happened inside that function, the test would keep asking the mock for the same joke and getting the same answer.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I changed
&lt;/h2&gt;

&lt;p&gt;I kept “Chuck Norris” in the mock response and changed the test to call the function with “Alex”:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;string result = check getRandomJoke("Alex");
test:assertEquals(result, "When Alex wants an egg, he cracks open a chicken.");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Keeping the two names different was important. “Chuck Norris” was what the mock supplied. “Alex” was what I expected the function to return. When I ran &lt;code&gt;bal test&lt;/code&gt;, it passed again.&lt;/p&gt;

&lt;h2&gt;
  
  
  The one-line experiment
&lt;/h2&gt;

&lt;p&gt;To see what the updated test could catch, I opened &lt;code&gt;main.bal&lt;/code&gt; and temporarily changed &lt;code&gt;return replacedText;&lt;/code&gt; to &lt;code&gt;return joke;&lt;/code&gt;. The function still calculated the sentence about Alex, but it returned the original joke.&lt;/p&gt;

&lt;p&gt;This time, &lt;code&gt;bal test&lt;/code&gt; failed. These two lines from the output showed why:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;expected: 'When Alex wants an egg, he cracks open a chicken.'
actual  : 'When Chuck Norris wants an egg, he cracks open a chicken.'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Ballerina also warned that &lt;code&gt;replacedText&lt;/code&gt; was unused. That warning gave me a clue in the code, and the failed assertion showed the effect a caller would see.&lt;/p&gt;

&lt;p&gt;I restored &lt;code&gt;return replacedText;&lt;/code&gt; and ran &lt;code&gt;bal test&lt;/code&gt; again:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1 passing
0 failing
0 skipped
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  What I learned
&lt;/h2&gt;

&lt;p&gt;Both versions passed when the function worked. The difference was what would happen when it broke. The original test could pass even if every caller got a joke about Chuck Norris. The updated test caught that mistake and showed me the wrong name right in the output.&lt;/p&gt;

&lt;p&gt;The next time I write a test, I’ll ask myself: what small change to the function should make this test fail?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://ballerina.io/learn/test-ballerina-code/test-services-and-clients/#test-with-clients" rel="noopener noreferrer"&gt;Ballerina tutorial — Test services and clients&lt;/a&gt; · &lt;a href="https://github.com/ballerina-platform/ballerina-dev-website/pull/10534" rel="noopener noreferrer"&gt;My documentation pull request&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I ran the Ballerina tests and collected the results myself. I used AI to draft this article.&lt;/p&gt;

</description>
      <category>ballerina</category>
      <category>testing</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
