Reader small image

You're reading from  Modern API Development with Spring 6 and Spring Boot 3 - Second Edition

Product typeBook
Published inSep 2023
Reading LevelIntermediate
PublisherPackt
ISBN-139781804613276
Edition2nd Edition
Languages
Concepts
Right arrow
Author (1)
Sourabh Sharma
Sourabh Sharma
author image
Sourabh Sharma

Sourabh Sharma is a Senior Development Manager at Oracle with over 20 years of experience in the industry. He is a manager and architect who has been designing on-premise and cloud-based applications using Java, Javascript, and Oracle DB. Sourabh has worked with leading companies and delivered enterprise products and applications. His expertise lies in conceptualizing, modeling, designing, and developing N-tier and cloud-based web applications while leading teams. Sourabh's experience also includes developing microservice-based solutions and implementing various types of workflow and orchestration engines. He believes in continuous learning and sharing knowledge through his books and training.
Read more about Sourabh Sharma

Right arrow

Testing APIs

Proper automated testing helps you to reduce regression bugs and keeps your application stable. It makes sure that every change you make will fail during the build or testing phase if the change has any side effects on existing code. Investing in a test automation suite can give you peace of mind and will prevent any surprises in production.

This chapter will help you learn about test automation by showing you how to implement unit and integration test automation. You will learn how to test APIs manually and automatically. First, you will learn about automating unit and integration tests. After learning about these forms of automation, you will be able to make both types of testing an integral part of any build. You will also learn how to set up the Java Code Coverage (JaCoCo) tool to calculate different code coverage metrics.

In this chapter, we will cover the following topics:

  • Testing APIs and code manually
  • Testing automation

Let’s get...

Testing APIs and code manually

Testing is a continuous process in software development and maintenance cycles. You need to do full testing that covers all possible use cases and the respective code for each change. Different types of testing can be performed for APIs, including the following:

  • Unit testing: Unit testing is performed by developers to test the smallest unit (such as a class method) of code.
  • Integration testing: Integration testing is performed by developers to test the integration of different layers of components.
  • Contract testing: Contract testing is performed by developers to make sure any changes that are made to the API won’t break the consumer code. The consumer code should always comply with the producer’s contract (API). It is primarily required in microservices-based development.
  • End-to-end (E2E) testing: E2E testing is performed by the quality assurance (QA) team to test end-to-end scenarios, such as from the UI (consumer) to...

Testing automation

Whatever testing you are doing manually can be automated and made part of the build. This means that any change or code commit will run the test suite as a part of the build. A build will only be successful if all the tests are passed.

You can add automated integration tests for all the APIs. So, instead of firing each API manually using cURL or Insomnia, the build will fire them, and the test result will be available at the end of the build.

In this section, you are going to write an integration test that will replicate the REST client call and test all the application layers, starting from the controller, all the way down to the persistence layer, including the database (H2).

But before that, you will add the necessary unit tests. Ideally, these unit tests should have been added alongside the development process, or before the development process in the case of test-driven development (TDD).

Unit tests are tests that validate the expected results of...

Summary

In this chapter, you explored both manual and automated testing. You learned how to write unit and integration tests using JUnit, the Spring test libraries, AssertJ, and Hamcrest. You also learned how to use the Gherkin Given > When > Then language to make tests more readable. You then learned how to separate unit and integration tests.

Finally, you learned about various test automation skills by automating unit and integration tests. This will help you to automate your tests and catch bugs and gaps before you deliver the code to quality analysts or customers.

In the next chapter, you will learn how to containerize an application and deploy it in Kubernetes.

Questions

  1. What is the difference between unit testing and integration testing?
  2. What is the advantage of having separate unit and integration tests?
  3. What is the difference between mocking and spying on an object?

Answers

  1. Unit testing is done to test the smallest code unit, such as a method, whereas integration testing is performed where either different layers or multiple modules are involved. In this chapter, integration testing has been performed for the entire application, which involves all the layers of the application, including the database, whereas unit testing has been performed class-wise for each of the methods. In the context of this chapter, unit testing is white-box testing, whereas API integration testing is a kind of black-box testing because you verify the API’s functional requirement.
  2. Having separate unit and integration tests (and including their source location) allows you to manage tests easily. You can also have a configurable build setup that will perform unit testing during development or on demand because unit tests are faster. You can run only unit tests by using the gradlew clean build –x integrationTest command, whereas on merge request builds...
lock icon
The rest of the chapter is locked
You have been reading a chapter from
Modern API Development with Spring 6 and Spring Boot 3 - Second Edition
Published in: Sep 2023Publisher: PacktISBN-13: 9781804613276
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 €14.99/month. Cancel anytime

Author (1)

author image
Sourabh Sharma

Sourabh Sharma is a Senior Development Manager at Oracle with over 20 years of experience in the industry. He is a manager and architect who has been designing on-premise and cloud-based applications using Java, Javascript, and Oracle DB. Sourabh has worked with leading companies and delivered enterprise products and applications. His expertise lies in conceptualizing, modeling, designing, and developing N-tier and cloud-based web applications while leading teams. Sourabh's experience also includes developing microservice-based solutions and implementing various types of workflow and orchestration engines. He believes in continuous learning and sharing knowledge through his books and training.
Read more about Sourabh Sharma