{"id":18142407,"url":"https://github.com/fboeller/component-testing-example-java","last_synced_at":"2026-04-28T11:03:33.184Z","repository":{"id":43852327,"uuid":"239336396","full_name":"fboeller/component-testing-example-java","owner":"fboeller","description":"This is an example project showcasing how a microservice can be tested in Java.","archived":false,"fork":false,"pushed_at":"2023-12-05T22:24:37.000Z","size":78,"stargazers_count":2,"open_issues_count":2,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-06T19:18:59.698Z","etag":null,"topics":["java","mockserver","postgresql","testcontainers"],"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/fboeller.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":"2020-02-09T16:32:57.000Z","updated_at":"2020-05-01T22:12:12.000Z","dependencies_parsed_at":"2024-12-20T06:23:56.577Z","dependency_job_id":"b2e1ae85-267e-4e29-999c-7e9d4752584e","html_url":"https://github.com/fboeller/component-testing-example-java","commit_stats":null,"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"purl":"pkg:github/fboeller/component-testing-example-java","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fboeller%2Fcomponent-testing-example-java","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fboeller%2Fcomponent-testing-example-java/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fboeller%2Fcomponent-testing-example-java/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fboeller%2Fcomponent-testing-example-java/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fboeller","download_url":"https://codeload.github.com/fboeller/component-testing-example-java/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fboeller%2Fcomponent-testing-example-java/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32377599,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-28T09:24:15.638Z","status":"ssl_error","status_checked_at":"2026-04-28T09:24:15.071Z","response_time":56,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: 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":["java","mockserver","postgresql","testcontainers"],"created_at":"2024-11-01T18:06:48.023Z","updated_at":"2026-04-28T11:03:33.166Z","avatar_url":"https://github.com/fboeller.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Example of Component Testing in Java\n\nThis is an example project showcasing how a microservice can be tested in Java.\n\n## Motivation\n\nA microservice has dependencies to databases and external services.\nWhen a microservice is changed, it is important to know if the functionality provided to the end user still works as expected.\nOne way to achieve that is end-to-end testing.\nThis testing method spawns the whole application stack and tests the system as a whole.\n\nIt has several disadvantages.\n* The startup of the application stack is slow and resource intensive, making it infeasible to run the tests for small changes.\n* Failed tests can not necessarily be assigned to an individual service, causing a lack of responsibility for failed tests.\n* It might not be possible to spawn all required dependencies.\n\nComponent testing aims to not have this disadvantages.\nThis testing method only focuses on a single microservice, eliminating the need to spawn the whole application stack.\nInstead, only direct dependencies are spawned.\nThese direct dependencies can be either mocked (often the case for external services) or unmocked (often the case for databases).\n\nAs a consequence, it becomes feasible to run the tests for single changes on developer machines and on a CI server.\nThis increases developer's confidence in their changes allowing them to deploy changes earlier, in smaller chunks and more frequently.\n\n## Application\n\nThe application under test offers an HTTP API to execute runs creating items in an external service.\nIt uses a `PostgreSQL` database for the persistence of runs.\n\nThe application uses several libraries to simplify the implementation.\n* The framework `Dropwizard` with `Jersey` to expose endpoints\n* The library `liquibase` for the database schema creation \n* The library `JDBI` to execute queries against the database\n* The library `retrofit` to execute requests against the external service\n\n## Test Strategy\n\nThe tests focus on three different parts of the application.\n1. The schema creation of the database and the application's interface to the database\n2. The business logic calling the external service\n3. The logic of the endpoints and how they affect each other\n\n### Database Tests\n\nThe first aspect is tested in the [RunDAOTest](src/test/java/RunDAOTest.java).\nThis test utilizes `testcontainers` to spawn a docker container with a `PostgreSQL` database.\nIt runs the `liquibase` schema migration of the application on the spawned database and tests the defined queries of the application's DAO.\n\n### Business Logic and External Service Tests\n\nThe second aspect is tested in the [RunServiceTest](src/test/java/RunServiceTest.java).\nThis test utilizes `testcontainers` to spawn a docker container with a mock server.\nIt executes the business logic of the application and verifies that the application sends the expected requests to the mock of the external service.\n\n### Resource Tests\n\nThe third aspect is tested in the [RunResourceTest](src/test/java/RunResourceTest.java).\nThis test utilizes `testcontainers` to spawn a docker container with a `PostgreSQL` database.\nIn contrast to the database tests, it uses a globally spawned docker container instead of a dedicated one.\nIt uses `Mockito` to mock the business logic of the application.\nIt calls the endpoints and verifies that they return the expected responses.\n\n## Setup, Build and Run\n\nMake sure you have `docker`, `docker-compose`, `Maven 3.6.x` and `Java 11` installed.\nEnsure that the docker daemon is running.\n\n2. Run `mvn clean package`\n1. Run `docker-compose up -d` to start the PostgreSQL database and a mock server.\n3. Start the Java application from an IDE or command line.\n4. Run `curl -X POST localhost:4201/runs?item_count=3` to create a run.\n5. Run `curl localhost:4201/runs` to retrieve all runs.\n6. Run `curl -X PUT \"localhost:4203/mockserver/retrieve?type=REQUESTS\"` to inspect the calls being send to the mock server.\n7. Run `docker-compose down` to shutdown the database and the mock server.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffboeller%2Fcomponent-testing-example-java","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffboeller%2Fcomponent-testing-example-java","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffboeller%2Fcomponent-testing-example-java/lists"}