Reader small image

You're reading from  Full Stack Development with Spring Boot 3 and React - Fourth Edition

Product typeBook
Published inOct 2023
PublisherPackt
ISBN-139781805122463
Edition4th Edition
Right arrow
Author (1)
Juha Hinkula
Juha Hinkula
author image
Juha Hinkula

Juha Hinkula is a software development lecturer at Haaga-Helia University of Applied Sciences in Finland. He received an MSc degree in Computer Science from the University of Helsinki and he has over 17 years of industry experience in software development. Over the past few years, he has focused on modern full stack development. He is also a passionate mobile developer with Android-native technology, and also uses React Native.
Read more about Juha Hinkula

Right arrow

Testing Your Backend

This chapter explains how to test your Spring Boot backend. The backend of an application is responsible for handling business logic and data storage. Proper testing of the backend ensures that the application works as intended, is secure, and is easier to maintain. We will create some unit and integration tests in relation to our backend, using the database application that we created earlier as a starting point.

In this chapter, we will cover the following topics:

  • Testing in Spring Boot
  • Creating test cases
  • Test-driven development

Technical requirements

The Spring Boot application that we created in the previous chapters is required.

The following GitHub link will also be required: https://github.com/PacktPublishing/Full-Stack-Development-with-Spring-Boot-3-and-React-Fourth-Edition/tree/main/Chapter06.

Testing in Spring Boot

The Spring Boot test starter package is automatically added to the build.gradle file by Spring Initializr when we create our project. The test starter dependency can be seen in the following snippet:

testImplementation 'org.springframework.boot:spring-boot-starter-test'

The Spring Boot test starter provides lots of handy libraries for testing, such as JUnit, Mockito, and AssertJ. Mockito is a mocking framework that is often used alongside testing frameworks like JUnit. AssertJ is a popular library for writing assertions in Java testing. In this book, we will use JUnit 5. The JUnit Jupiter module is part of JUnit 5 and provides annotations for more flexible testing.

If you take a look at your project structure, you’ll see that it already has its own package created for test classes:

­

Figure 6.1: Test classes

By default, Spring Boot uses an in-memory database for testing. We are using MariaDB at this point in the...

Before you begin: Join our book community on Discord

Give your feedback straight to the author himself and chat to other early readers on our Discord server (find the "full-stack-dev-spring-boot-3-react-4e" channel under EARLY ACCESS SUBSCRIPTION).

https://packt.link/EarlyAccess

Qr code Description automatically generated

This chapter explains how to test your Spring Boot backend. The backend of an application is responsible for handling business logic and data storage of the application. Proper testing of the backend ensures that the application works as intended, is secure and is easier to maintain. We will create some unit tests in relation to our backend—these will make your backend code easier to maintain. We will use the database application that we created in the previous chapter as a starting point.In this chapter, we will cover the following topics:

  • Testing in Spring Boot
  • Creating unit tests
  • Running tests

Technical requirements

The Spring Boot application that we created in the previous chapters is required.The following GitHub link will also be required: https://github.com/PacktPublishing/Full-Stack-Development-with-Spring-Boot-3-and-React-Fourth-Edition/tree/main/Chapter06

Testing in Spring Boot

We have built our Spring Boot backend application and now we start to write some tests with JUnit. The Spring Boot test starter package is added to the build.gradle file by Spring Initializr when we create our project. This is added automatically without any selection in the Spring Initializr page. The test starter dependency can be seen in the following snippet:

testImplementation 'org.springframework.boot:spring-boot-starter-test'

The Spring Boot test starter provides lots of handy libraries for testing, such as JUnit, Mockito, and AssertJ. Mockito is a mocking framework that is often used alongside testing frameworks like JUnit. AssertJ is a popular library for writing assertions in Java testing. In this book, we are using the JUnit 5 version (JUnit Jupiter). JUnit Jupiter is part of JUnit 5 and it provides annotations for more flexible testing. If you take a look of your project structure, it already has its own package created for test classes,...

Creating test cases

For unit and integration testing, we are using JUnit, which is a popular Java-based unit testing library. Spring Boot has built-in support for JUnit, making it easy to write tests for your Spring Boot application. The following source code shows an example skeleton of the Spring Boot test class. The @SpringBootTest annotation specifies that the class is a regular test class that runs Spring Boot-based tests. The @Test annotation before the method specifies to JUnit that the method can be run as a test case:

@SpringBootTest
public class MyTestsClass {
  @Test
  public void testMethod()  {
    // Test case code
  }
}

Assertions in unit testing are statements that can be used to verify whether the actual output of a code unit matches the expected output. In our case, the assertions are implemented using the AssertJ library that the spring-boot-starter-test artifact automatically includes. The AssertJ library provides assertThat() method that you can use to write assertions...

Summary

In this chapter, we focused on testing the Spring Boot backend. We used JUnit for testing and implemented test cases for JPA and RESTful web service authentication. We created one test case for our owner repository to verify that repository methods behave as expected. We also tested the authentication process by using our RESTful API. Remember that testing is an ongoing process throughout the development lifecycle. You should update and add tests to cover new features and changes when your application evolves.In the next chapter, we will set up the environment and tools related to frontend development.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Full Stack Development with Spring Boot 3 and React - Fourth Edition
Published in: Oct 2023Publisher: PacktISBN-13: 9781805122463
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at AU $19.99/month. Cancel anytime

Author (1)

author image
Juha Hinkula

Juha Hinkula is a software development lecturer at Haaga-Helia University of Applied Sciences in Finland. He received an MSc degree in Computer Science from the University of Helsinki and he has over 17 years of industry experience in software development. Over the past few years, he has focused on modern full stack development. He is also a passionate mobile developer with Android-native technology, and also uses React Native.
Read more about Juha Hinkula