DEV Community

Thisul Herath
Thisul Herath

Posted on Edited on AI-assisted

Why Both Tests Passed: Testing a Mocked HTTP Client in Ballerina

I ran bal test 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?

I found the answer by changing one line in the function and watching what happened.

What the first test checked

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

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:

http:Response result = check clientEndpoint->/random;
Enter fullscreen mode Exit fullscreen mode

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

What I changed

I kept “Chuck Norris” in the mock response and changed the test to call the function with “Alex”:

string result = check getRandomJoke("Alex");
test:assertEquals(result, "When Alex wants an egg, he cracks open a chicken.");
Enter fullscreen mode Exit fullscreen mode

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 bal test, it passed again.

The one-line experiment

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

This time, bal test failed. These two lines from the output showed why:

expected: 'When Alex wants an egg, he cracks open a chicken.'
actual  : 'When Chuck Norris wants an egg, he cracks open a chicken.'
Enter fullscreen mode Exit fullscreen mode

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

I restored return replacedText; and ran bal test again:

1 passing
0 failing
0 skipped
Enter fullscreen mode Exit fullscreen mode

What I learned

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.

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

Ballerina tutorial — Test services and clients · My documentation pull request

I ran the Ballerina tests and collected the results myself. I used AI to draft this article.

Top comments (1)

Collapse
 
challan116ux profile image
challan116-ux •

That mutation-style check — changing return replacedText to return joke and watching the test fail — is the real lesson here, and it is a habit worth keeping for any integration code, not just Ballerina. A test that passes no matter what the function does is really testing the mock, as you found, and that false confidence is exactly what bites us in integration work at SignalEDI when a partner API changes a field. I now ask the same question you landed on before trusting a green suite: what small change should make this test fail?