lucasvalenteds / poc-spring-api-testing

Running unit and integration tests against domain, persistence and web layers of a REST API

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

POC: Spring API Testing

It demonstrates how to run unit and integration tests against domain, persistence and web layer of a REST API.

The goal is to develop a minimal Web Service that can manage customers, so we must be able to send HTTP requests to this service that is going to persist the data we sent in a relational database. Every aspect of the implementation, such as serialization, persistence, validation, business logic should be validated by different types of automated tests and no manual verification should be required to make sure the code works. The test instrumentation is done using Spring Framework annotations from the test module, the database is provisioned via migrations managed by Flyway.

The code related to domain is implemented using jakarta.validation annotations and tested using JUnit. The code related to persistence is implemented using Spring Data JPA and tested using Mockito. The code related to web (headers, status code, response body) is implemented using Spring Framework (MVC) and tested using MockMvc.

How to run

Description Command
Run tests ./gradlew test
Start the API ./gradlew bootRun

Preview

Logging output from test suite:

$ ./gradlew test

> Task :test

com.example.customer.CustomerControllerTest

  Test testFindingValidCustomerById() PASSED
  Test testCustomerNotFoundById() PASSED
  Test testRemovingValidCustomer() PASSED
  Test testCreatingValidCustomer() PASSED
  Test testFindingAllCustomers() PASSED

com.example.customer.CustomerRepositoryTest

  Test testNameShouldNotBeBlank() PASSED
  Test testCustomerShouldHaveName() PASSED

Logging output while running the application manually:

$ ./gradlew bootRun

> Task :bootRun

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::             (v3.0.0-M3)

2022-07-09T16:09:33.432-03:00  INFO 59946 --- [           main] com.example.Application          : Starting Application using Java 18.0.1.1 on pc with PID 59946
2022-07-09T16:09:33.435-03:00  INFO 59946 --- [           main] com.example.Application          : No active profile set, falling back to 1 default profile: "default"
2022-07-09T16:09:33.670-03:00  INFO 59946 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2022-07-09T16:09:33.691-03:00  INFO 59946 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 16 ms. Found 1 JPA repository interfaces.
2022-07-09T16:09:34.015-03:00  INFO 59946 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2022-07-09T16:09:34.020-03:00  INFO 59946 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2022-07-09T16:09:34.020-03:00  INFO 59946 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/10.0.21]
2022-07-09T16:09:34.070-03:00  INFO 59946 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2022-07-09T16:09:34.071-03:00  INFO 59946 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 602 ms
2022-07-09T16:09:34.142-03:00  INFO 59946 --- [           main] o.f.c.internal.license.VersionPrinter    : Flyway Community Edition 8.5.11 by Redgate
2022-07-09T16:09:34.142-03:00  INFO 59946 --- [           main] o.f.c.internal.license.VersionPrinter    : See what's new here: https://flywaydb.org/documentation/learnmore/releaseNotes#8.5.11
2022-07-09T16:09:34.142-03:00  INFO 59946 --- [           main] o.f.c.internal.license.VersionPrinter    : 
2022-07-09T16:09:34.146-03:00  INFO 59946 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2022-07-09T16:09:34.230-03:00  INFO 59946 --- [           main] com.zaxxer.hikari.pool.HikariPool        : HikariPool-1 - Added connection conn0: url=jdbc:h2:mem:01e1b276-3c1b-45fc-8e5d-15b2428463a1 user=SA
2022-07-09T16:09:34.231-03:00  INFO 59946 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2022-07-09T16:09:34.236-03:00  INFO 59946 --- [           main] o.f.c.i.database.base.BaseDatabaseType   : Database: jdbc:h2:mem:01e1b276-3c1b-45fc-8e5d-15b2428463a1 (H2 2.1)
2022-07-09T16:09:34.254-03:00  WARN 59946 --- [           main] o.f.c.internal.database.base.Database    : Flyway upgrade recommended: H2 2.1.212 is newer than this version of Flyway and support has not been tested. The latest supported version of H2 is 2.1.210.
2022-07-09T16:09:34.263-03:00  INFO 59946 --- [           main] o.f.core.internal.command.DbValidate     : Successfully validated 1 migration (execution time 00:00.004s)
2022-07-09T16:09:34.265-03:00  INFO 59946 --- [           main] o.f.c.i.s.JdbcTableSchemaHistory         : Creating Schema History table "PUBLIC"."flyway_schema_history" ...
2022-07-09T16:09:34.278-03:00  INFO 59946 --- [           main] o.f.core.internal.command.DbMigrate      : Current version of schema "PUBLIC": << Empty Schema >>
2022-07-09T16:09:34.280-03:00  INFO 59946 --- [           main] o.f.core.internal.command.DbMigrate      : Migrating schema "PUBLIC" to version "1 - create customer table"
2022-07-09T16:09:34.285-03:00  INFO 59946 --- [           main] o.f.core.internal.command.DbMigrate      : Successfully applied 1 migration to schema "PUBLIC", now at version v1 (execution time 00:00.007s)
2022-07-09T16:09:34.321-03:00  INFO 59946 --- [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [name: default]
2022-07-09T16:09:34.340-03:00  INFO 59946 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate ORM core version 5.6.9.Final
2022-07-09T16:09:34.406-03:00  INFO 59946 --- [           main] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.1.2.Final}
2022-07-09T16:09:34.455-03:00  INFO 59946 --- [           main] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.H2Dialect
2022-07-09T16:09:34.691-03:00  INFO 59946 --- [           main] o.h.e.t.j.p.i.JtaPlatformInitiator       : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2022-07-09T16:09:34.694-03:00  INFO 59946 --- [           main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2022-07-09T16:09:34.993-03:00  WARN 59946 --- [           main] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2022-07-09T16:09:35.138-03:00  INFO 59946 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2022-07-09T16:09:35.143-03:00  INFO 59946 --- [           main] com.example.Application          : Started Application in 1.885 seconds (process running for 2.09)
2022-07-09T16:09:35.155-03:00  INFO 59946 --- [           main] com.example.Application          : Persisting customer Customer(id=1, name=Hudson Cox)
2022-07-09T16:09:35.189-03:00  INFO 59946 --- [           main] com.example.Application          : Persisting customer Customer(id=2, name=Chase Griffin)
2022-07-09T16:09:35.191-03:00  INFO 59946 --- [           main] com.example.Application          : Persisting customer Customer(id=3, name=Ariana Cote)
2022-07-09T16:09:35.192-03:00  INFO 59946 --- [           main] com.example.Application          : Persisting customer Customer(id=4, name=Julia Callahan)
2022-07-09T16:09:35.193-03:00  INFO 59946 --- [           main] com.example.Application          : Persisting customer Customer(id=5, name=Makayla Norton)
2022-07-09T16:09:35.195-03:00  INFO 59946 --- [           main] com.example.Application          : Persisting customer Customer(id=6, name=Caleb Harrell)
2022-07-09T16:09:35.196-03:00  INFO 59946 --- [           main] com.example.Application          : Persisting customer Customer(id=7, name=Jose Banks)
2022-07-09T16:09:35.197-03:00  INFO 59946 --- [           main] com.example.Application          : Persisting customer Customer(id=8, name=Caroline Leonard)
2022-07-09T16:09:35.199-03:00  INFO 59946 --- [           main] com.example.Application          : Persisting customer Customer(id=9, name=Elizabeth Fowler)
2022-07-09T16:09:35.200-03:00  INFO 59946 --- [           main] com.example.Application          : Persisting customer Customer(id=10, name=Ryan Molina)
2022-07-09T16:09:35.201-03:00  INFO 59946 --- [           main] com.example.Application          : Persisting customer Customer(id=11, name=Grace Reed)
2022-07-09T16:09:35.202-03:00  INFO 59946 --- [           main] com.example.Application          : Persisting customer Customer(id=12, name=Piper Marks)
2022-07-09T16:09:35.204-03:00  INFO 59946 --- [           main] com.example.Application          : Persisting customer Customer(id=13, name=Caroline Sawyer)
2022-07-09T16:09:35.205-03:00  INFO 59946 --- [           main] com.example.Application          : Persisting customer Customer(id=14, name=William Neal)
2022-07-09T16:09:35.206-03:00  INFO 59946 --- [           main] com.example.Application          : Persisting customer Customer(id=15, name=Amelia Stein)
2022-07-09T16:09:35.207-03:00  INFO 59946 --- [           main] com.example.Application          : Persisting customer Customer(id=16, name=Julia Flowers)
2022-07-09T16:09:35.209-03:00  INFO 59946 --- [           main] com.example.Application          : Persisting customer Customer(id=17, name=Kevin Pierce)
2022-07-09T16:09:35.210-03:00  INFO 59946 --- [           main] com.example.Application          : Persisting customer Customer(id=18, name=Anthony Spears)
2022-07-09T16:09:35.211-03:00  INFO 59946 --- [           main] com.example.Application          : Persisting customer Customer(id=19, name=Jasmine Frank)
2022-07-09T16:09:35.212-03:00  INFO 59946 --- [           main] com.example.Application          : Persisting customer Customer(id=20, name=Zachary Lott)
2022-07-09T16:09:35.213-03:00  INFO 59946 --- [           main] com.example.Application          : Persisting customer Customer(id=21, name=Madelyn Owens)
2022-07-09T16:09:35.214-03:00  INFO 59946 --- [           main] com.example.Application          : Persisting customer Customer(id=22, name=Ellie Haney)
2022-07-09T16:09:35.215-03:00  INFO 59946 --- [           main] com.example.Application          : Persisting customer Customer(id=23, name=Kevin Burke)
2022-07-09T16:09:35.217-03:00  INFO 59946 --- [           main] com.example.Application          : Persisting customer Customer(id=24, name=Lydia James)
2022-07-09T16:09:35.218-03:00  INFO 59946 --- [           main] com.example.Application          : Persisting customer Customer(id=25, name=Charles Finley)

About

Running unit and integration tests against domain, persistence and web layers of a REST API


Languages

Language:Java 93.5%Language:Kotlin 6.5%