<?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: Majid Abarghooei</title>
    <description>The latest articles on DEV Community by Majid Abarghooei (@dante0747).</description>
    <link>https://dev.to/dante0747</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%2F497343%2Fdc83a196-0f74-47ce-969d-319ac484a504.jpeg</url>
      <title>DEV Community: Majid Abarghooei</title>
      <link>https://dev.to/dante0747</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/dante0747"/>
    <language>en</language>
    <item>
      <title>A seemingly simple question: what are design patterns?</title>
      <dc:creator>Majid Abarghooei</dc:creator>
      <pubDate>Sat, 18 Feb 2023 17:28:31 +0000</pubDate>
      <link>https://dev.to/dante0747/a-seemingly-simple-question-what-are-design-patterns-dbm</link>
      <guid>https://dev.to/dante0747/a-seemingly-simple-question-what-are-design-patterns-dbm</guid>
      <description>&lt;p&gt;In the software industry, there are a range of topics you hear debates about in almost every company. I believe the &lt;br&gt;
design patterns story is one of them. You can find countless posts, articles, and Quora/Stackoverflow answers acting &lt;br&gt;
for or against them. For instance, the other day, I came across &lt;a href="https://www.quora.com/Why-do-programmers-talk-less-about-design-patterns-now-Which-patterns-if-any-are-still-valuable" rel="noopener noreferrer"&gt;this old Quora question&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Why do programmers talk less about design patterns now? Which patterns (if any) are still valuable?"&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The author meant object-oriented design patterns, or the 23 patterns introduced in the &lt;a href="https://www.google.com/books/edition/Design_Patterns/tmNNfSkfTlcC?hl=en&amp;amp;gbpv=1&amp;amp;dq=Design+Patterns:+Elements+of+Reusable+Object-Oriented+Software&amp;amp;printsec=frontcover#v=onepage&amp;amp;q=Design%20Patterns%3A%20Elements%20of%20Reusable%20Object-Oriented%20Software&amp;amp;f=false" rel="noopener noreferrer"&gt;Gang of Four book&lt;/a&gt;, &lt;br&gt;
and most of the people who answered the question assumed that this was the case. However, 'the design pattern' is a term &lt;br&gt;
that some people use instead of 'object-oriented design patterns'-so don't get shocked when you come across the strange &lt;br&gt;
statement: "design patterns are dead".&lt;/p&gt;

&lt;p&gt;In this article, we're going to answer a seemingly simple question: what is a design pattern? Answering this could help &lt;br&gt;
us determine what we're talking about while we're discussing or applying a particular design pattern.&lt;/p&gt;
&lt;h2&gt;
  
  
  What do design patterns mean to you?
&lt;/h2&gt;

&lt;p&gt;Let's put all the clichés aside and focus on what comes to your mind when thinking about the design pattern concept. &lt;br&gt;
Is it a rule that shouldn't be broken? Is it a wordy academic recommendation? Is it another design constraint? Or is it &lt;br&gt;
something you only need to know to pass job interviews?&lt;/p&gt;

&lt;p&gt;To me, it's like a whispering sound in my head preventing me from reinventing the wheel. You may have experienced the &lt;br&gt;
urge to start the design/build process immediately after facing a problem and created your own solution-if so, &lt;br&gt;
welcome to the club. But in this scenario, if a solution already exists, creating another one just takes up unnecessary &lt;br&gt;
time and effort. Although the urge to jump straight into creating your own solution might settle over time, I believe &lt;br&gt;
that one of the crucial tools to help us overcome this urge is the design pattern concept.&lt;/p&gt;

&lt;p&gt;A design pattern reminds me that many software engineers have already faced the same problem before and solved it in a &lt;br&gt;
particular way for several reasons. There's probably a rationale behind it and it gives us the ability to see the &lt;br&gt;
problem from a different perspective, even if we don't intend to follow that pattern.&lt;/p&gt;

&lt;p&gt;Let's look at a paragraph from the &lt;a href="https://www.google.com/books/edition/Design_Patterns/tmNNfSkfTlcC?hl=en&amp;amp;gbpv=1&amp;amp;dq=Design+Patterns:+Elements+of+Reusable+Object-Oriented+Software&amp;amp;printsec=frontcover#v=onepage&amp;amp;q=Design%20Patterns%3A%20Elements%20of%20Reusable%20Object-Oriented%20Software&amp;amp;f=false" rel="noopener noreferrer"&gt;GoF famous book&lt;/a&gt;'s introduction:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"We all know the value of design experience. How many times have you had design déjà vu-that feeling that you've &lt;br&gt;
solved a problem before but not knowing exactly where or how? If you could remember the details of the previous &lt;br&gt;
problem and how you solved it, then you could reuse the experience instead of rediscovering it." &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;More or less, we have experienced something similar: solving a common design problem several times when we couldn't &lt;br&gt;
remember the previous solution. Through reusing solutions to solve recurring problems, design patterns save time and &lt;br&gt;
effort, both of which can be directed elsewhere.&lt;/p&gt;

&lt;p&gt;Now let's dive into some fundamental concepts and definitions.&lt;/p&gt;
&lt;h2&gt;
  
  
  What do patterns mean?
&lt;/h2&gt;

&lt;p&gt;What is a pattern? The pattern concept is not something specific to our industry. Other disciplines and industries such &lt;br&gt;
as architecture, economics, etc. also apply this concept. Let's see the pattern definitions by &lt;a href="https://en.wikipedia.org/wiki/Christopher_Alexander" rel="noopener noreferrer"&gt;Christopher Alexander&lt;/a&gt;, &lt;br&gt;
a leading authority in architecture:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Each pattern is a three-part rule, which expresses a relation between a certain context, a problem, and a solution." (&lt;a href="https://www.google.com/books/edition/The_Timeless_Way_of_Building/H6CE9hlbO8sC?hl=en&amp;amp;gbpv=1&amp;amp;dq=The+Timeless+Way+of+Building&amp;amp;printsec=frontcover#v=onepage" rel="noopener noreferrer"&gt;Alexander, 1979&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;"Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice." (&lt;a href="https://www.google.com/books/edition/A_Pattern_Language/FTpxDwAAQBAJ?hl=en&amp;amp;gbpv=1&amp;amp;dq=Pattern+language&amp;amp;printsec=frontcover#v=onepage&amp;amp;q&amp;amp;f=false" rel="noopener noreferrer"&gt;Alexander, 1977&lt;/a&gt;)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now let's see what patterns mean in the software industry:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"A pattern for software architecture describes a particular recurring design problem that arises in specific design &lt;br&gt;
contexts, and presents a well-proven generic scheme for its solution. The solution scheme is specified by describing &lt;br&gt;
its constituent components, their responsibilities and relationships, and the ways in which they collaborate." &lt;br&gt;
(&lt;a href="https://books.google.nl/books/about/Pattern_Oriented_Software_Architecture_A.html?id=j_ahu_BS3hAC&amp;amp;printsec=frontcover&amp;amp;source=kp_read_button&amp;amp;hl=en&amp;amp;redir_esc=y#v=onepage&amp;amp;q&amp;amp;f=false" rel="noopener noreferrer"&gt;BMRSS, 96&lt;/a&gt;)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Based on the above definitions, we can consider a pattern as a triad of context, problem, and solution combined with &lt;br&gt;
the relationship among them. However, it's a broad concept and can be applied to different scales and abstraction &lt;br&gt;
levels, from designing a whole software system, to solving a design problem belonging to a particular programming language.&lt;/p&gt;
&lt;h2&gt;
  
  
  The different pattern categories
&lt;/h2&gt;

&lt;p&gt;Patterns are classified based on their scale and abstraction level:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Architectural patterns&lt;/li&gt;
&lt;li&gt;Design patterns&lt;/li&gt;
&lt;li&gt;Idioms&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;
![ ](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2af9n68tns2b5gvzmxix.jpeg)
&lt;/p&gt;

&lt;p&gt;Architectural patterns focus on a whole software system structure, its subsystems, the different broad components and &lt;br&gt;
how they fit together. These patterns are at a high level of abstraction. For instance, they don't care what the &lt;br&gt;
programming language is or what framework is being used.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"An architectural pattern expresses a fundamental structural organisation schema for software systems. It provides a &lt;br&gt;
set of predefined subsystems, specifies their responsibilities, and includes rules and guidelines for organising the &lt;br&gt;
relationships between them." (&lt;a href="https://books.google.nl/books/about/Pattern_Oriented_Software_Architecture_A.html?id=j_ahu_BS3hAC&amp;amp;printsec=frontcover&amp;amp;source=kp_read_button&amp;amp;hl=en&amp;amp;redir_esc=y#v=onepage&amp;amp;q&amp;amp;f=false" rel="noopener noreferrer"&gt;BMRSS, 96&lt;/a&gt;)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;On the other hand, we have some patterns in the lowest level of abstraction called idioms which address both design and &lt;br&gt;
implementation. Idioms are language-specific meaning that an idiom in language A is irrelevant in language B.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"An idiom is a low-level pattern specific to a programming language. An idiom describes how to implement particular &lt;br&gt;
aspects of components or the relationships between them using the features of the given language." (&lt;a href="https://books.google.nl/books/about/Pattern_Oriented_Software_Architecture_A.html?id=j_ahu_BS3hAC&amp;amp;printsec=frontcover&amp;amp;source=kp_read_button&amp;amp;hl=en&amp;amp;redir_esc=y#v=onepage&amp;amp;q&amp;amp;f=false" rel="noopener noreferrer"&gt;BMRSS, 96&lt;/a&gt;)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;However, there are still some other patterns that occur between those mentioned above regarding the scale and &lt;br&gt;
abstraction level, that's where design patterns come in.&lt;/p&gt;
&lt;h2&gt;
  
  
  A closer look at design patterns
&lt;/h2&gt;

&lt;p&gt;So let's review the definition of the remaining category:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"A design pattern provides a scheme for refining the subsystems or components of a software system, or the &lt;br&gt;
relationships between them. It describes a commonly-recurring structure of communicating components that solves a &lt;br&gt;
general design problem within a particular context." (&lt;a href="https://books.google.nl/books/about/Pattern_Oriented_Software_Architecture_A.html?id=j_ahu_BS3hAC&amp;amp;printsec=frontcover&amp;amp;source=kp_read_button&amp;amp;hl=en&amp;amp;redir_esc=y#v=onepage&amp;amp;q&amp;amp;f=false" rel="noopener noreferrer"&gt;BMRSS, 96&lt;/a&gt;)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Design patterns are medium-scale, meaning they're neither highly abstract like architectural patterns, nor coupled to &lt;br&gt;
programming languages, like idioms. Applying design patterns doesn't affect the software system structure, but it does &lt;br&gt;
impact the subsystems' architecture and its smaller components.&lt;/p&gt;

&lt;p&gt;Now, let's discuss the following statement (extracted from the same &lt;a href="https://books.google.nl/books/about/Pattern_Oriented_Software_Architecture_A.html?id=j_ahu_BS3hAC&amp;amp;printsec=frontcover&amp;amp;source=kp_read_button&amp;amp;hl=en&amp;amp;redir_esc=y#v=onepage&amp;amp;q&amp;amp;f=false" rel="noopener noreferrer"&gt;reference&lt;/a&gt;) &lt;br&gt;
containing a crucial fact about design patterns:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"They tend to be independent of a particular programming language or programming paradigm." (&lt;a href="https://books.google.nl/books/about/Pattern_Oriented_Software_Architecture_A.html?id=j_ahu_BS3hAC&amp;amp;printsec=frontcover&amp;amp;source=kp_read_button&amp;amp;hl=en&amp;amp;redir_esc=y#v=onepage&amp;amp;q&amp;amp;f=false" rel="noopener noreferrer"&gt;BMRSS, 96&lt;/a&gt;)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;According to the pattern category we previously addressed in this article, we'd better rephrase it like this:&lt;/p&gt;

&lt;p&gt;"They are independent of a particular programming language but &lt;strong&gt;often&lt;/strong&gt; (not always) dependent on a programming paradigm."&lt;/p&gt;

&lt;p&gt;The first part of the statement is clear: if a pattern is dependent on a specific programming language, it would be in &lt;br&gt;
the idioms category. Regarding the second part, design patterns are attempts to solve common problems that occur in &lt;br&gt;
programming paradigms. They're likely responses to the deficiencies of paradigms. For instance, there are 23 &lt;br&gt;
object-oriented design patterns described in the GoF book, most of which are not genuine concerns in the functional &lt;br&gt;
programming paradigm. But the opposite is also true as the design patterns have different contexts.&lt;/p&gt;

&lt;p&gt;As you can see in the &lt;a href="https://blog.ploeh.dk/2012/05/25/Designpatternsacrossparadigms/" rel="noopener noreferrer"&gt;following image&lt;/a&gt;, &lt;br&gt;
different paradigms could share design patterns, but we're not going to address this topic as it's beyond the scope of &lt;br&gt;
this article.&lt;/p&gt;

&lt;p&gt;
![ ](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u299fc7tzxysuvjmw7u1.jpeg)
&lt;/p&gt;

&lt;h2&gt;
  
  
  Analysing a famous statement
&lt;/h2&gt;

&lt;p&gt;With the previous sections in mind, let's have a quick look at a statement you may encounter when you simply google &lt;br&gt;
design patterns:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Functional languages don't need design patterns.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You can find a sample here. This statement is wrong as it implies design patterns only belong to the object-oriented &lt;br&gt;
paradigm. However, design patterns are a concept that can be present in both functional programming paradigm and &lt;br&gt;
object-oriented paradigm. The design patterns may just be specific to the problems and demands of the host paradigm. &lt;br&gt;
For instance, the functional programming paradigm has design patterns specific to its context.&lt;/p&gt;

&lt;p&gt;Moreover, as we discussed in the previous sections, different paradigms have different contexts and consequently &lt;br&gt;
encounter different problems. We should not expect the design patterns of paradigm B to solve the problems of paradigm A.&lt;/p&gt;

&lt;h2&gt;
  
  
  A quick recap
&lt;/h2&gt;

&lt;p&gt;Design patterns, similar to other pattern types, try to solve recurring problems of a particular context.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;They're language-independent but mostly paradigm coupled.&lt;/li&gt;
&lt;li&gt;Being medium-scale, they are not as abstract as architectural patterns.&lt;/li&gt;
&lt;li&gt;Their application doesn't affect the fundamental architecture of the software system but influences the subsystems' architecture.&lt;/li&gt;
&lt;li&gt;Last but not least, they're (all types of patterns) means of preventing us from reinventing the wheel.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;From my perspective, design patterns are not strict laws to constrain our design. They're neither fixed nor flawless as &lt;br&gt;
they could evolve and get better or even fade away, but the pattern concept still exists. To follow patterns is like a &lt;br&gt;
mindset that helps us figure out that there could be a better solution.&lt;/p&gt;

</description>
      <category>watercooler</category>
    </item>
    <item>
      <title>How to Make Your Reviewer Cry Using Java Optional</title>
      <dc:creator>Majid Abarghooei</dc:creator>
      <pubDate>Fri, 25 Dec 2020 15:10:01 +0000</pubDate>
      <link>https://dev.to/dante0747/how-to-make-your-reviewer-cry-using-java-optional-5go3</link>
      <guid>https://dev.to/dante0747/how-to-make-your-reviewer-cry-using-java-optional-5go3</guid>
      <description>&lt;p&gt;I think code review is one of the best sources of inspiration. I see it as an opportunity to learn new things from other software developers sending pull/merge requests. Moreover, sometimes you may need to study a specific subject with more details regarding the under review code. Usually, this process leads to a more profound knowledge of that domain. However, there's still another fact about code review: after a while, you face some common mistakes. Recently, I reviewed a feature in which I saw an &lt;strong&gt;Optional&lt;/strong&gt; pitfall. Honestly, I had seen that issue several times in different merge requests sent by several developers (from beginners to experienced ones).&lt;/p&gt;

&lt;p&gt;I know that there are tonnes of articles regarding Java &lt;strong&gt;Optional&lt;/strong&gt; some of which are quite useful like &lt;a href="https://dzone.com/articles/using-optional-correctly-is-not-optional" rel="noopener noreferrer"&gt;this one&lt;/a&gt; (although I have arguments against some of the cases mentioned in the article) by &lt;a href="https://dzone.com/users/196910/anghelleonard.html" rel="noopener noreferrer"&gt;Anghel Leonard&lt;/a&gt;, the author of &lt;a href="https://www.amazon.com/gp/product/B07Y9BPV4W/" rel="noopener noreferrer"&gt;Java Coding Problems&lt;/a&gt;. I don't want to repeat all of the contents you can simply find by googling. I intend to talk about some of the most common and less addressed pitfalls regarding &lt;strong&gt;Optional&lt;/strong&gt; usage.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Feuo06h282ib61st5mhjf.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Feuo06h282ib61st5mhjf.jpg" alt="Code Review Image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Optional?
&lt;/h2&gt;

&lt;p&gt;Before diving into the main subject, let’s review some definitions and see what &lt;strong&gt;Optional&lt;/strong&gt; is. As a Java developer, you should have encountered the infamous &lt;strong&gt;NullPointerException&lt;/strong&gt; that occurs when you're going to access a &lt;strong&gt;null&lt;/strong&gt; reference. With a quick search, you can find thousands of memes and jokes about null references in Java (and other languages as well). &lt;strong&gt;Optional&lt;/strong&gt; came in to play in Java 8 to help programmers get rid of all of the problems caused by &lt;strong&gt;null&lt;/strong&gt; references. Have a look at &lt;a href="https://www.oracle.com/technical-resources/articles/java/java8-optional.html" rel="noopener noreferrer"&gt;this article&lt;/a&gt;, from &lt;a href="https://www.oracle.com/technical-resources/" rel="noopener noreferrer"&gt;Oracle technical resources&lt;/a&gt;, to read more about the motivation of &lt;strong&gt;Optional&lt;/strong&gt; existence in Java.&lt;/p&gt;

&lt;p&gt;Now we are going to have a look at Optional’s &lt;a href="https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Optional.html" rel="noopener noreferrer"&gt;JavaDoc&lt;/a&gt; and check the provided definition.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A container object which may or may not contain a non-null value.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Following this introduction, as an API note, there is an answer to the first question that may come to mind: “when should we use &lt;strong&gt;Optional&lt;/strong&gt;?”&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Optional is primarily intended for use as a method return type where there is a clear need to represent "no result," and where using null is likely to cause errors. A variable whose type is Optional should never itself be null; it should always point to an Optional instance.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So we can extract the following points from the &lt;a href="https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Optional.html" rel="noopener noreferrer"&gt;JavaDoc&lt;/a&gt; descriptions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Optional is a container object.&lt;/li&gt;
&lt;li&gt;It may not contain a &lt;strong&gt;non-null&lt;/strong&gt; value.&lt;/li&gt;
&lt;li&gt;It is mainly used as a return type when using &lt;strong&gt;null&lt;/strong&gt; is troublesome.&lt;/li&gt;
&lt;li&gt;The variable with the Optional type should not be &lt;strong&gt;null&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Keeping in mind these subtle points, you can simply spot most of the &lt;strong&gt;Optional&lt;/strong&gt; related code smells. Now, it’s time to go through the four most common &lt;strong&gt;Optional&lt;/strong&gt; pitfalls.&lt;/p&gt;

&lt;h2&gt;
  
  
  Old Fashioned Exception Handling
&lt;/h2&gt;

&lt;p&gt;One of the most common mistakes I usually face in code reviews is using an if/else block to handle the circumstances that the &lt;strong&gt;Optional&lt;/strong&gt; variable doesn't contain a non-null value (as previously mentioned in the &lt;strong&gt;Optional&lt;/strong&gt; &lt;br&gt;
definition). Check this out:&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="nc"&gt;Optional&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;userOptional&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;userDao&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;findByCellNumber&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cellNumber&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(!&lt;/span&gt;&lt;span class="n"&gt;userOptional&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;isPresent&lt;/span&gt;&lt;span class="o"&gt;())&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;UserNotFoundException&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"..."&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;User&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;userOptional&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;get&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="c1"&gt;//...&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Suppose that you intend to find a user by a specific cell number from the database. Then you receive an &lt;strong&gt;Optional&lt;/strong&gt; variable from the corresponding DAO class. Now, you want to check if there's a user with the cell number you have provided. What's wrong with the above code? Doesn't work? Honestly, it works like a charm. However, it's not elegant; it's something like twisting wood screws with a wrench. Now, check the following code snippet:&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="nc"&gt;User&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;userDao&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;findByCellNumber&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cellNumber&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="na"&gt;orElseThrow&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nl"&gt;UserNotFoundException:&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Using the &lt;strong&gt;orElseThrow&lt;/strong&gt;, you can do the same thing more oncisely and elegantly without any need to &lt;strong&gt;get&lt;/strong&gt; method. &lt;br&gt;
You can also use its &lt;a href="https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Optional.html#orElseThrow()" rel="noopener noreferrer"&gt;no-argument version&lt;/a&gt; of &lt;strong&gt;orElseThrow&lt;/strong&gt; introduced in Java 10.&lt;/p&gt;
&lt;h2&gt;
  
  
  orElse and orElseGet Misuse
&lt;/h2&gt;

&lt;p&gt;The second common pitfall may emerge while using &lt;strong&gt;orElse&lt;/strong&gt; or &lt;strong&gt;orElseGet&lt;/strong&gt;. It seems these methods do the same thing; however, there is a significant difference not that fancy like the previous one. Let’s have a look at their signatures and corresponding Javadoc:&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="no"&gt;T&lt;/span&gt; &lt;span class="nf"&gt;orElse&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="no"&gt;T&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;//If a value is present, returns the value, otherwise returns other.&lt;/span&gt;
&lt;span class="c1"&gt;//Parameters:&lt;/span&gt;
&lt;span class="c1"&gt;//other - the value to be returned, if no value is present. May be null.&lt;/span&gt;
&lt;span class="c1"&gt;//Returns:&lt;/span&gt;
&lt;span class="c1"&gt;//the value, if present, otherwise other&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&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="no"&gt;T&lt;/span&gt; &lt;span class="nf"&gt;orElseGet&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Supplier&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;?&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="no"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;supplier&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;//If a value is present, returns the value, otherwise returns the result produced by the supplying function.&lt;/span&gt;
&lt;span class="c1"&gt;//Parameters:&lt;/span&gt;
&lt;span class="c1"&gt;//supplier - the supplying function that produces a value to be returned&lt;/span&gt;
&lt;span class="c1"&gt;//Returns:&lt;/span&gt;
&lt;span class="c1"&gt;//the value, if present, otherwise the result produced by the supplying function&lt;/span&gt;
&lt;span class="c1"&gt;//Throws:&lt;/span&gt;
&lt;span class="c1"&gt;//NullPointerException - if no value is present and the supplying function is null&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Regarding the above signatures, the first difference is crystal clear: &lt;strong&gt;orElse&lt;/strong&gt; accepts a generic object as an argument; however, &lt;strong&gt;orElseGet&lt;/strong&gt; accepts a supplier function that produces a value. So what? You are right; it may seem negligible. However, it's not that easy. Let’s check the following sample:&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="nc"&gt;Optional&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Optional&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;of&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&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;"The number is: "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;orElseGet&lt;/span&gt;&lt;span class="o"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;alternativeNumber&lt;/span&gt;&lt;span class="o"&gt;()));&lt;/span&gt;

&lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="nc"&gt;Integer&lt;/span&gt; &lt;span class="nf"&gt;alternativeNumber&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;747&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;"The alternative number is: "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;number&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;p&gt;What's the output of the above code snippet? As you see, the number is an &lt;strong&gt;Optional&lt;/strong&gt; variable initiated with the value of 10. So we don't expect &lt;strong&gt;orElseGet&lt;/strong&gt; to do something. Here's the output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;The number is: 10
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's super easy, right? Now check this one:&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="nc"&gt;Optional&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Optional&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;of&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&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;"The number is: "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;orElse&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;alternativeNumber&lt;/span&gt;&lt;span class="o"&gt;()));&lt;/span&gt;

&lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="nc"&gt;Integer&lt;/span&gt; &lt;span class="nf"&gt;alternativeNumber&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;747&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;"The alternative number is: "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;number&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;p&gt;Everything is the same as the previous example except the second line in which we have used &lt;strong&gt;orElse&lt;/strong&gt; instead of &lt;strong&gt;orElseGet&lt;/strong&gt;. We don't expect the &lt;strong&gt;orElse&lt;/strong&gt; to do something yet because our &lt;strong&gt;number&lt;/strong&gt; has a value. Just stop here and guess the output! Same as the previous one? No! The output is:&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="nc"&gt;The&lt;/span&gt; &lt;span class="n"&gt;alternative&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt; &lt;span class="nl"&gt;is:&lt;/span&gt; &lt;span class="mi"&gt;747&lt;/span&gt;
&lt;span class="nc"&gt;The&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt; &lt;span class="nl"&gt;is:&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;strong&gt;orElse&lt;/strong&gt;’s parameter always gets evaluated; even if the &lt;strong&gt;Optional&lt;/strong&gt; variable contains a value. However, the supplier method passed to the &lt;strong&gt;orElseGet&lt;/strong&gt; only gets evaluated when the &lt;strong&gt;Optional&lt;/strong&gt; variable is empty. That’s why the second output is different. Note that this could affect the performance drastically in cases you use &lt;strong&gt;orElse&lt;/strong&gt; and pass it a complex method.&lt;/p&gt;

&lt;h2&gt;
  
  
  Using orElse(null)
&lt;/h2&gt;

&lt;p&gt;The other common mistake I usually encounter in code reviews is somehow philosophical; that's using &lt;strong&gt;orElse(null)&lt;/strong&gt;. What’s wrong with it? To find the answer, let's review the motivation of Optional existence. As I previously mentioned, &lt;strong&gt;Optional&lt;/strong&gt; exists to help you not to face the problems caused by &lt;strong&gt;null&lt;/strong&gt; references such as &lt;strong&gt;NullPointerException&lt;/strong&gt;. We also talked about the &lt;strong&gt;orElse&lt;/strong&gt; method in the previous section. Now, put these two parts together. Does it make any sense to ask our &lt;strong&gt;Optional&lt;/strong&gt; object to return &lt;strong&gt;null&lt;/strong&gt; if it’s empty and doesn’t contain a value? &lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;orElse&lt;/strong&gt; method is a proper way to return an alternative value in the cases the &lt;strong&gt;Optional&lt;/strong&gt; object is empty. Returning &lt;strong&gt;null&lt;/strong&gt;, questions the usage of &lt;strong&gt;Optional&lt;/strong&gt;. You may prefer to use a &lt;strong&gt;null&lt;/strong&gt; reference instead of &lt;strong&gt;Optional&lt;/strong&gt;; it's ok, although I never recommend it. However, mixing these two ideas is somehow misleading. At the first glance, the code seems &lt;strong&gt;null&lt;/strong&gt; safe; however, you may face &lt;strong&gt;null&lt;/strong&gt; reference issues, sooner or later.&lt;/p&gt;

&lt;h2&gt;
  
  
  Returning null in a method with Optional return type
&lt;/h2&gt;

&lt;p&gt;Before describing the last and weirdest case, let’s see how it happens. Let’s get back to the previous example. Suppose that the &lt;strong&gt;findByCellNumber&lt;/strong&gt; is something like this:&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="nc"&gt;Optional&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;findByCellNumber&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;cellNumber&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;//some code here&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;someCondition&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="c1"&gt;//some code here&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you see, this method returns &lt;strong&gt;null&lt;/strong&gt; in a specific condition. Suppose that the database is down and the author handles the exception and returns &lt;strong&gt;null&lt;/strong&gt; in this situation (&lt;strong&gt;BAD PRACTICE&lt;/strong&gt;, don’t try this at home). So, there are some circumstances where you try to get the &lt;strong&gt;Optional&lt;/strong&gt; value but you encounter a &lt;strong&gt;NullPointerException&lt;/strong&gt;. Look at this:&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="nc"&gt;User&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;userDao&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;findByCellNumber&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cellNumber&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="na"&gt;orElseThrow&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nl"&gt;UserNotFoundException:&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We expect the method to return an &lt;strong&gt;Optional&lt;/strong&gt; then we use &lt;strong&gt;orElseThrow&lt;/strong&gt;; however, it returns &lt;strong&gt;null&lt;/strong&gt;. So, our try leads to an exception. How to avoid this issue? This is the point: as previously mentioned, an &lt;strong&gt;Optional&lt;/strong&gt; MUST not be &lt;strong&gt;null&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Quick Recap
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Each explicit call to &lt;strong&gt;get&lt;/strong&gt; method of an &lt;strong&gt;Optional&lt;/strong&gt; variable could be a code smell.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;orElse&lt;/strong&gt; and &lt;strong&gt;orElseGet&lt;/strong&gt; are not equivalent.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Optional&lt;/strong&gt; was added to Java to help developers avoid &lt;strong&gt;null&lt;/strong&gt; references. So each &lt;strong&gt;return null&lt;/strong&gt; or &lt;strong&gt;orElse(null)&lt;/strong&gt; presence in your code using &lt;strong&gt;Optional&lt;/strong&gt; variables is a code smell.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Keeping in mind these points, you can prevent most of the common pitfalls while using &lt;strong&gt;Optional&lt;/strong&gt;.&lt;/p&gt;

</description>
      <category>java</category>
      <category>programming</category>
    </item>
    <item>
      <title>Sluggish Spring Boot Tests Riddle</title>
      <dc:creator>Majid Abarghooei</dc:creator>
      <pubDate>Fri, 23 Oct 2020 20:02:45 +0000</pubDate>
      <link>https://dev.to/dante0747/sluggish-spring-boot-tests-riddle-4ej6</link>
      <guid>https://dev.to/dante0747/sluggish-spring-boot-tests-riddle-4ej6</guid>
      <description>&lt;p&gt;Most of us have faced beliefs that go unquestioned. Such ideas can vary from small daily matters such as grocery shopping to something as serious as religious matters. In my case, it was a professional belief, specifically whether or not integration tests are too slow.&lt;/p&gt;

&lt;p&gt;There was a belief in our squad (and our chapter as well) that integration tests were slow and this belief made us not put this issue in our technical debts backlog and we all accepted it as a fact. It continued until the build time in some microservices exceeded 10 minutes and teammates started complaining about them. The first reaction was avoiding integration tests as much as possible and using unit tests instead. I know that it is not a wise decision against which there are many arguments, but It was the decision that the team made. In this post, we will see how this issue got resolved and the build time in our microservices decreased to half.&lt;/p&gt;

&lt;p&gt;Finally, over a weekend, I decided to follow this issue and find an answer to the question: &lt;strong&gt;Why are our integration tests that slow?&lt;/strong&gt; I started with writing down all the things I was skeptical about but my mind was just obsessed with &lt;strong&gt;@SpringBootTest&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Following the first suspicion
&lt;/h2&gt;

&lt;p&gt;Whenever our tests were running, I saw the Spring logo showing up several times. I thought that &lt;strong&gt;@SpringBootTest&lt;/strong&gt; loaded the full application context per each test class. After a ten-minute search, I realized that all my assumptions were wrong. I found the point in the &lt;a href="https://docs.spring.io/autorepo/docs/spring-framework/4.2.0.RC2/spring-framework-reference/html/integration-testing.html#testcontext-ctx-management-caching"&gt;Spring docs&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Once the TestContext framework loads an ApplicationContext (or WebApplicationContext) for a test, that context will be cached and reused for all subsequent tests that declare the same unique context configuration within the same test suite.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So why did it still load the context per each test class? Actually, it did not. I got this by counting the context loads by a specific piece of text in the log (which is repeating every time the Spring loads the application context). Something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mvn clean install &amp;gt; build-log.txt

grep "The following profiles are active: test" log.txt| wc -l
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The result was 16 while we had 46 integration test classes in the codebase. It means that it did not load the context per each test class but why only 16 times?&lt;/p&gt;

&lt;h2&gt;
  
  
  Digging into the test classes
&lt;/h2&gt;

&lt;p&gt;After getting this weird result from counting the context loads, I checked all of the test classes one by one to find a clue. I realized that all of the integration test classes were annotated by &lt;strong&gt;@TestPropertySource&lt;/strong&gt; to load one or more specific property files. I also had another strange finding in this investigation: &lt;strong&gt;MockBean&lt;/strong&gt; and &lt;strong&gt;SpyBean&lt;/strong&gt; annotations in integration tests. Not only I had philosophical issues with using these annotations in integration tests but was it screaming that: "&lt;strong&gt;Hey dude, there’s a code smell here&lt;/strong&gt;".&lt;/p&gt;

&lt;p&gt;I started the second round of searching and the same as the previous one, I managed to find tonnes of useful articles and blog posts related to the context and some clues after about only ten minutes. I found &lt;a href="https://www.baeldung.com/spring-tests"&gt;this helpful blog post&lt;/a&gt; by José Carlos Valero Sánchez about optimizing Spring boot integration tests and &lt;a href="https://rieckpil.de/improve-build-times-with-context-caching-from-spring-test/"&gt;this one&lt;/a&gt; that its author has had the same journey as mine.&lt;/p&gt;

&lt;p&gt;By reading these articles and the same ones, I realized that there are some pitfalls in integration tests that prevent the Spring from reusing the loaded application context in integration tests. Here are the most important ones:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Using &lt;strong&gt;@MockBean&lt;/strong&gt; and &lt;strong&gt;@SpyBean&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Using &lt;strong&gt;@DirtiesContext&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Careless use of profiles in integration tests&lt;/li&gt;
&lt;li&gt;Careless use of &lt;strong&gt;@TestPropertySource&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I will review all of these items in a separate blog post in detail. Now, let’s see how I boosted up our integration tests by two minor actions.&lt;/p&gt;

&lt;h2&gt;
  
  
  Optimizing the integration tests
&lt;/h2&gt;

&lt;p&gt;As I previously mentioned, there were two pitfalls in our integration test classes. The first one was annotating all of the integration tests with &lt;strong&gt;@TestPropertySource&lt;/strong&gt;. I took a quick look at them and found out there were different combinations of property files used for each class. For example:&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="nd"&gt;@TestPropertySource&lt;/span&gt;&lt;span class="o"&gt;({&lt;/span&gt;
    &lt;span class="s"&gt;"classpath:x.properties"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
    &lt;span class="s"&gt;"classpath:y.properties"&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;TestOne&lt;/span&gt;&lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;//...&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="nd"&gt;@TestPropertySource&lt;/span&gt;&lt;span class="o"&gt;({&lt;/span&gt;
    &lt;span class="s"&gt;"classpath:z.properties"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
    &lt;span class="s"&gt;"classpath:y.properties"&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;TestTwo&lt;/span&gt;&lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;//...&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="nd"&gt;@TestPropertySource&lt;/span&gt;&lt;span class="o"&gt;({&lt;/span&gt;
    &lt;span class="s"&gt;"classpath:x.properties"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
    &lt;span class="s"&gt;"classpath:z.properties"&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;TestThree&lt;/span&gt;&lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;//...&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="nd"&gt;@TestPropertySource&lt;/span&gt;&lt;span class="o"&gt;({&lt;/span&gt;
    &lt;span class="s"&gt;"classpath:x.properties"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
    &lt;span class="s"&gt;"classpath:z.properties"&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;TestFour&lt;/span&gt;&lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;//...&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this situation, Spring loads the context per each unique combination of property files. For instance, in the previous sample, it loads the context three times because x and z got repeated two times so the Spring can reuse the context for both of them.&lt;/p&gt;

&lt;p&gt;So I decided to eliminate the &lt;strong&gt;TestPropertySource&lt;/strong&gt; annotation from all of the integration test classes and aggregate them in an abstract class that all of them extend. Here is the new style of the test classes:&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="nd"&gt;@TestPropertySource&lt;/span&gt;&lt;span class="o"&gt;({&lt;/span&gt;
    &lt;span class="s"&gt;"classpath:x.properties"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
    &lt;span class="s"&gt;"classpath:y.properties"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
    &lt;span class="s"&gt;"classpath:z.properties"&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="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;AbstractTes&lt;/span&gt;&lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="c1"&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;TestX&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;AbstractTest&lt;/span&gt;&lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;//...&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, in this sample, it only loads the context once. I did something like this in the codebase and counted the context loads again. The result was promising: the number of context loads decreased to 2 (from 16). Note that, for example, if you have a property called &lt;strong&gt;database.url&lt;/strong&gt; both in &lt;strong&gt;x.properties&lt;/strong&gt; and &lt;strong&gt;z.properties&lt;/strong&gt;, the second one overrides the first one.&lt;/p&gt;

&lt;p&gt;I got through the remaining test classes. Guess what? All of them contained either &lt;strong&gt;@MockBean&lt;/strong&gt; or &lt;strong&gt;@SpyBean&lt;/strong&gt;. As I previously mentioned, in a separate blog post I will address the usage of these fancy annotations and the issues they may cause in integration tests.&lt;/p&gt;

&lt;h2&gt;
  
  
  Lesson Learned
&lt;/h2&gt;

&lt;p&gt;The whole process took about 3 to 4 hours. Consequently, the build time decreased from 10 to 4 minutes on Jenkins. It means that sometimes we could save plenty of time by investing some hours on following such issues that waste considerable amounts of time in the long term.&lt;/p&gt;

&lt;p&gt;I will address the performance issues in integration tests in a separate blog post soon. I would be grateful if you share your comments.&lt;/p&gt;

</description>
      <category>java</category>
      <category>testing</category>
      <category>springboot</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
