{"id":13697591,"url":"https://github.com/chbatey/kafka-unit","last_synced_at":"2025-04-06T19:13:08.654Z","repository":{"id":26623576,"uuid":"30079086","full_name":"chbatey/kafka-unit","owner":"chbatey","description":null,"archived":false,"fork":false,"pushed_at":"2021-11-04T06:45:09.000Z","size":136,"stargazers_count":172,"open_issues_count":25,"forks_count":72,"subscribers_count":15,"default_branch":"master","last_synced_at":"2025-03-30T17:11:34.831Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/chbatey.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-01-30T15:58:37.000Z","updated_at":"2025-02-16T21:43:05.000Z","dependencies_parsed_at":"2022-09-01T15:01:56.567Z","dependency_job_id":null,"html_url":"https://github.com/chbatey/kafka-unit","commit_stats":null,"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chbatey%2Fkafka-unit","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chbatey%2Fkafka-unit/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chbatey%2Fkafka-unit/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chbatey%2Fkafka-unit/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/chbatey","download_url":"https://codeload.github.com/chbatey/kafka-unit/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247535519,"owners_count":20954576,"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":[],"created_at":"2024-08-02T18:01:00.532Z","updated_at":"2025-04-06T19:13:08.624Z","avatar_url":"https://github.com/chbatey.png","language":"Java","funding_links":[],"categories":["Libraries","Kafka","Development"],"sub_categories":["Kafka","Spring Cloud框架","Testing"],"readme":"# Kafka Unit Testing\n\n![TravisCI](https://travis-ci.org/chbatey/kafka-unit.svg?branch=master)\n\nAllows you to start and stop a Kafka broker + ZooKeeper instance for unit testing applications that communicate with Kafka.\n\n## Versions\n| kafka-unit | Kafka broker            | Zookeeper |\n|------------|-------------------------|-----------|\n| 1.0        | kafka_2.11:0.11.0.0     | 3.4.10    |\n| 0.7        | kafka_2.11:0.10.0.2     | 3.4.10    |\n| 0.6        | kafka_2.11:0.10.0.0     | 3.4.6     |\n| 0.5        | kafka_2.11:0.9.0.1      | 3.4.6     |\n| 0.4        | kafka_2.11:0.9.0.1      | 3.4.6     |\n| 0.3        | kafka_2.11:0.8.2.2      | 3.4.6     |\n| 0.2        | kafka_2.11:0.8.2.1      | 3.4.6     |\n\n## Maven central\n\n```xml\n\u003cdependency\u003e\n    \u003cgroupId\u003einfo.batey.kafka\u003c/groupId\u003e\n    \u003cartifactId\u003ekafka-unit\u003c/artifactId\u003e\n    \u003cversion\u003e1.0\u003c/version\u003e\n\u003c/dependency\u003e\n```\n\n## Starting manually\n\nTo start both a Kafka server and ZooKeeper instance on random ports use following code:\n\n```java\nKafkaUnit kafkaUnitServer = new KafkaUnit();\nkafkaUnitServer.startup();\nkafkaUnitServer.shutdown();\n```\n\nZooKeeper and Kafka broker ports can be specified explicitly using second constructor, which takes two `int`s:\n\n```java\nKafkaUnit kafkaUnitServer = new KafkaUnit(5000, 5001);\n```\n\nThe alternative constructor allows providing connection strings rather than ports, which might be convenient if you want to use existing config without parsing it to extract port numbers:\n\n```java\nKafkaUnit kafkaUnitServer = new KafkaUnit(\"localhost:5000\", \"localhost:5001\");\n```\n\nCurrently only `localhost` is supported and it's required that the connection string consists of only one `localhost:[port]` pair.\n\nYou can then write your own code to interact with Kafka or use the following methods:\n\n```java\nkafkaUnitServer.createTopic(testTopic);\nProducerRecord\u003cString, String\u003e keyedMessage = new ProducerRecord\u003c\u003e(testTopic, \"key\", \"value\");\nkafkaUnitServer.sendMessages(keyedMessage);\n```\n\nAnd to read messages:\n\n```java\nList\u003cString\u003e messages = kafkaUnitServer.readMessages(testTopic, 1);\nList\u003cString\u003e allMessages = kafkaUnitServer.readAllMessages(testTopic);\n```\n\nOnly `String` messages are supported at the moment.\n\nAlternatively, you can use `getKafkaConnect()` to manually configure producer and consumer clients like:\n\n```java\nProperties props = new Properties();\nprops.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, LongSerializer.class.getCanonicalName());\nprops.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getCanonicalName());\nprops.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, kafkaUnitServer.getKafkaConnect());\n\nProducer\u003cLong, String\u003e producer = new KafkaProducer\u003c\u003e(props);\n```\n\n## Using the JUnit Rule\n\nIf you don't want to start/stop the server manually, you can use the JUnit rule, e.g.\n\n```java\npublic class KafkaUnitIntegrationTest {\n\n    @Rule\n    public KafkaUnitRule kafkaUnitRule = new KafkaUnitRule();\n\n    @Test\n    public void junitRuleShouldHaveStartedKafka() throws Exception {\n        String testTopic = \"TestTopic\";\n        kafkaUnitRule.getKafkaUnit().createTopic(testTopic);\n        ProducerRecord\u003cString, String\u003e keyedMessage = new ProducerRecord\u003c\u003e(testTopic, \"key\", \"value\");\n\n        kafkaUnitServer.sendMessages(keyedMessage);\n        List\u003cString\u003e messages = kafkaUnitRule.getKafkaUnit().readMessages(testTopic, 1);\n\n        assertEquals(Arrays.asList(\"value\"), messages);\n    }\n}\n```\n\nThis will start/stop the broker every test, so that particular test can't interfere with the next. \nContrary to `KafkaUnit()` constructor, it does not throw checked `IOException` when socket initialization fails, but wraps it in runtime exception and thus is suitable for use as `@Rule` field in tests.\n\nIf you want to start server on specific ports, use `KafkaUnitRule(int, int)` or `KafkaUnitRule(String, String)` constructor, which accepts ZooKeeper and Kafka broker ports or connection strings respectively (just like corresponding `KafkaUnit` constructors), e.g.:\n\n```java\n    @Rule\n    public KafkaUnitRule kafkaUnitRule = new KafkaUnitRule(5000, 5001);\n```\n\n## License\n\n```\nCopyright 2013 Christopher Batey\n\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License.\nYou may obtain a copy of the License at\n\n    http://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software\ndistributed under the License is distributed on an \"AS IS\" BASIS,\nWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\nSee the License for the specific language governing permissions and\nlimitations under the License.\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchbatey%2Fkafka-unit","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fchbatey%2Fkafka-unit","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchbatey%2Fkafka-unit/lists"}