Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Full Stack Development with Spring Boot 3 and React - Fourth Edition

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

Product type Book
Published in Oct 2023
Publisher Packt
ISBN-13 9781805122463
Pages 454 pages
Edition 4th Edition
Languages
Author (1):
Juha Hinkula Juha Hinkula
Profile icon Juha Hinkula

Table of Contents (23) Chapters

Preface 1. Part I: Backend Programming with Spring Boot
2. Setting Up the Environment and Tools – Backend 3. Understanding Dependency Injection 4. Using JPA to Create and Access a Database 5. Creating a RESTful Web Service with Spring Boot 6. Securing Your Backend 7. Testing Your Backend 8. Part II: Frontend Programming with React
9. Setting Up the Environment and Tools – Frontend 10. Getting Started with React 11. Introduction to TypeScript 12. Consuming the REST API with React 13. Useful Third-Party Components for React 14. Part III: Full Stack Development
15. Setting Up the Frontend for Our Spring Boot RESTful Web Service 16. Adding CRUD Functionalities 17. Styling the Frontend with MUI 18. Testing React Apps 19. Securing Your Application 20. Deploying Your Application 21. Other Books You May Enjoy
22. Index

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 2023 Publisher: Packt ISBN-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.
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 €14.99/month. Cancel anytime}