<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Onur Süme on Medium]]></title>
        <description><![CDATA[Stories by Onur Süme on Medium]]></description>
        <link>https://medium.com/@onursume?source=rss-b7040272dc40------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*dLKfYSuAig92Q81FpaIVHw.jpeg</url>
            <title>Stories by Onur Süme on Medium</title>
            <link>https://medium.com/@onursume?source=rss-b7040272dc40------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 05:12:37 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@onursume/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Kafka Test Containers with GoLang]]></title>
            <link>https://medium.com/trendyol-tech/kafka-test-containers-with-golang-b85e4b2469db?source=rss-b7040272dc40------2</link>
            <guid isPermaLink="false">https://medium.com/p/b85e4b2469db</guid>
            <category><![CDATA[kafka]]></category>
            <category><![CDATA[platform]]></category>
            <category><![CDATA[testcontainer]]></category>
            <category><![CDATA[golang]]></category>
            <dc:creator><![CDATA[Onur Süme]]></dc:creator>
            <pubDate>Mon, 21 Dec 2020 13:27:49 GMT</pubDate>
            <atom:updated>2021-07-27T08:28:18.565Z</atom:updated>
            <content:encoded><![CDATA[<p>Do we need test containers?</p><ul><li>What does Test Container mean? Why do we use it?</li><li>How did we implement it?</li><li>How did we use the Kafka test container?</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/1*Ma3YUjOWuvH3MCfRtDZ9Zg.png" /></figure><p>We take care to develop applications according to the test-driven development approach in Trendyol. There may also be a need for integration or end-to-end tests to make sure that more than one unit can work together. These enable us to make deployment in the production environment more secure.</p><p>In an incident we went through a few months ago, it took us quite a while to understand where the error was due to a configuration we forgot to do. Thus, we understood the importance of integration testing much more there. In this article, I am going to mention the test containers that are one of the basic needs in integration test processes.</p><h4>What does Test Container mean &amp; Why do we use it?</h4><p>It provides an environment that simulates the dependencies of the application and allows for running it independently of these dependencies by using docker container technology.</p><p>Unlike unit tests, one of the biggest problems in integration test processes is the necessity of testing with dependencies. To perform integration or end-to-end tests with dependencies such as database, messaging systems, Kafka need to exist independently from the application. However instead of using real environments, it just needs simulation environments that will live while testing and then die. It is why test containers are necessary. It creates a simulation environment for the application.</p><p>In this way, it guarantees to run successfully with all the dependencies it has had before deploying the application.</p><h4>How did we implement it?</h4><p>We have decided to use <a href="https://docs.docker.com/compose/"><em>docker-compose</em></a> for the management of dependencies. We define a container configuration for each dependency to create it with docker-compose.</p><p>When testing the application that needs containers, we run our tests by managing the creation and killing processes of these containers. Thus, we reduce the code complexity by separating the container configurations from the application.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/612/1*tFAFhtXbwH95zJ1nxmBwzA.png" /></figure><h4>How did we use Kafka Test Container?</h4><p>To simulate the Kafka environment, Zookeeper and Kafka containers must be created. They are created according to the docker-compose.yml file that you see below. You can set the ports as default for Kafka and Zookeeper.</p><pre>version: &quot;3&quot;<br>services:<br>  zookeeper:<br>    image: wurstmeister/zookeeper:latest<br>    container_name: zookeeper<br>    expose:<br>      - &lt;ZOOKEEPER_PORT&gt;</pre><pre>kafka:<br>    image: wurstmeister/kafka:latest<br>    container_name: kafka<br>    depends_on:<br>      - zookeeper<br>    ports:<br>      - &quot;&lt;KAFKA_PORT&gt;:&lt;KAFKA_PORT&gt;&quot;<br>    environment:<br>      KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://localhost:&lt;KAFKA_PORT&gt;<br>      KAFKA_LISTENERS: PLAINTEXT://0.0.0.0:&lt;KAFKA_PORT&gt;<br>      KAFKA_ZOOKEEPER_CONNECT: zookeeper:&lt;ZOOKEEPER_PORT&gt;<br>      KAFKA_CREATE_TOPICS: &quot;test-topic:1:1&quot;</pre><p>Let’s test a code snippet written in Golang that claims to be sending a message to a topic in Kafka. We are not concerned with how this code works. As the name suggests, it does the job of sending messages. Let’s see the test class that checks if it does this job correctly.</p><pre>func TestSendMessage(t *testing.T) {<br>   //given<br>   kafka := testcontainers.NewLocalDockerCompose(<br>      []string{configs.RootDir() + &quot;kafka/test-docker-compose.yml&quot;},<br>      strings.ToLower(uuid.New().String()),<br>   )</pre><pre>   kafka.WithCommand([]string{&quot;up&quot;, &quot;-d&quot;}).Invoke()<br>   time.Sleep(5 * time.Second)<br>   defer destroyKafka(kafka)</pre><pre>   //when<br>   SendMessage(&quot;test-topic&quot;, &quot;test-key&quot;, &quot;test-message&quot;)</pre><pre>   //then<br>   result := read()</pre><pre>   require.Equal(t, result[&quot;test-key&quot;], &quot;test-message&quot;)<br>}</pre><pre>func read() map[string]string {<br>   r := kafka.NewReader(kafka.ReaderConfig{<br>      Brokers: []string{&quot;localhost:9092&quot;},<br>      Topic:   &quot;test-topic&quot;,<br>   })</pre><pre>   m, _ := r.ReadMessage(context.Background())<br>   defer r.Close()<br>   <br>   result := make(map[string]string)<br>   result[string(m.Key)] = string(m.Value)<br>   return result<br>}</pre><pre>func destroyKafka(compose *testcontainers.LocalDockerCompose) {<br>   compose.Down()<br>   time.Sleep(1 * time.Second)<br>}</pre><ul><li>We create a docker-compose and send the “up” command to it to start the containers.</li><li>Then, we call the function that sends a message to the relevant topic in Kafka.</li><li>We read the message from the Kafka container to check if it is delivered correctly.</li><li>Then, we kill these containers with the “down” command.</li></ul><p>Thereby, we have successfully tested the code snippet that depends upon Kafka by creating a Kafka container. You can see the Kafka test containers project that we use in this application at the link below.</p><p><a href="https://github.com/testcontainers/testcontainers-go">https://github.com/testcontainers/testcontainers-go</a></p><p>In summary, we can use docker technology for the management of dependencies in integration test processes. Libraries such as test containers run by using this technology. These containers are necessary to test an application independently of all dependencies. Thus, the application can be tested as independent from the real environment.</p><p>We tried to understand the test containers through the test of a function sending a message to Kafka. In the example, we wrote a test of a function that sends a message to Kafka. To simulate a Kafka environment, we created a container. And, we examined whether the message was sent successfully to Kafka in that environment.</p><p>Thanks for reading.</p><p>I hope you find this article helpful.</p><h4>References</h4><p><a href="https://testing.googleblog.com/2015/04/just-say-no-to-more-end-to-end-tests.html">https://testing.googleblog.com/2015/04/just-say-no-to-more-end-to-end-tests.html</a></p><p>You can apply via the link below to join us.</p><p><a href="https://jobs.lever.co/trendyol?lever-via=BXDkB1Lo4z">Trendyol jobs</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b85e4b2469db" width="1" height="1" alt=""><hr><p><a href="https://medium.com/trendyol-tech/kafka-test-containers-with-golang-b85e4b2469db">Kafka Test Containers with GoLang</a> was originally published in <a href="https://medium.com/trendyol-tech">Trendyol Tech</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>