{"id":14977560,"url":"https://github.com/jupiter-tools/spring-test-kafka","last_synced_at":"2025-10-28T04:31:42.575Z","repository":{"id":57725724,"uuid":"202994841","full_name":"jupiter-tools/spring-test-kafka","owner":"jupiter-tools","description":"Tools for integration testing of Apache Kafka with SpringBoot applications","archived":false,"fork":false,"pushed_at":"2020-09-01T12:21:06.000Z","size":512,"stargazers_count":17,"open_issues_count":2,"forks_count":4,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-01T11:25:00.216Z","etag":null,"topics":["kafka","spring-boot","spring-boot-test","spring-framework","spring-kafka","test-containers","testcontainers"],"latest_commit_sha":null,"homepage":"http://jupiter-tools.com","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jupiter-tools.png","metadata":{"files":{"readme":"README.adoc","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-08-18T11:32:07.000Z","updated_at":"2023-10-31T14:36:41.000Z","dependencies_parsed_at":"2022-09-02T03:31:07.067Z","dependency_job_id":null,"html_url":"https://github.com/jupiter-tools/spring-test-kafka","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jupiter-tools%2Fspring-test-kafka","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jupiter-tools%2Fspring-test-kafka/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jupiter-tools%2Fspring-test-kafka/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jupiter-tools%2Fspring-test-kafka/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jupiter-tools","download_url":"https://codeload.github.com/jupiter-tools/spring-test-kafka/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":238597386,"owners_count":19498396,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":["kafka","spring-boot","spring-boot-test","spring-framework","spring-kafka","test-containers","testcontainers"],"created_at":"2024-09-24T13:55:54.609Z","updated_at":"2025-10-28T04:31:35.696Z","avatar_url":"https://github.com/jupiter-tools.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":":toc: preamble\n\n# Spring Test Kafka\n\nimage:https://travis-ci.com/jupiter-tools/spring-test-kafka.svg?branch=master[\"Build Status\", link=\"https://travis-ci.com/jupiter-tools/spring-test-kafka\"]\nimage:https://codecov.io/gh/jupiter-tools/spring-test-kafka/branch/master/graph/badge.svg[\"\", link=\"https://codecov.io/gh/jupiter-tools/spring-test-kafka\"]\n\nTools to write integration tests of Spring Framework with the Apache Kafka.\n\n## Overview\n\nimage:./images/spring-test-kafka-containers.png[redis container scheme]\n\n## How to write integration tests on Spring Boot with Apache Kafka\n\nAdd this library in dependencies:\n\n[source,xml]\n----\n\u003cdependency\u003e\n    \u003cgroupId\u003ecom.jupiter-tools\u003c/groupId\u003e\n    \u003cartifactId\u003espring-test-kafka\u003c/artifactId\u003e\n    \u003cversion\u003e0.2\u003c/version\u003e\n\u003c/dependency\u003e\n----\n\nNow, you can start Kafka in docker (TestContainers) by the using of `@KafkaTestContainer` annotation in tests:\n\n[source, java]\n----\n@SpringBootTest\n@KafkaTestContainer\nclass RedisTestContainerTest {\n\n    @Autowired\n    private KafkaTemplate\u003cString, String\u003e kafkaTemplate;\n\n    @Test\n    void sendAndReceiveTest() throws InterruptedException {\n        assertThat(kafkaTemplate).isNotNull();\n        kafkaTemplate.send(\"test-topic\", \"flight of a dragon\");\n        ...\n    }\n}\n----\n\nYou can use this annotation to start Kafka container in tests both with JUnit5 and JUnit4.\nThe implementation doesn't depend on some test framework, just on the Spring Framework.\n\n## How to use multiple Kafka containers in the one test case:\n\n[source, java]\n----\n@SpringBootTest\n@KafkaTestContainer  \u003c1\u003e\n@KafkaTestContainer(bootstrapServersPropertyName = \"second.kafka.server\") \u003c2\u003e\nclass MultipleContainerInOneTest {\n\n    ...\n\n}\n----\n\u003c1\u003e start a first Kafka test container and set a bootstrap servers of started container to default Spring Boot properties (`spring.kafka.bootstrap-servers`)\n\u003c2\u003e start one more Kafka container and set a bootstrap servers to specified property, exactly in this property you can read an actual value of bootstrap servers after run the application context.\n\n\n## A better way to test your data in integration tests\n\nYou can test received messages after test execution if you use JSON format.\nJust describe expected dataset for a test case by the using `@ExpectedDataSet` annotation:\n\n[source, java]\n----\n@SpringBootTest\n@KafkaTestContainer\n@EnableKafkaTest(topics = {\"test-topic\", \"another-topic\"})\nclass ExpectedMessagesTest {\n\n    @Autowired\n    private KafkaTemplate\u003cString, Object\u003e kafkaTemplate;\n\n\n    @Test\n    @ExpectedMessages(topic = \"test-topic\", datasetFile = \"/datasets/expected_event.json\")\n    void firstTopic() {\n        kafkaTemplate.send(\"test-topic\", new Foo(\"qwert\"));\n        kafkaTemplate.send(\"test-topic\", new Bar(\"baaark\"));\n    }\n\n    @Test\n    @ExpectedMessages(topic = \"another-topic\", datasetFile = \"/datasets/expected_another_event.json\")\n    void anotherTopic() {\n        kafkaTemplate.send(\"another-topic\", Bar.builder().time(new Date()).build());\n    }\n}\n\nclass Foo {\n    String value;\n}\n\nclass Bar {\n    String name;\n    Date time;\n}\n----\n\nAnd after test execution, we will wait for messages declared in this JSON file.\nThe content of JSON dataset file:\n\n[source, json]\n----\n{\n  \"com.kafkatest.example.events.Foo\": [\n    {\n      \"value\": \"qwert\"\n    }\n  ],\n  \"com.kafkatest.example.events.Bar\": [\n    {\n      \"name\": \"baaark\"\n    }\n  ]\n}\n----\n\nAlso, you can use `@NoMessagesExpected` to check the silence on air.\nReceiving any messages will fail the test case.\n\n[source, java]\n----\n@Test\n@NoMessagesExpected(timeout = 3000)\nvoid silence() {\n\n}\n----\nWaits during the timeout for messages in Kafka topics(set in `@EnableKafkaTest`),\nif receive something then throws an exception.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjupiter-tools%2Fspring-test-kafka","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjupiter-tools%2Fspring-test-kafka","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjupiter-tools%2Fspring-test-kafka/lists"}