How to test a controller in Spring Boot - a practical guide

How to test a controller in Spring Boot - a practical guide

There are different ways to test your Controller (Web or API Layer) classes in Spring Boot, some provide support to write pure Unit Tests and some others are more useful for Integration Tests. Within this post, I’ll cover the main three test approaches available for Spring: using MockMVC in standalone mode, MockMVC together with SpringRunner, and using SpringBootTest.

read more

Spring Boot and Kafka – Practical Example

Spring Boot and Kafka – Practical Example

This blog post shows you how to configure Spring Kafka and Spring Boot to send messages using JSON and receive them in multiple formats: JSON, plain Strings or byte arrays.

This sample application also demonstrates how to use multiple Kafka consumers within the same consumer group with the @KafkaListener annotation, so the messages are load-balanced. Each consumer implements a different deserialization approach.

Besides, at the end of this post, you will find some practical exercises in case you want to grasp some Kafka concepts like the Consumer Group and Topic partitions.

read more

Sending and receiving JSON messages with Spring Boot AMQP and RabbitMQ

Sending and receiving JSON messages with Spring Boot AMQP and RabbitMQ

Setting up a Hello-World Spring Boot application using AMQP with RabbitMQ is quite easy if you go for the basic setup, as many other guides do. However, the configuration is not so straightforward when you get into the serialization setup and want to make use of @RabbitListener annotations to produce and consume messages in JSON format. Therefore, I’m sharing with you a really simple but more serious approach that those hello-messaging apps, supported with a real-life practical example.

This article covers:

  • How to send/publish Java Objects as JSON messages using Spring Boot and RabbitMQ’s RabbitTemplate.
  • How to read/consume JSON messages as Java Objects using Spring Boot and RabbitMQ’s @RabbitListener annotation.
  • How to send and receive Java Objects through RabbitMQ using default Java serializer.

read more

A Practical Example of Cucumber's Step Definitions in Java

A Practical Example of Cucumber's Step Definitions in Java

This third section of the guide focuses on the Step Definition Java classes. We use the real example project, and map the Gherkin sentences to Java methods with the so-called Step Definition files. You’ll learn about Cucumber’s built-in Parameter Types and you’ll create your own one. Besides, we’ll put into practice the best practices to keeping the state in a separate abstraction layer.

read more