<?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: Yüksel Özdemir</title>
    <description>The latest articles on DEV Community by Yüksel Özdemir (@yukselcodingwithyou).</description>
    <link>https://dev.to/yukselcodingwithyou</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%2F512221%2F89d360c4-0f8a-4802-a5da-24dc0dd89599.jpg</url>
      <title>DEV Community: Yüksel Özdemir</title>
      <link>https://dev.to/yukselcodingwithyou</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/yukselcodingwithyou"/>
    <language>en</language>
    <item>
      <title>TDD using Spring BOOT</title>
      <dc:creator>Yüksel Özdemir</dc:creator>
      <pubDate>Sun, 27 Dec 2020 12:04:22 +0000</pubDate>
      <link>https://dev.to/yukselcodingwithyou/tdd-using-spring-boot-57n6</link>
      <guid>https://dev.to/yukselcodingwithyou/tdd-using-spring-boot-57n6</guid>
      <description>&lt;p&gt;This weekend, I decided to build a sample Spring-Boot application and learn development concepts, principles, and specifically to see how Test Driven Development can be achieved in Spring-Boot by doing some actual work.&lt;/p&gt;

&lt;p&gt;This blog post follows code in this repository &lt;/p&gt;
&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&gt;
      &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--vJ70wriM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://practicaldev-herokuapp-com.freetls.fastly.net/assets/github-logo-ba8488d21cd8ee1fee097b8410db9deaa41d0ca30b004c0c63de0a479114156f.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/yukselcodingwithyou"&gt;
        yukselcodingwithyou
      &lt;/a&gt; / &lt;a href="https://github.com/yukselcodingwithyou/spring-boot"&gt;
        spring-boot
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      Spring Boot projects
    &lt;/h3&gt;
  &lt;/div&gt;
  &lt;div class="ltag-github-body"&gt;
    
&lt;div id="readme" class="md"&gt;
&lt;h1&gt;
spring-boot&lt;/h1&gt;
&lt;p&gt;Spring Boot projects&lt;/p&gt;
&lt;p&gt;Hello: A Hello Application developed by implementing with TDD approach.&lt;/p&gt;
&lt;/div&gt;



&lt;/div&gt;
&lt;br&gt;
  &lt;div class="gh-btn-container"&gt;&lt;a class="gh-btn" href="https://github.com/yukselcodingwithyou/spring-boot"&gt;View on GitHub&lt;/a&gt;&lt;/div&gt;
&lt;br&gt;
&lt;/div&gt;
&lt;br&gt;


&lt;h2&gt;
  
  
  Why Testing?
&lt;/h2&gt;

&lt;p&gt;Testing in necessary in any field, because human beings are very likely to make mistakes anytime. Therefore, we need to check and verify that our work is completed correctly.&lt;br&gt;
So why testing in software is important explained below.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Point out defects and errors during development phases&lt;/li&gt;
&lt;li&gt;Reliability of organization increases in customers' perspective&lt;/li&gt;
&lt;li&gt;Ensuring quality of the product.&lt;/li&gt;
&lt;li&gt;Provides effective performance of product&lt;/li&gt;
&lt;li&gt;Foresee any failures that will cost much more in the next phases.&lt;/li&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
&lt;p&gt;I think, in this 5 bullet points the most important one is the fifth one. In my opinion, in a software project we must foresee any defect or failure, only then we can create a product that is time and cost effective. &lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2&gt;
  
  
  Testing Types
&lt;/h2&gt;

&lt;p&gt;There are many testing types used by people developing software, but I will emphasize &lt;strong&gt;unit testing&lt;/strong&gt; and &lt;strong&gt;integration testing&lt;/strong&gt; in this context.&lt;/p&gt;
&lt;h3&gt;
  
  
  Integration Testing
&lt;/h3&gt;

&lt;p&gt;This type of test is applied to see whether different components of system work well together. For more information you can see this &lt;a href="https://stackoverflow.com/questions/5357601/whats-the-difference-between-unit-tests-and-integration-tests"&gt;stackoverflow link&lt;/a&gt;. &lt;/p&gt;
&lt;h3&gt;
  
  
  Unit Testing
&lt;/h3&gt;

&lt;p&gt;This type of test is applied by programmer to see any unit of code is working properly. For more information you can see this &lt;a href="https://stackoverflow.com/questions/5357601/whats-the-difference-between-unit-tests-and-integration-tests"&gt;stackoverflow link&lt;/a&gt;. &lt;/p&gt;
&lt;h2&gt;
  
  
  Building The Application
&lt;/h2&gt;

&lt;p&gt;While building application, I used &lt;a href="https://start.spring.io/"&gt;spring-initializr&lt;/a&gt;, added some dependencies, you can also start a maven project and add dependencies to your &lt;code&gt;pom.xml&lt;/code&gt; &lt;/p&gt;
&lt;h4&gt;
  
  
  Added Dependencies
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;spring-boot-starter-web&lt;/code&gt;: to create web layer of Spring-Boot application.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;spring-boot-starter-test&lt;/code&gt; : to use features related testing comes in bundle with Spring-Boot.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;spring-boot-starter-data-jpa&lt;/code&gt;: to create entities and database layer in Spring-Boot application.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;junit-jupiter-engine&lt;/code&gt;: to use test functionalities of JUnit 5.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;lombok&lt;/code&gt; : to save some boilerplate code.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;com.h2database&lt;/code&gt; : in-memory database.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;First, I added an integration test to test happy path in the whole system. I will not dive into implementation details, but fundamentally, I used same annotations to make the integration test work. While we write integration tests, we should add these two annotations below to the top of the class, as you know, Spring-Boot uses annotation-driven approach :)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@SpringBootTest(webEnvironment=RANDOM_PORT)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In Spring-Boot, &lt;code&gt;@SpringBootTest&lt;/code&gt; annotation searches for the main application context, and creates a similar context to that. This annotation loads all of the application context, therefore it is suitable to use when writing integration tests. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;important&lt;/strong&gt;: Since the whole application context is loaded, frequent usage of &lt;code&gt;@SpringBootTest&lt;/code&gt; annotation causes long running test suites.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@ExtendWith(SpringExtension.class)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This &lt;code&gt;@ExtendWith(SpringExtension.class)&lt;/code&gt; annotation is used to enable Spring support of JUnit 5, in JUnit 4 &lt;code&gt;@RunWith(SpringRunner.class)&lt;/code&gt; annotation is used.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; @Autowired
 private TestRestTemplate testRestTemplate;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As Spring Documentation insists by saying, &lt;em&gt;Convenient alternative of RestTemplate that is suitable for integration tests.&lt;/em&gt; &lt;/p&gt;

&lt;p&gt;We use &lt;code&gt;TestRestTemplate&lt;/code&gt; in integration tests as part of the &lt;code&gt;spring-boot-starter-test&lt;/code&gt;. You can inject this to your test class as you see above. &lt;/p&gt;

&lt;p&gt;While you are writing tests, you simply should/can follow this convention below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    @Test
    @DisplayName("Tests happy path of hello POST method")
    public void testHello_Returns200() {
        // arrange
         ...
        // act
         ...
        // assert
         ...
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After I wrote integration tests, to test the whole process. Then I added tests for the web layer, so called controller. &lt;/p&gt;

&lt;p&gt;While testing the controller, as a difference from integration test, &lt;code&gt;@WebMvcTest&lt;/code&gt; annotation is added instead of &lt;code&gt;@SpringBootTest&lt;/code&gt; annotation. Since &lt;code&gt;@SpringBootTest&lt;/code&gt; loads all of the application context. We do not want the test take too long, since it is  a unit test. &lt;code&gt;@WebMvcTest&lt;/code&gt; loads only the beans required to test a web controller to the application context.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@ExtendWith(SpringExtension.class)
@WebMvcTest(HelloController.class)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can add these two annotations above to your controller test class. &lt;code&gt;@WebMvcTest&lt;/code&gt; annotation takes controller as parameter, if no parameter given as controller, includes all of the controllers in the context.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Autowired
private MockMvc mockMvc;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;While we write tests for controller layer, as the documentation implies as &lt;em&gt;"Main entry point for server-side Spring MVC test support."&lt;/em&gt; It is a good practice to use &lt;code&gt;MockMvc&lt;/code&gt; to simulate HTTP requests.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@MockBean
private HelloService service;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Any service, component or repository that our controller is dependent on by dependency injection is separately included, and mocked using &lt;code&gt;@MockBean&lt;/code&gt; annotation. This annotation adds the mocked object, in spring context it is a bean, to the application context. For a detailed information see this &lt;a href="https://stackoverflow.com/questions/44200720/difference-between-mock-mockbean-and-mockito-mock"&gt;stackoverflow link&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;After I passed all the test and refactored my code, I started to write unit test for the service layer. In this layer, I did not use any spring related context or annotation, because there was no need to load the application context. I only added the annotation below to my service test class.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@ExtendWith(MockitoExtension.class)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since there is no need to use Spring Application context in this layer, I preferred to use the &lt;code&gt;MockitoExtension.class&lt;/code&gt; to test lightweight.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Mock
private HelloRepository repository;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since I only used &lt;code&gt;MockitoExtension.class&lt;/code&gt; I also used &lt;code&gt;@Mock&lt;/code&gt; to inject mocked dependent services, components, or repositories as you can see above. In case you want to use &lt;code&gt;@MockBean&lt;/code&gt; instead of &lt;code&gt;@Mock&lt;/code&gt;, you need to use &lt;code&gt;@ExtendWith(SpringExtension.class)&lt;/code&gt;&lt;br&gt;
For a detailed information, when to use &lt;code&gt;SpringExtension&lt;/code&gt; and &lt;code&gt;MockitoExtension&lt;/code&gt; see this &lt;a href="https://stackoverflow.com/questions/61433806/junit-5-with-spring-boot-when-to-use-extendwith-spring-or-mockito"&gt;stackoverflow link&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;After testing service layer, I continued to test data layer in the project, so called repository in Spring-Boot. While testing data layer in Spring-Boot, use these annotations below at the top of your test class.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@ExtendWith(SpringExtension.class)
@DataJpaTest
@AutoConfigureTestDatabase(replace = NONE, connection = EmbeddedDatabaseConnection.H2)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since Spring related application context should be loaded, I  used &lt;code&gt;@ExtendWith(SpringExtension.class)&lt;/code&gt; to tell JUnit 5 to use spring extension. Also &lt;code&gt;@DataJpaTest&lt;/code&gt; will load not whole spring application context but only JPA related context. Apart from these, I used  &lt;code&gt;@AutoConfigureTestDatabase&lt;/code&gt; annotation to configure H2 in-memory database automatically for the repository test.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Autowired
private TestEntityManager entityManager;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In addition to these I also used &lt;code&gt;TestEntityManager&lt;/code&gt; in repository test to test the actual persistence to the database, if we use only the repository's save method, we do not actually fully test the behavior, jpa save method only saves to cache, therefore it is healthy to use  &lt;code&gt;TestEntityManager&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  TDD (Test-Driven-Development)
&lt;/h2&gt;

&lt;p&gt;All along, I tried to apply TDD approach while writing code. That approach is: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Add a test&lt;/li&gt;
&lt;li&gt;Run all tests and see if the new test fails&lt;/li&gt;
&lt;li&gt;Write the code&lt;/li&gt;
&lt;li&gt;Run tests&lt;/li&gt;
&lt;li&gt;Refactor code&lt;/li&gt;
&lt;li&gt;Repeat&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;To read more about TDD, see this &lt;a href="https://en.wikipedia.org/wiki/Test-driven_development"&gt;wikipedia link&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;While I was learning how to code testing in Spring Boot I harnessed some tutorials below.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://reflectoring.io/unit-testing-spring-boot/"&gt;link 1&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.youtube.com/watch?v=s9vt6UJiHg4&amp;amp;t=1436s&amp;amp;ab_channel=SpringDeveloper"&gt;link 2&lt;/a&gt; &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can see these useful links and more.. &lt;/p&gt;

&lt;p&gt;Happy Coding :-)&lt;/p&gt;

</description>
      <category>springboot</category>
    </item>
  </channel>
</rss>
