Hello, API Testing! A Beginner's Guide to Spring Boot and Cucumber
Tebogo Tseka
Software Development Engineer in Test | ISTQB certified | FinOps | Python, Java, Springboot, 5 x AWS Certified, GenAI, Terraform, Prompt Engineering | Masters in Chemical Engineering
If you’re new to building and testing APIs, you’re in the right place. I will walk you through how I created a simple API using Spring Boot and tested it with a tool called Cucumber. Don’t worry if those words sound unfamiliar—I’ll explain everything in a beginner-friendly way, step by step. By the end, you’ll understand how to make your API and test it to ensure it works perfectly. Let’s get started!
What Is an API?
First things first: What’s an API? An API, or Application Programming Interface, is like a messenger that lets different software programs talk to each other. Imagine you’re at a restaurant, and the waiter takes your order to the kitchen. You don’t need to know how the chef cooks the food—the waiter (the API) handles that for you. In this case, I built a simple API called the "Hello API" that manages greeting messages, like saying "Hello, Alice!" It’s a basic example, but it’s perfect for learning the ropes.
Why Testing Matters
Before we jump into building anything, let’s talk about testing. When you create an API, you want to be sure it works the way you expect—not just now but also later when you improve it or add new features. Testing is like double-checking your homework before handing it in. It helps you spot mistakes early so your API doesn’t break unexpectedly. Trust me, it’s a lifesaver!
CRUD: The Basics of APIs
Most APIs, including my Hello API, are built around something called CRUD. It’s a fancy acronym that stands for:
Here’s how it worked in my Hello API:
These are the building blocks of most APIs, and testing makes sure they all work smoothly.
Introducing Cucumber: Testing Made Simple
For testing my API, I used a tool called Cucumber. What’s cool about Cucumber is that it lets you write tests in plain English, so you don’t need to be a coding expert to understand them. This is great if you’re working with a team, especially if some people aren’t technical—like designers or managers.
Cucumber uses a format called Gherkin, which breaks tests into simple steps using words like "Given," "When," and "Then." Here’s an example:
Scenario: Create a new greeting
Given I have some information to create a greeting
When I ask the API to create it
Then I should get a message saying it worked and see the greeting details
It’s like writing a short story about what the API should do. This makes testing:
A Real Test Example
Let’s look at a test I wrote for my Hello API. Don’t worry if it seems a little technical—I’ll break it down.
gherkin
Scenario: Create a new hello message
Given I set the request body to:
"""
{
"name": "Alice",
"message": "Hello, Alice"
}
"""
When I send a POST request to "/hello"
Then the response status code should be 200
And the response body should have "name" as "Alice" and "message" as "Hello, Alice"
Here’s what this means in simple terms:
领英推荐
It’s like giving the API a little job and checking if it did it right.
Hooking Cucumber Up to Spring Boot
Since I built my API with Spring Boot (a popular tool for making Java apps), I needed to connect it to Cucumber. This part sounds complicated, but it’s just about setting things up so the tests run in an environment that matches the real API. Think of it as telling Cucumber, "Hey, test this the same way the API works." Once that’s done, the tests are reliable and give you confidence in your API.
Testing Beyond the Basics
I didn’t just test the CRUD stuff—I checked other things too:
These extra checks made my API not just functional but solid.
Lessons I Learned as a Beginner
This project taught me a lot, even as someone still figuring things out. Here’s what stood out:
Is Testing Worth It?
You might wonder, "Doesn’t testing take a lot of time?" At first, it feels that way, but it pays off big time:
It’s like practicing for a game—the more you practice, the better you play, and the less you panic.
You Can Try This Too!
If you’re just starting, don’t let APIs or testing scare you. Cucumber makes it approachable, even for beginners. It might feel a bit new at first, but with practice, you’ll build APIs that work great and don’t let you down.
Here’s my tip: start small. Make a simple API like my Hello API and write a couple of Cucumber tests. You’ll learn so much, and it’s fun to see it come together.
Let’s Talk!
Have you tried creating or testing an API? What went well? What tripped you up? Leave a comment below—I’d love to hear your story. If you have questions, ask away—I’m happy to help!
Happy coding!
Student at University of Johannesburg
2 周So cucumber uses Pseudocode?
Kutloanong, freestate
3 周Very informative I'm a beginner & just learned alot thanks