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 (0)