Adding Test Cases to Reactive Spring
I am a developer who loves Java, Spring, Quarkus, Micronaut, Open source, Microservices, Cloud
Search for a command to run...
I am a developer who loves Java, Spring, Quarkus, Micronaut, Open source, Microservices, Cloud
No comments yet. Be the first to comment.
Spring is most popular java framework. We will explore Spring boot and microservices.
Kafka is most sought after event system today. In this series we will look at Kafka event messaging and streaming. Github Repository link : https://github.com/CODINGSAINT/kafka-stream-spring What will we be creating We will create a streaming ap...
Spring AI Tool Calling: From Chatbot to AI Agent with @Tool Your AI is smart. It knows an enormous amount. But it's frozen. It doesn't know what time it is right now. It doesn't know what's on your ca
Spring AI Advisors API Explained Series: Spring AI Complete Course — Lecture 4 of 12Reading Time: 8 minutesLevel: Intermediate Most developers stop at ChatClient. That is enough for demos. It is not
Working with Multiple AI Models in Spring AI Spring AI Complete Course — Lecture 3 of 12Previous: Lecture 2 — ChatClient API | Next: Lecture 4 — Advisors API In production AI applications, you rarel
Spring AI ChatClient API: The Fluent Heart of AI Integration Introduction If you've ever tried integrating AI models into a Java application, you know the pain. HTTP clients, API keys scattered everywhere, vendor-specific SDKs that never quite fit. W...
What is Spring AI? — Why Java Developers Need This in 2026 Every AI tutorial you see is in Python. LangChain, LlamaIndex, OpenAI SDK — all Python. But here's the uncomfortable truth: 80% of enterprise backends run Java. So who's building AI into thos...
Github source code :
https://github.com/CODINGSAINT/springboot-rective-microservices-with-reactjs
In last 2 lectures, we have seen how to create a Reactive Web API and adding a separate Handler class to keep logic simpler and code organised. In this Lecture we will look at writing test cases for Reactive API.
Writing test cases for imperative programming is simpler as we know it executes line by line. Let us look how to we write Reactive API test case.
Here we have created different test cases for all of the API. We have autowired WebTestClient which act as test restTemplate. It will help us to call a reactive API with GET/PUT/POST/DELETE.
We have also created a static id field which we will be initialising at init method (which is only called while testing POST) . The initialized variable will be used at other test methods ,it will help us to create a user with unique id, update , retrieve and delete same user.
Now , look at the different test cases the first one is for create i.e. addUserTest
@Test
public void addUserTest() {
init();
User user= new User();
user.setId(id);
user.setName("Namish");
user.setEmail("namish@codingsaint.com");
webTestClient.post()
.uri("/user")
.body(Mono.just(user), User.class)
.accept(MediaType.APPLICATION_JSON_UTF8)
.exchange()
.expectStatus().isCreated()
.expectBody()
.jsonPath("$.id").isNotEmpty()
.jsonPath("$.name").isEqualTo("Namish");
}
Here we create User with unique id. We use webTestClient to post . It also help us to post a user as we do with restTemplate. We have configured that it accepts JSON with UTF-8. Once we call exchange it will call the rest endpoint. This endpoint is excepted to send HTTP status as 201 ,it has a body with ID field present and name equals to Namish.
@Test
public void getAllUsers(){
webTestClient.get().uri("/users")
.accept(MediaType.APPLICATION_JSON_UTF8)
.exchange()
.expectStatus().isOk()
.expectHeader().contentType(MediaType.APPLICATION_JSON_UTF8)
.expectBodyList(User.class);
}
With User been already intersted, Let’s verify if list of users can be tested. Here we expect “users” endpoint to return HTTP status as OK (200) with Json output and List should contain Users as body.
@Test
public void getUser (){
webTestClient.get().uri("/user/{id}" , Collections.singletonMap("id" ,id))
.accept(MediaType.APPLICATION_JSON_UTF8)
.exchange()
.expectStatus().isOk()
.expectBody()
.jsonPath("$.id").isEqualTo(id);
}
Now we can test getting User based on ID, In above test case we are verifying getting the user we inserted with id. This ID we can access as we made it static and initialized it while testing save user.
@Test
public void deleteUser (){
webTestClient.delete().uri("/user/{id}" , Collections.singletonMap("id" ,id))
.exchange()
.expectStatus().isNoContent();
}
}
At last let’s delete the user. We expect no content.
The full source of Test class is below.
https://gist.github.com/CODINGSAINT/9f74943e83fa55533d6ac526a3713586