<?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: Rellyson Silva</title>
    <description>The latest articles on DEV Community by Rellyson Silva (@rellyson).</description>
    <link>https://dev.to/rellyson</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%2F392268%2Fa9bc6bfc-28cc-45fb-8779-f461b32c595d.png</url>
      <title>DEV Community: Rellyson Silva</title>
      <link>https://dev.to/rellyson</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rellyson"/>
    <language>en</language>
    <item>
      <title>Test Doubles: Replacing real objects for testing</title>
      <dc:creator>Rellyson Silva</dc:creator>
      <pubDate>Wed, 18 Aug 2021 17:24:30 +0000</pubDate>
      <link>https://dev.to/rellyson/test-doubles-replacing-real-objects-for-testing-2pfg</link>
      <guid>https://dev.to/rellyson/test-doubles-replacing-real-objects-for-testing-2pfg</guid>
      <description>&lt;h2&gt;
  
  
  What it is?
&lt;/h2&gt;

&lt;p&gt;Test Doubles are objects &lt;strong&gt;that mimic real objects&lt;/strong&gt;. This give us the possibility to &lt;strong&gt;simulate an external dependency&lt;/strong&gt; to the SUT (System under Test) or any other real element under test. This approach is &lt;strong&gt;widely used in unit tests&lt;/strong&gt;, focused in &lt;strong&gt;ensure the unit's assertiveness&lt;/strong&gt;, ignoring anything outside this scope.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Test Double is a generic term for any case where you replace a production object for testing purposes." ~Martin Flower.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Gerard Meszaros (creator of Test Doubles) divides this into five categories: &lt;strong&gt;Mocks, Stubs, Fakes, Spies e Dummies.&lt;/strong&gt; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Dummy:&lt;/strong&gt; is an object passed around but never actually used. Used to fill parameter lists.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Fake:&lt;/strong&gt; is an object that have working implementations, but are not suitable for production.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Spies&lt;/strong&gt;: are stubs that record some information based on how they were called. One example of this might be an email service that records how many messages it was sent.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Mocks:&lt;/strong&gt; predefined implementations with expectations which form a specification of the calls they are expected to receive. They can throw an exception if they receive a call they don't expect and are checked during verification to ensure they got all the calls they were expecting.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Stubs:&lt;/strong&gt; provide canned answers to calls made during the test, not responding at all to anything outside what's programmed in for the test.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Furthermore, &lt;strong&gt;these five categories can be reduced&lt;/strong&gt; into just two groups: &lt;strong&gt;Mocks and Stubs.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_92iSPaF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://miro.medium.com/max/1290/1%2AYzzk9bNkuXumtXPNVoDorQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_92iSPaF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://miro.medium.com/max/1290/1%2AYzzk9bNkuXumtXPNVoDorQ.png" width="645" height="343"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt; Removed from Unit Testing Principles, Practices, and Patterns (Vladimir Khorikov).&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Mocks and Stubs
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Mocks
&lt;/h3&gt;

&lt;p&gt;Mocks are generally used to &lt;strong&gt;simulate outbound interactions.&lt;/strong&gt; They are used to control and inspect &lt;strong&gt;fake calls&lt;/strong&gt; to &lt;strong&gt;a dependency&lt;/strong&gt;. We can simulate and test if one or more methods &lt;strong&gt;were called/not called&lt;/strong&gt;,  &lt;strong&gt;the order&lt;/strong&gt; in which these methods were called, whether those methods &lt;strong&gt;were called with the right arguments&lt;/strong&gt;, and &lt;strong&gt;how many times&lt;/strong&gt; they were called.&lt;/p&gt;

&lt;h4&gt;
  
  
  Usage
&lt;/h4&gt;

&lt;p&gt;✅ You can use &lt;strong&gt;Mocks&lt;/strong&gt; to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Test if external dependency methods were called as expected.&lt;/li&gt;
&lt;li&gt;Test how many times an external dependency method were called.&lt;/li&gt;
&lt;li&gt;Test if an external dependency method was called with the correct arguments.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;🚫 Do not use &lt;strong&gt;Mocks&lt;/strong&gt; to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;To test function return values.&lt;/li&gt;
&lt;li&gt;To test function behaviors.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Stubs
&lt;/h3&gt;

&lt;p&gt;Stubs are generally used to &lt;strong&gt;simulate inbound interactions.&lt;/strong&gt; Different from Mock, &lt;strong&gt;stubs are  defined objects,&lt;/strong&gt; ready to be used. They &lt;strong&gt;don't know how to answer how many times an external dependency method was called/not called and which parameters were used&lt;/strong&gt;, they just return an object already defined. In other words, a stub is the final return of a method.&lt;/p&gt;

&lt;h4&gt;
  
  
  Usage
&lt;/h4&gt;

&lt;p&gt;✅ You can use &lt;strong&gt;Stubs&lt;/strong&gt; to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Test the SUT behaviors when receiving different returns from an external dependency.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;🚫 Do not use &lt;strong&gt;Stubs&lt;/strong&gt; to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Test if external dependency methods were called.&lt;/li&gt;
&lt;li&gt;Test external dependency methods behaviors.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What about Fakes, Spies and Dummies?
&lt;/h2&gt;

&lt;p&gt;They are complementary to the groups cited, helping the proper creation of Mocks and Stubs.&lt;/p&gt;

&lt;h3&gt;
  
  
  Fakes
&lt;/h3&gt;

&lt;p&gt;Fakes (also known as &lt;strong&gt;Fakers&lt;/strong&gt;) are &lt;strong&gt;real implementations of an external dependency&lt;/strong&gt;, but are not suitable to production. They are generally used when we &lt;strong&gt;need a real functionality (or as close as possible to a real one)&lt;/strong&gt;. The most common example is to use an in-memory database to persist data. Fakers are useful in functional testing or integration testing, but are not recommended for unit testing where we can supply our needs with stubs.&lt;/p&gt;

&lt;h3&gt;
  
  
  Spies
&lt;/h3&gt;

&lt;p&gt;Spies are objects &lt;strong&gt;that record their interactions to other objects&lt;/strong&gt; (can be compared to a combination of Mock and Stub). They can be &lt;strong&gt;used to test both inbound and outbound interactions.&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Dummies
&lt;/h3&gt;

&lt;p&gt;Dummies are &lt;strong&gt;the simplest piece of an test&lt;/strong&gt;, used to fill a method signature. Their only &lt;strong&gt;purpose is to fill a necessary space&lt;/strong&gt;, having no effect in the test.&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://martinfowler.com/articles/mocksArentStubs.html"&gt;Mocks aren't Stubs&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.amazon.com.br/Unit-Testing-Principles-Practices-Patterns/dp/1617296279"&gt;Unit Testing Principles, Practices, and Patterns&lt;/a&gt;&lt;/p&gt;

</description>
      <category>testing</category>
      <category>tdd</category>
      <category>refactoring</category>
    </item>
    <item>
      <title>Abstract class vs interfaces. When to use them?</title>
      <dc:creator>Rellyson Silva</dc:creator>
      <pubDate>Wed, 28 Jul 2021 18:56:51 +0000</pubDate>
      <link>https://dev.to/rellyson/abstract-class-vs-interfaces-when-to-use-them-1d68</link>
      <guid>https://dev.to/rellyson/abstract-class-vs-interfaces-when-to-use-them-1d68</guid>
      <description>&lt;h2&gt;
  
  
  Abstract class
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;"When we talk about abstract classes we are defining characteristics of an object type; specifying **what an object is.&lt;/em&gt;&lt;em&gt;" ~Jorge, 2017.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;An abstract class is &lt;strong&gt;a "template"&lt;/strong&gt; for classes, cannot be instantiated and should be &lt;strong&gt;extended&lt;/strong&gt; by a class. It is used to define some common functionality across a set of related classes and allowing default method implementations.&lt;/p&gt;

&lt;p&gt;Abstract classes are defined using the keyword &lt;code&gt;abstract&lt;/code&gt;. It can have both abstract and regular methods and can only be accessed in classes that inherit it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;abstract&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Animal&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;abstract&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;makeSound&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;sleep&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Zzz"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Dog&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Animal&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;makeSound&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Uff uff!"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Main&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;Dog&lt;/span&gt; &lt;span class="n"&gt;snoopy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Dog&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="n"&gt;snoopy&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;makeSound&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="n"&gt;snoopy&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;sleep&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  When to use abstract classes?
&lt;/h3&gt;

&lt;h4&gt;
  
  
  When you need to implement code
&lt;/h4&gt;

&lt;p&gt;Different from interfaces, abstract class &lt;strong&gt;can have method implementations&lt;/strong&gt;. This helps having &lt;strong&gt;code re-usability&lt;/strong&gt; and to ensure that a class contains &lt;strong&gt;its expected behaviors&lt;/strong&gt;, by extending an existing method or implementing an abstract method already defined in the abstract class.   &lt;/p&gt;

&lt;h4&gt;
  
  
  When you need encapsulation
&lt;/h4&gt;

&lt;p&gt;Different from interfaces, abstract classes allow us to &lt;strong&gt;have encapsulated methods and attributes&lt;/strong&gt;. It helps &lt;strong&gt;restricting  direct access&lt;/strong&gt; to the object's component and &lt;strong&gt;preventing unauthorized access&lt;/strong&gt; to them.&lt;/p&gt;

&lt;h2&gt;
  
  
  Interface
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;"When we talk about an interface and define capabilities that we promise to provide, we are talking about establishing a contract about **what the object can do.&lt;/em&gt;&lt;em&gt;" ~Jorge, 2017.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;An interface is a &lt;strong&gt;"contract"&lt;/strong&gt; and should be &lt;strong&gt;implemented&lt;/strong&gt; by a class*&lt;em&gt;.&lt;/em&gt;* When a class implements *&lt;strong&gt;*an interface, it must follow the behavior published by the interface. Interfaces generally defines properties and methods a class must have and unlike abstract class, **enable multiple inheritance&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Interfaces are defined using the keyword &lt;strong&gt;&lt;code&gt;interface&lt;/code&gt;&lt;/strong&gt;. In languages like C# and Java, there is a standard convention to prefix all interfaces with an &lt;code&gt;I&lt;/code&gt;, so a file handler interface will be &lt;code&gt;IFileHandler&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;IWallet&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;withdraw&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;deposit&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="nf"&gt;getBalance&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Wallet&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;IWallet&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;withdraw&lt;/span&gt;&lt;span class="o"&gt;(){&lt;/span&gt;
        &lt;span class="c1"&gt;//do something&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;deposit&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;//do something&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="nf"&gt;getBalance&lt;/span&gt;&lt;span class="o"&gt;(){&lt;/span&gt;
        &lt;span class="c1"&gt;//do something&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  When to use interfaces?
&lt;/h3&gt;

&lt;h4&gt;
  
  
  When you need &lt;strong&gt;a class to have expected behaviors&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;It means that by implementing an interface, the class &lt;strong&gt;agrees to implement all of the functionality specified by the interface&lt;/strong&gt;, ensuring that the expected behaviors are fulfilled.&lt;/p&gt;

&lt;h4&gt;
  
  
  When you need &lt;strong&gt;multiple inheritance&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;Different from abstract classes, interfaces enables &lt;strong&gt;multiple inheritance in classes&lt;/strong&gt;. It allow us to write &lt;strong&gt;assertive interfaces for a specific need&lt;/strong&gt;. For example, we have an interface for a specific class and it can be reused in another class that combines two or more interfaces to achieve it's behavior.&lt;/p&gt;

&lt;h4&gt;
  
  
  When you need to provide common functionality to unrelated classes
&lt;/h4&gt;

&lt;p&gt;Interfaces allow us to &lt;strong&gt;ensure functionality to classes in different contexts&lt;/strong&gt; but with similar behavior.&lt;/p&gt;

&lt;h3&gt;
  
  
  References
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://www.devfields.com/abstract-classes-and-interfaces/"&gt;Abstract classes and interfaces - Developer Fields&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.w3schools.com/java/java_oop.asp"&gt;Java OOP&lt;/a&gt;&lt;/p&gt;

</description>
      <category>java</category>
      <category>engineering</category>
      <category>oop</category>
    </item>
  </channel>
</rss>
