{"id":23122321,"url":"https://github.com/andrecaiado/spring-boot-tests","last_synced_at":"2026-04-30T15:32:47.835Z","repository":{"id":242546893,"uuid":"809700455","full_name":"andrecaiado/spring-boot-tests","owner":"andrecaiado","description":"Spring Boot application with integration tests and slice tests","archived":false,"fork":false,"pushed_at":"2024-06-07T22:18:39.000Z","size":46,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-04T04:14:56.317Z","etag":null,"topics":["failsafe-plugin","integration-testing","slice-testing","spring-boot","surefire-plugin"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/andrecaiado.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-06-03T09:28:46.000Z","updated_at":"2024-08-19T14:31:17.000Z","dependencies_parsed_at":"2024-06-03T18:01:48.433Z","dependency_job_id":"53c7a97e-fe2d-4525-94a0-dbfe4869e70b","html_url":"https://github.com/andrecaiado/spring-boot-tests","commit_stats":null,"previous_names":["andrecaiado/spring-boot-tests"],"tags_count":0,"template":false,"template_full_name":"andrecaiado/spring-boot-template","purl":"pkg:github/andrecaiado/spring-boot-tests","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andrecaiado%2Fspring-boot-tests","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andrecaiado%2Fspring-boot-tests/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andrecaiado%2Fspring-boot-tests/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andrecaiado%2Fspring-boot-tests/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/andrecaiado","download_url":"https://codeload.github.com/andrecaiado/spring-boot-tests/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andrecaiado%2Fspring-boot-tests/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32469344,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-30T13:12:12.517Z","status":"ssl_error","status_checked_at":"2026-04-30T13:12:06.837Z","response_time":57,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["failsafe-plugin","integration-testing","slice-testing","spring-boot","surefire-plugin"],"created_at":"2024-12-17T07:18:43.832Z","updated_at":"2026-04-30T15:32:47.816Z","avatar_url":"https://github.com/andrecaiado.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Spring Boot tests project\nThis is a Spring Boot applications with slice tests and integration tests.\n\nThe tests implemented in this project are the following:\n\n- [Slice tests](#slice-tests)\n  - [Repository layer](#repository-layer)\n  - [Service layer](#service-layer)\n  - [Web layer](#web-layer)\n- [Integration tests](#integration-tests)\n  - [With an embedded servlet container](#with-an-embedded-servlet-container)\n  - [Mocking the servlet container](#mocking-the-servlet-container)\n\n**Notes on running the tests**:\n\nThe [Maven Surefire](https://maven.apache.org/surefire/maven-surefire-plugin/usage.html) and [Maven Failsafe](https://maven.apache.org/surefire/maven-failsafe-plugin/index.html) plugins are configured in the [pom.xml](pom.xml) to run the slice tests (unit tests) and the integration-tests.\n\nRun the slice tests:\n```shell\nmvn surefire:test\n```\n\nRun the integration tests:\n```shell\nmvn failsafe:integration-test\n```\n\nRun all the tests:\n```shell\nmvn verify\n```\n\n## Slice tests\n\nSlice tests allows to perform isolated tests within a slice of the application, e.g., repository slice, web slice. In slice tests, dependencies need to be mocked.\n\n### Repository slice\n\nThe repository slice tests are implemented using `@DataJpaTest` annotation. This annotation will bootstrap only the repository layer of the application and will not load the full application context. It will also autoconfigure `TestEntityManager` which provides a subset of `EntityManager` methods.\n\nWhen using `@DataJpaTest` annotation, an in-memory database is autoconfigured and replaces any explicit or autoconfigured DataSource.\nWe will use the `@AutoConfigureTestDatabase` annotation to override this default behavior and use the database configuration we provide for tests in the `src/test/resources/application.yaml` file that will bootstrap an H2 database.\n\n```java\n@DataJpaTest\n@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)\npublic class EmployeeRepositoryTest {\n  // Add tests here\n}\n```\n\nThe H2 database will be populated by Flyway with the schema and data from the `src/main/resources/db/migration` folder. The tests will be executed upon this database and data.\n\n**Notes:**\n- By default, all tests decorated with the `@DataJpaTest` annotation become transactional. This means that the test will run within a transaction that will be rolled back at the end of the test.\n\n### Service slice\n\nSlice tests usually refers to the repository or web layer, nevertheless, we are going to use the term to refer to the service layer tests as well.\n\nWe use the SpringExtension class provided by the Spring Framework to enable integration between Spring and JUnit. To do this, we use the `@ExtendWith` annotation and pass the `Spring Extension` class as a parameter.\n\n```java\n@ExtendWith(SpringExtension.class)\npublic class EmployeeServiceTest {\n    \n}\n```\n\n### Web slice\n\nThe web layer slice tests are implemented using `@WebMvcTest` annotation. This annotation will bootstrap only the web layer of the application and will not load the full application context. It will also autoconfigure `MockMvc` which provides a way to test the web layer of the application.\n\n```java\n@WebMvcTest(EmployeeController.class)\npublic class EmployeeControllerTest {\n    \n}\n```\n\nThe implemented tests cover the following responsibilities:\n- Verifying HTTP Request Matching, response status, and response body.\n- Verifying Input Deserialization and Output Serialization.\n- Verifying Input Validation.\n- Verifying Business Logic Calls (service).\n- Verifying Exception Handling\n\n## Integration tests\n\nIntegration tests with a servlet container are implemented using the `@SpringBootTest` annotation. This annotation will bootstrap the full application context and will start the embedded servlet container.\n\nWhen using this annotation, Spring Boot automatically searches for a class annotated with `SpringBootConfiguration` up in the hierarchy. Usually, the entry point of the application is annotated with `SpringBootApplication` which is a meta-annotation for `SpringBootConfiguration`, therefore, Spring Boot will find the entry point of the application and bootstrap the full application context.\n\nIf we want to explicitly specify the classes we want to use for configuration, we can combine the `@SpringBootTest` annotation with the `@ContextConfiguration` annotation.\n\nThe `@SpringBootTest` annotation will also autoconfigure a `TestRestTemplate`so we can test the endpoints.\n\n**Notes:**\n\nThis project follows the approach that says that unit and integration tests should have different source and resource directories. For this purpose, the following folders were created:\n\n- src/integration-test/java\n- src/integration-test/resources\n\nThe folders were created manually and the [Build Helper Maven plugin](https://www.mojohaus.org/build-helper-maven-plugin/) was configured in the [pom.xml](pom.xml) to set the directories as source and resources.\n\n### With an embedded servlet container\n\nThis is the default behavior of the `@SpringBootTest` annotation. It will start the embedded servlet container and will bootstrap the full application context.\n\n```java\n@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)\npublic class EmployeeControllerIT {\n  \n  @Autowired\n  private TestRestTemplate testRestTemplate;\n\n}\n```\n\nIn the above example, we are starting the embedded servlet container with a random port. This is useful when we have multiple tests running in parallel and we want to avoid port conflicts.\n\nThe `TestRestTemplate` will be autoconfigured to use the random port and that's why we can use relative paths instead of absolute paths.\n\n**Notes:**\n\nAs the `testDeleteEmployeeById` deletes the record from the embedded database, the following was implemented:\n- The test was annotated with `@DirtiesContext(methodMode = DirtiesContext.MethodMode.AFTER_METHOD)` so the context is reloaded after the method is executed and the database is recreated.\n- The database name was appended with a random uuid, so it forces to create a new instance of the database.\n\n### Mocking the servlet container\n\nA mocked servlet container allows to process the requests through the DispatcherServlet but without running a servlet container.\n\nAdding the `@AutoConfigureMockMvc` annotation will autoconfigure the `MockMvc` bean that we can then inject in the tests.\n\n```java\nimport static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;\nimport static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;\n\n@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.MOCK)\n@AutoConfigureMockMvc\npublic class EmployeeControllerIT {\n\n  @Autowired\n  private MockMvc mockMvc;\n\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fandrecaiado%2Fspring-boot-tests","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fandrecaiado%2Fspring-boot-tests","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fandrecaiado%2Fspring-boot-tests/lists"}