<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Patrick Charles Höfer</title>
    <description>The latest articles on DEV Community by Patrick Charles Höfer (@xetra11).</description>
    <link>https://dev.to/xetra11</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F697182%2F342e1b94-c63a-4e3a-83b2-921b5e8e9231.jpg</url>
      <title>DEV Community: Patrick Charles Höfer</title>
      <link>https://dev.to/xetra11</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/xetra11"/>
    <language>en</language>
    <item>
      <title>MockK - nifty mock features I recently discovered</title>
      <dc:creator>Patrick Charles Höfer</dc:creator>
      <pubDate>Tue, 19 Oct 2021 15:29:24 +0000</pubDate>
      <link>https://dev.to/xetra11/mockk-nifty-mock-features-i-recently-discovered-13ap</link>
      <guid>https://dev.to/xetra11/mockk-nifty-mock-features-i-recently-discovered-13ap</guid>
      <description>&lt;p&gt;I am using &lt;code&gt;MockK&lt;/code&gt; (&lt;a href="https://mockk.io/"&gt;https://mockk.io/&lt;/a&gt;) for a while now in all my Kotlin applications. But just recently I discovered some features I wish I knew beforehand because they really reduce the verbosity of my unit test.&lt;/p&gt;

&lt;p&gt;As a developer I am lazy. Therefore I just looked up MockK features I needed at the time without discovering all the other mentioned advanced features. I felt like I will find them when I need them. To be honest - I should have spent some time reading the documentation. Next time I definitely will!&lt;/p&gt;

&lt;p&gt;Here is a simple example that improved in readability thanks to some research on MockK features.&lt;/p&gt;

&lt;h2&gt;
  
  
  Logic to Test
&lt;/h2&gt;

&lt;p&gt;Here is the logic I wanted to test&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;dispatchEvent&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;taskPayload&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Json&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;encodeToString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;TaskPayload&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"123"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
        &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;gameEvent&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;GameEvent&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;GameEvent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;UUID&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;randomUUID&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="s"&gt;"task"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;DateTime&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;now&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;taskPayload&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;topic&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"projects/barbarus-game/topics/task"&lt;/span&gt;
        &lt;span class="nc"&gt;LOG&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;info&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"dispatch event to topic $topic"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;pubSubTemplate&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;publish&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;topic&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;gameEvent&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;addCallback&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nc"&gt;LOG&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;info&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;it&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nc"&gt;LOG&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"failure"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Unit Test Iteration #0
&lt;/h2&gt;

&lt;p&gt;And here is my initial unit test:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;    &lt;span class="k"&gt;internal&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;EventDispatcherTest&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;pubSubTemplate&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;PubSubTemplate&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;mockk&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;eventDispatcher&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;EventDispatcher&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;EventDispatcher&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pubSubTemplate&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="nd"&gt;@Test&lt;/span&gt;
    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;`should&lt;/span&gt; &lt;span class="n"&gt;publish&lt;/span&gt; &lt;span class="n"&gt;game&lt;/span&gt; &lt;span class="n"&gt;event&lt;/span&gt; &lt;span class="nf"&gt;message`&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;testTopic&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"projects/barbarus-game/topics/task"&lt;/span&gt;

        &lt;span class="nf"&gt;every&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;pubSubTemplate&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;publish&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;eq&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;testTopic&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;any&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;GameEvent&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;())&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="n"&gt;returns&lt;/span&gt; &lt;span class="nf"&gt;mockk&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nf"&gt;every&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nf"&gt;addCallback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;any&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="nf"&gt;any&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="n"&gt;returns&lt;/span&gt; &lt;span class="nc"&gt;Unit&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="n"&gt;eventDispatcher&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;dispatchEvent&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

        &lt;span class="nf"&gt;verify&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;pubSubTemplate&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;publish&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;eq&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;testTopic&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;any&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;GameEvent&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;())&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I simply want to see if the &lt;code&gt;dispatchEvent()&lt;/code&gt; function does call the &lt;code&gt;PubSubTemplate#publish&lt;/code&gt; function as expected.&lt;/p&gt;

&lt;p&gt;However &lt;code&gt;MockK#verify&lt;/code&gt; does not work withou the &lt;code&gt;pubSubTemplate&lt;/code&gt; mock being defined with a proxy. To satisfy the test runner I added an &lt;code&gt;every&lt;/code&gt; for &lt;code&gt;pubSubTemplate&lt;/code&gt; and also had to satisfy the return value (or answer in mockk-domain language). The return value is of type &lt;code&gt;ListenableFuture&lt;/code&gt;. However I do not need any specific behaviour from it so I just mocked it away.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="na"&gt;[...]&lt;/span&gt; &lt;span class="n"&gt;returns&lt;/span&gt; &lt;span class="nf"&gt;mockk&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nf"&gt;every&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nf"&gt;addCallback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;any&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="nf"&gt;any&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="n"&gt;returns&lt;/span&gt; &lt;span class="nc"&gt;Unit&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is the part where I mock the &lt;code&gt;ListenableFuture&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;All of that I had to do, to simply just use &lt;code&gt;verify&lt;/code&gt; and assert that my &lt;code&gt;dispatchEvent&lt;/code&gt; function is calling the &lt;code&gt;PubSubTemplate#$publish&lt;/code&gt; function with the expected &lt;code&gt;topic&lt;/code&gt; string.&lt;/p&gt;

&lt;p&gt;I did a quick google research to see what I can do better.&lt;/p&gt;

&lt;h2&gt;
  
  
  Unit Test Iteration #1
&lt;/h2&gt;

&lt;p&gt;The first thing I was wondering about was, if there is a MockK idiomatic way to mock a function that returns &lt;code&gt;Unit&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;And I found this sweet article/documentation:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://notwoods.github.io/mockk-guidebook/docs/mockito-migrate/void/"&gt;https://notwoods.github.io/mockk-guidebook/docs/mockito-migrate/void/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So instead of using &lt;code&gt;returns Unit&lt;/code&gt; one can simply replace &lt;code&gt;every&lt;/code&gt; with &lt;code&gt;justRun&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The statement now looks a bit slicker&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;        &lt;span class="nf"&gt;every&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;pubSubTemplate&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;publish&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;eq&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;testTopic&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;any&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;GameEvent&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;())&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="n"&gt;returns&lt;/span&gt; &lt;span class="nf"&gt;mockk&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nf"&gt;justRun&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nf"&gt;addCallback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;any&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="nf"&gt;any&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But wait! There is more!&lt;/p&gt;

&lt;h3&gt;
  
  
  Unit Test Iteration #2
&lt;/h3&gt;

&lt;p&gt;So still I am unhappy with &lt;code&gt;verify&lt;/code&gt; being dependant on the &lt;code&gt;every&lt;/code&gt; mock of &lt;code&gt;pubSubTemplate&lt;/code&gt;. I then stumbled over &lt;a href="https://mockk.io/#relaxed-mock"&gt;https://mockk.io/#relaxed-mock&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I had no idea what "relaxed" might imply in the context of unit testing. So I took a little research. It says "allows creation with no specific behaviour". So all functions are returning "Simple Values" by default - so me as a developer doesn't need to implement a behaviour with &lt;code&gt;every&lt;/code&gt; for the called functions of that unit under test.&lt;/p&gt;

&lt;p&gt;The only thing I needed to do was to call the &lt;code&gt;mockk()&lt;/code&gt; function with &lt;code&gt;relaxed = true&lt;/code&gt; argument.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;
&lt;span class="k"&gt;internal&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;EventDispatcherTest&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;pubSubTemplate&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;PubSubTemplate&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;mockk&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;relaxed&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;eventDispatcher&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;EventDispatcher&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;EventDispatcher&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pubSubTemplate&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="nd"&gt;@Test&lt;/span&gt;
    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;`should&lt;/span&gt; &lt;span class="n"&gt;publish&lt;/span&gt; &lt;span class="n"&gt;game&lt;/span&gt; &lt;span class="n"&gt;event&lt;/span&gt; &lt;span class="nf"&gt;message`&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;testTopic&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"projects/barbarus-game/topics/task"&lt;/span&gt;
        &lt;span class="n"&gt;eventDispatcher&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;dispatchEvent&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="nf"&gt;verify&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;pubSubTemplate&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;publish&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;eq&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;testTopic&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;any&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;GameEvent&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;())&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And voila the simple &lt;code&gt;verify&lt;/code&gt; assertion does it job without the need of a behaviour definition of the mocked class.&lt;/p&gt;

&lt;h2&gt;
  
  
  Final Words
&lt;/h2&gt;

&lt;p&gt;I hope I could help one or two developers with this. I'd wish I knew it earlier but hey - better now than never eh?&lt;/p&gt;

</description>
      <category>kotlin</category>
      <category>mockk</category>
      <category>mocking</category>
      <category>testing</category>
    </item>
    <item>
      <title>How I established TDD into my programming routine</title>
      <dc:creator>Patrick Charles Höfer</dc:creator>
      <pubDate>Mon, 27 Sep 2021 15:23:39 +0000</pubDate>
      <link>https://dev.to/xetra11/how-i-established-tdd-into-my-programming-routine-4e7b</link>
      <guid>https://dev.to/xetra11/how-i-established-tdd-into-my-programming-routine-4e7b</guid>
      <description>&lt;h2&gt;
  
  
  Table Of Contents
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Preface&lt;/li&gt;
&lt;li&gt;Misconceptions&lt;/li&gt;
&lt;li&gt;Things to Learn&lt;/li&gt;
&lt;li&gt;How I do it&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Preface &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;I have been working as a developer for about six years now. I started rather late in this career when I was 27 years old. That said it has always been my inner call to make up for that late start by trying to be as skilled and proficient at programming as possible as I could. Reading about best practices and all the Do's and Don'ts was therefore inevitable. However, one particular topic was pretty hard to grasp for me personally. The problem was not understanding it but more how to apply it to my everyday programming work routine. It was Test Driven Development - or how most people would call it TDD.&lt;/p&gt;

&lt;p&gt;TDD really changed my overall efficiency. I was often praised for being disciplined using Test Driven Development. It was not easy for me at first and it took me around two years to really manifest into my daily routine. With this article, I hope to speed up the time to master TDD for other developers.&lt;/p&gt;

&lt;p&gt;If you are not familiar with TDD yet just check out this Youtube Tutorial. Afterwards you can read on and see how I managed to bring TDD into my programming routine.&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/z6gOPonp2t0"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h3&gt;
  
  
  Misconceptions about TDD &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;When mentioning TDD in companies I worked in I often received a subtle 'sigh' or other eye-roll-related expressions from some of my new colleagues. The common counterarguments were mostly revolving around the idea that TDD is a waste of time and writing code with the TDD paradigm or tests at all are just not proficient for everyday use.&lt;/p&gt;

&lt;p&gt;To achieve those benefits you have to "learn" how to write tests. It's similar to learning the API of a new framework or library. At first you're slow with it and often have to look up the documentation. Writing a custom solution might be "faster" one could say. But after you mastered an API you usually are way more efficient and faster in writing solutions covered by those frameworks.&lt;/p&gt;

&lt;p&gt;Exactly the same applies for testing and foremost &lt;em&gt;Test Driven Development&lt;/em&gt;. However one has to force themself to use TDD. It's tempting to go around and use the 'shortcut' and just write the business logic without TDD or tests at all. Try to avoid those shortcuts. Believe me it's worth it.&lt;/p&gt;

&lt;p&gt;Also writing a test is not &lt;strong&gt;just&lt;/strong&gt; writing the test. You first write a test specification. Then you write the &lt;strong&gt;business logic&lt;/strong&gt; to fulfil the specification and afterwards you apply refactoring if necessary. One could claim that at least 50% of the time writing a test is actually writing the actual code or business logic. And the faster you are in writing the test specification the greater is the amount of business logic written under the hood of TD (&lt;strong&gt;plus&lt;/strong&gt; the benefits of TDD!).&lt;/p&gt;

&lt;p&gt;In the following chapter I want to point out what things you need to understand and learn to use TDD efficiently.&lt;/p&gt;

&lt;h3&gt;
  
  
  Things you need to master to be efficient &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;To reduce the time spent writing tests in terms of &lt;em&gt;Test Driven Development&lt;/em&gt; you should know how to use testing frameworks and concepts. The less time you spent on reading up documentation the less time it takes you to write a test and the earlier you can focus on the actual business logic.&lt;/p&gt;

&lt;p&gt;Here is a quick listing of things that are important to you to master:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Testing Frameworks (JUnit, Jest, Mokka, etc.)&lt;/li&gt;
&lt;li&gt;Assertion Frameworks (AssertJ, AssertK, Chai, etc.)&lt;/li&gt;
&lt;li&gt;Mocking Frameworks (MockK, Mockito, etc.)&lt;/li&gt;
&lt;li&gt;Testing Concepts (TDD, BDD, etc.)&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Translate task requirements into test specifications&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And last but not least: Write tests as often as you can to drill the routine. Writing a test needs to be as trivial to you as writing a function in your favourite programming language.&lt;/p&gt;

&lt;h3&gt;
  
  
  My TDD routine &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;In the following I'd like to show an example on how I implement feature specifications using TDD.&lt;/p&gt;

&lt;p&gt;Let's start with a fictional feature request. The task description/request might look like this:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Hey there,&lt;/p&gt;

&lt;p&gt;I need a service that converts a string to either uppercase or lowercase depending on the first character being capitalised or not.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
"breAD" -&amp;gt; "bread"&lt;br&gt;
"House" -&amp;gt; "HOUSE"&lt;br&gt;
"gIRAFFE" -&amp;gt; "giraffe"&lt;/p&gt;

&lt;p&gt;Thanks for your help!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let's start by using the following steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Understand the requirement&lt;/li&gt;
&lt;li&gt;Split the requirement into small independent specifications&lt;/li&gt;
&lt;li&gt;Implement one by one

&lt;ul&gt;
&lt;li&gt;Write the test as an expected assertion&lt;/li&gt;
&lt;li&gt;Run the test into failure (&lt;strong&gt;Red&lt;/strong&gt;)&lt;/li&gt;
&lt;li&gt;Implement business logic of the feature and run the test into success (&lt;strong&gt;Green&lt;/strong&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Refactor&lt;/em&gt; implemented business logic if necessary (&lt;strong&gt;Refactor&lt;/strong&gt;)&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;
  
  
  Understand the requirement
&lt;/h4&gt;

&lt;p&gt;The product owner (or whoever) wants me to implement a feature where a string is converted to an uppercase representation if the first letter of the string is capitalised. If it is not then the string should be converted to all lowercase. Sounds easy.&lt;/p&gt;

&lt;h4&gt;
  
  
  Split into small and independent problems
&lt;/h4&gt;

&lt;p&gt;The complete task should now be split into smaller chunks of subtasks for the sake of writing small and easy to read (and write) unit tests. In this case, we have only two criteria.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;The feature...&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;should convert strings that start with capital letters to uppercase strings&lt;/li&gt;
&lt;li&gt;should convert strings that start with non-capital letters to lowercase strings&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;What &lt;strong&gt;I&lt;/strong&gt; do at this point is that I write down those "subtasks" directly as unit test signatures. That way I have a &lt;em&gt;in-code&lt;/em&gt; TODO or Acceptance Criteria list and can work on it right away without the need to look into the task description again (usually).&lt;/p&gt;

&lt;p&gt;Here is an example written in JUnit5 and Kotlin:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="c1"&gt;// CaseConverterTest.kt&lt;/span&gt;
 &lt;span class="nd"&gt;@Test&lt;/span&gt;
 &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;`should&lt;/span&gt; &lt;span class="n"&gt;convert&lt;/span&gt; &lt;span class="n"&gt;strings&lt;/span&gt; &lt;span class="n"&gt;that&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt; &lt;span class="n"&gt;with&lt;/span&gt; &lt;span class="n"&gt;capital&lt;/span&gt; &lt;span class="n"&gt;letters&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="n"&gt;uppercase&lt;/span&gt; &lt;span class="nf"&gt;strings`&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
 &lt;span class="nd"&gt;@Test&lt;/span&gt;
 &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;`should&lt;/span&gt; &lt;span class="n"&gt;convert&lt;/span&gt; &lt;span class="n"&gt;strings&lt;/span&gt; &lt;span class="n"&gt;that&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt; &lt;span class="n"&gt;with&lt;/span&gt; &lt;span class="n"&gt;non-capital&lt;/span&gt; &lt;span class="n"&gt;letters&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="n"&gt;lowercase&lt;/span&gt; &lt;span class="nf"&gt;strings`&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;(To remind yourself to implement those specifications you could add &lt;code&gt;assertTrue(false)&lt;/code&gt; or something like that to force failure on those unit tests.)&lt;/em&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Implement one by one
&lt;/h4&gt;

&lt;p&gt;Now let's get to work on the actual testing logic. As I mentioned in Things to Learn the pace at which you actually write test code depends on your experience with the aforementioned testing frameworks.&lt;/p&gt;

&lt;p&gt;In this example we have two classes. The &lt;code&gt;CaseConverter&lt;/code&gt; that is the actual business logic class and the &lt;code&gt;CaseConverterTest&lt;/code&gt; that runs the unit tests.&lt;/p&gt;

&lt;p&gt;Here is the way I setup my test logic before I start to write down any business logic for the feature at all.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="c1"&gt;// CaseConverterTest.kt&lt;/span&gt;

&lt;span class="nd"&gt;@Test&lt;/span&gt;
&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;`should&lt;/span&gt; &lt;span class="n"&gt;convert&lt;/span&gt; &lt;span class="n"&gt;strings&lt;/span&gt; &lt;span class="n"&gt;that&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt; &lt;span class="n"&gt;with&lt;/span&gt; &lt;span class="n"&gt;capital&lt;/span&gt; &lt;span class="n"&gt;letters&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="n"&gt;uppercase&lt;/span&gt; &lt;span class="nf"&gt;strings`&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;caseConverter&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;CaseConverter&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;CaseConverter&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;givenString&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"House"&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;expectedString&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"HOUSE"&lt;/span&gt;

    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;convertedString&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;caseConverter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;convert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;givenString&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="nf"&gt;assertThat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;convertedString&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;isEqualTo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;expectedString&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Executing this test will result in &lt;em&gt;failure&lt;/em&gt; and this is fine. Because we did not implement any business logic just yet and we want to confirm that the test is properly asserting the expected behaviour.&lt;/p&gt;

&lt;p&gt;In TDD terms this is the &lt;strong&gt;Red&lt;/strong&gt; step.&lt;/p&gt;

&lt;p&gt;This is what my assertion framework (AssertK) tells me in the console:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;expected:&amp;lt;"[HOUSE]"&amp;gt; but was:&amp;lt;"[]"&amp;gt;&lt;br&gt;
Expected :"[HOUSE]"&lt;br&gt;
Actual   :"[]"&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now let's continue to the &lt;strong&gt;Green&lt;/strong&gt; step where we implement the expected business logic. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;The nice thing with TDD now is that we can rely on the test assertion while writing business logic. When writing code without tests one always has to build the application, run it and "use" it to assert the expected behaviour by hand. Tests are doing this for you. Why would a developer not want to have that?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Here is the business logic implementation to make the first test specification happy:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="c1"&gt;// CaseConverter.kt&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;CaseConverter&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;convert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stringToConvert&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stringToConvert&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;first&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;isUpperCase&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;stringToConvert&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;uppercase&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="s"&gt;""&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will result in a successful test execution.&lt;/p&gt;

&lt;p&gt;Now let's continue with the next test specification. For this test, I use the given string &lt;code&gt;breAD&lt;/code&gt; from the task/feature description above. The resulting string should be all lowercase -&amp;gt; &lt;code&gt;bread&lt;/code&gt;. Let's write this as a test.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="c1"&gt;// CaseConverterTest.kt&lt;/span&gt;

&lt;span class="nd"&gt;@Test&lt;/span&gt;
&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;`should&lt;/span&gt; &lt;span class="n"&gt;convert&lt;/span&gt; &lt;span class="n"&gt;strings&lt;/span&gt; &lt;span class="n"&gt;that&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt; &lt;span class="n"&gt;with&lt;/span&gt; &lt;span class="n"&gt;non-capital&lt;/span&gt; &lt;span class="n"&gt;letters&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="n"&gt;lowercase&lt;/span&gt; &lt;span class="nf"&gt;strings`&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;caseConverter&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;CaseConverter&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;CaseConverter&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;givenString&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"breAD"&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;expectedString&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"bread"&lt;/span&gt;

    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;convertedString&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;caseConverter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;convert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;givenString&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="nf"&gt;assertThat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;convertedString&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;isEqualTo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;expectedString&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Nothing much of a difference here in comparison to the first test specification. Still, it's important to keep these small differences separated from each other.&lt;/p&gt;

&lt;p&gt;A developer might be tempted to to extract reusable code. Do not do it in tests. A test needs to be read as is and inline. Reading a test should not result in jumping through different extracted methods/functions. &lt;em&gt;(I am quite dogmatic regarding this topic so that's still just my personal opinion)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;If your tests becomes too large and unreadable then that is a definite &lt;em&gt;code smell&lt;/em&gt; for your business logic or problem/feature not being split into smaller pieces or separated responsibilities. Do not blame the test. Refactor your business logic.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Now let's continue make the second test happy by adding the expected behaviour to the business logic:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="c1"&gt;// CaseConverter.kt&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;CaseConverter&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;convert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stringToConvert&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stringToConvert&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;first&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;isUpperCase&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;stringToConvert&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;uppercase&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stringToConvert&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;first&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;isLowerCase&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;stringToConvert&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;lowercase&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="s"&gt;""&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now both tests are going &lt;em&gt;Green&lt;/em&gt;. The feature therefore is implemented and the requirements/acceptance criteria fulfilled.&lt;/p&gt;

&lt;p&gt;One final thing we can do now is to &lt;em&gt;Refactor&lt;/em&gt;. Refactoring being the last step in this case is quite important. Now that you have a unit test that guarantees the expected behaviour for your business logic you can go nuts on the refactoring. Make it as nice as you want to. As long as the test is going &lt;em&gt;Green&lt;/em&gt; you are fine.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;(To be honest here. A unit test isn't a guarantee for anything since you wrote it by yourself. You have to rely on your own test quality)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;For the sake of completeness here is the refactored business logic:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="c1"&gt;// CaseConverter.kt&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;CaseConverter&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;convert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stringToConvert&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stringToConvert&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;first&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;isUpperCase&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;stringToConvert&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;uppercase&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="n"&gt;stringToConvert&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;lowercase&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The tests are still &lt;em&gt;Green&lt;/em&gt; and everyone is happy.&lt;/p&gt;

&lt;p&gt;That's it about my way of using TDD in my programming routine. I managed to start with test specifications instead of business logic and am very happy with this style. I could never go back from this, and I feel very uncomfortable working on an established codebase where it's troublesome or even impossible to write tests for.&lt;/p&gt;

&lt;p&gt;I hope I could inspire some of you non-testing developers to join the "right" path ;)&lt;/p&gt;

</description>
      <category>testing</category>
      <category>tdd</category>
      <category>unittests</category>
      <category>testdrivendevelopment</category>
    </item>
  </channel>
</rss>
