The project I took it from is Camus which is Kafka->Hadoop ETL project, and I just made some slight changes related to newer Zookeeper, as well as some changes configuration-wise.
My embedded Kafka & Zookeeper servers are available at this gist. All the code is tested against Kafka 0.8.1.1 and Zookeeper 3.4.6.
Here is simple example on how to setup Zookeeper at fixed port (2181), and 2 Kafka servers at random available ports:
EmbeddedZookeeper embeddedZookeeper = new EmbeddedZookeeper(2181); List<Integer> kafkaPorts = new ArrayList<Integer>(); // -1 for any available port kafkaPorts.add(-1); kafkaPorts.add(-1); EmbeddedKafkaCluster embeddedKafkaCluster = new EmbeddedKafkaCluster(embeddedZookeeper.getConnection(), new Properties(), kafkaPorts); embeddedZookeeper.startup(); System.out.println("### Embedded Zookeeper connection: " + embeddedZookeeper.getConnection()); embeddedKafkaCluster.startup(); System.out.println("### Embedded Kafka cluster broker list: " + embeddedKafkaCluster.getBrokerList()); Thread.sleep(10000); embeddedKafkaCluster.shutdown(); embeddedZookeeper.shutdown();