Skip to content

kattlo/piemok

Repository files navigation

🥧 Piemok

Are you tired to run an Apache Kafka® broker for unit testing?

Use 🥧 Piemok and run tests faster.

Mocks for:

Support:

  • Java 11+
  • Apache Kafka® 2.6.0+

Getting Started

  1. Dependency
  • Gradle

    repositories {
        // ...
    
        maven {
            url = uri('http://packages.confluent.io/maven/')
        }
    
        maven { url 'https://jitpack.io' }
    }
    
    dependencies {
        testImplementation 'com.github.kattlo:piemok:v0.13.0'
    }
    
  • Apache Maven®

    <repositories>
    	<repository>
    	    <id>jitpack.io</id>
    	    <url>https://jitpack.io</url>
    	</repository>
    </repositories>
    
    <dependency>
        <groupId>com.github.kattlo</groupId>
        <artifactId>piemok</artifactId>
        <version>v0.13.0</version>
    </dependency>
  • See other options

  1. To test code that produces events
import io.github.kattlo.piemok.MockedProducer;

public class MyTestProducer {
    public void someTest() {

        var producer = MockedProducer.create();

        // --- pass the producer instance to your code --- //

        // get the produced records
        var records = producer.history();

        // do your assertions . ..

    }
}
  1. To test code that consumes topics by subscription
import io.github.kattlo.piemok.MockedConsumer;

public class MyTestBySubscription {
    public void someTest() {

        //                                          ***************
        var mocked = MockedConsumer.<String, String>forSubscribe();
        var topic = "my-topic";

        // add new record to be consumed in the poll() call
        mocked.reset(topic, "my-key", "my-value");

        // --- pass the mocked.consumer() to your code --- //

        // do your assertions . . .
    }
}
  1. To test code that consumes topics by assignment and seek
import io.github.kattlo.piemok.MockedConsumer;
import org.apache.kafka.common.TopicPartition;

public class MyTestByAssignment {
    public void someTest() {

        //                                          **********
        var mocked = MockedConsumer.<String, String>forSeek();
        var topic = "my-topic";

        // add new record to be consumed in the poll() call
        mocked.reset(topic, "my-key", "my-value");

        // --- pass the instance mocked.consumer() to your code --- //

        // do your assertions . . .
    }
}
  1. To test code with Kafka Avro Serializer, if you are running an embedded Apache Kafka® or using Testcontainers.
value.serializer=io.github.kattlo.piemok.MockedKafkaAvroSerializer
# . . .
  1. Use to test code with Kafka Avro Deserializer, if you are running and embedded Apache Kafka® or using Testcontainers.
value.deserializer=io.github.kattlo.piemok.MockedKafkaAvroDeserializer
# . . .

About

Mocking dor Apache Kafka® Producer and Consumer

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages