<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.9.2">Jekyll</generator><link href="/feed.xml" rel="self" type="application/atom+xml" /><link href="/" rel="alternate" type="text/html" /><updated>2022-09-18T23:18:47-07:00</updated><id>/feed.xml</id><title type="html">Zhenye’s Blog</title><subtitle>Sharing technical knowledge in a easy-to-learn way
</subtitle><author><name>Zhenye Na</name><email>nazhenye@gmail.com</email></author><entry><title type="html">Introduction to Dependency Injection in Java</title><link href="/2022/09/18/intro-to-java-dependency-injection.html" rel="alternate" type="text/html" title="Introduction to Dependency Injection in Java" /><published>2022-09-18T00:00:00-07:00</published><updated>2022-09-18T00:00:00-07:00</updated><id>/2022/09/18/intro-to-java-dependency-injection</id><content type="html" xml:base="/2022/09/18/intro-to-java-dependency-injection.html">&lt;h2 id=&quot;background&quot;&gt;Background&lt;/h2&gt;

&lt;h3 id=&quot;what-is-dependency-injection&quot;&gt;What is Dependency Injection?&lt;/h3&gt;

&lt;p&gt;If you’re not familiar with Dependency Injection, feel free to watch the following video to grasp the idea behind this pattern&lt;/p&gt;

&lt;div class=&quot;extensions extensions--video&quot;&gt;
  &lt;iframe src=&quot;https://www.youtube.com/embed/IKD2-MAkXyQ?rel=0&amp;amp;showinfo=0&quot; frameborder=&quot;0&quot; scrolling=&quot;no&quot; allowfullscreen=&quot;&quot;&gt;&lt;/iframe&gt;
&lt;/div&gt;

&lt;p&gt;And this article to understand the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;why&lt;/code&gt; part &lt;a href=&quot;https://medium.com/bigeye/dependency-injection-101-what-and-why-7bd11d53c528&quot;&gt;Dependency Injection 101 — What and Why&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Dependency Injection is a concrete application of the more generic &lt;strong&gt;Inversion of Control&lt;/strong&gt; principle in which the flow of the program is controlled by the program itself.&lt;/p&gt;

&lt;p&gt;It’s implemented through an external component that provides instances of objects (or dependencies) needed by other objects.&lt;/p&gt;

&lt;p&gt;Different frameworks implement dependency injection in different ways. In particular, one of the most notable of these differences is whether the injection happens at run-time or compile-time.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Run-time DI&lt;/strong&gt; is usually based on reflection which is simpler to use but slower at run-time
    &lt;ul&gt;
      &lt;li&gt;Example: Spring and Google Guice&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Compile-time DI&lt;/strong&gt;, on the other hand, is based on code generation. This means that all the heavy-weight operations are performed during compilation. Compile-time DI adds complexity but generally performs faster.
    &lt;ul&gt;
      &lt;li&gt;Example: Google Dagger 2&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;what-are-the-advantages-that-dependency-injection-provides&quot;&gt;What are the advantages that Dependency Injection provides?&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;Simplifies access to shared instances
    &lt;ul&gt;
      &lt;li&gt;Dagger 2 provides a simple way to obtain references to shared instances compared to using a Java constructor to create dependencies&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Easy configuration of complex dependencies
    &lt;ul&gt;
      &lt;li&gt;Module reuse&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Easier Unit Testing
    &lt;ul&gt;
      &lt;li&gt;We can easily mock the injected dependency to write unit tests&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;dependency-injection-with-dagger-2&quot;&gt;Dependency Injection with Dagger 2&lt;/h2&gt;

&lt;h3 id=&quot;pojo&quot;&gt;POJO&lt;/h3&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nd&quot;&gt;@Lombok&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;Data&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Car&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Engine&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;engine&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Brand&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;brand&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;nd&quot;&gt;@Inject&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Car&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Engine&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;engine&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Brand&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;brand&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;engine&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;engine&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;brand&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;brand&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;module&quot;&gt;Module&lt;/h3&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Module&lt;/code&gt; is the class with the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@Module&lt;/code&gt; annotation. This annotation indicates that the class can make dependencies available to the container&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@Module&lt;/code&gt;s are classes or interfaces that act as collections of instructions for Dagger on how to construct dependencies. They’re called modules because they are modular: you can mix and match modules in different applications and contexts.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nd&quot;&gt;@Module&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;includes&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;BrandModule&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;})&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;VehiclesModule&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;nd&quot;&gt;@Provides&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Engine&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;provideEngine&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Engine&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Here &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@Module(includes = {BrandModule.class})&lt;/code&gt; means that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VehiclesModule&lt;/code&gt; depends on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BrandModule&lt;/code&gt; and in order to build the object graph, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BrandModule&lt;/code&gt; is requried&lt;/p&gt;

&lt;p&gt;Below is the definition of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BrandModule&lt;/code&gt;. Noticed that instead of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@Singleton&lt;/code&gt; annotation that creates a singleton object, we also have other two annotations&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@Provides&lt;/code&gt; provides the dependency that the target class’s constructor needs&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@Named&lt;/code&gt; differentiate the dependency based on the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;name&quot;&lt;/code&gt; given&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nd&quot;&gt;@Module&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;BrandModule&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;nd&quot;&gt;@Provides&lt;/span&gt;
    &lt;span class=&quot;nd&quot;&gt;@Singleton&lt;/span&gt;
    &lt;span class=&quot;nd&quot;&gt;@Named&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Lamborghini&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Brand&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;provideLamboBrand&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt; 
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Brand&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Lamborghini&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt; 
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;nd&quot;&gt;@Provides&lt;/span&gt;
    &lt;span class=&quot;nd&quot;&gt;@Singleton&lt;/span&gt;
    &lt;span class=&quot;nd&quot;&gt;@Named&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Bugatti&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Brand&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;provideBugattiBrand&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt; 
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Brand&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Bugatti&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt; 
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;component&quot;&gt;Component&lt;/h3&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Component&lt;/code&gt; is the class that will generate &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Car&lt;/code&gt; instances, injecting dependencies provided by &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VehiclesModule&lt;/code&gt;. We need a method signature that returns a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Car&lt;/code&gt; and we need to mark the class with the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@Component&lt;/code&gt; annotation:&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nd&quot;&gt;@Singleton&lt;/span&gt;
&lt;span class=&quot;nd&quot;&gt;@Component&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;modules&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;VehiclesModule&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;VehiclesComponent&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nc&quot;&gt;Car&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;buildCar&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Notice how we passed our module class as an argument to the @Component annotation. If we didn’t do that, &lt;strong&gt;Dagger wouldn’t know how to build the car’s dependencies.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Also, since our module provides a singleton object, we must give the same scope to our component because &lt;strong&gt;Dagger doesn’t allow for unscoped components to refer to scoped bindings.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;After annotating with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@Component&lt;/code&gt;, Dagger will generate boilerplate code and prepend the class name with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Dagger&lt;/code&gt;, which means if we wanna use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VehiclesComponent&lt;/code&gt; in the code base, we can call &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DaggerVehiclesComponent&lt;/code&gt;&lt;/p&gt;

&lt;h3 id=&quot;client-code-usage&quot;&gt;Client code usage&lt;/h3&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nd&quot;&gt;@Test&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;givenGeneratedComponent_whenBuildingCar_thenDependenciesInjected&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;nc&quot;&gt;VehiclesComponent&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;component&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;DaggerVehiclesComponent&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;create&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;nc&quot;&gt;Car&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;car&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;component&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;buildCar&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;// ....&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;assisted-injection-with-dagger-2&quot;&gt;Assisted Injection with Dagger 2&lt;/h2&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Assisted injection&lt;/code&gt; is a dependency injection (DI) pattern that is used to construct an object where &lt;strong&gt;some parameters may be provided&lt;/strong&gt; by the DI framework and &lt;strong&gt;others must be passed in at creation time (a.k.a “assisted”) by the user.&lt;/strong&gt;&lt;/p&gt;

  &lt;p&gt;Dagger 2 documentation&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;To use Dagger’s assisted injection, annotate the constructor of an object with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@AssistedInject&lt;/code&gt; and annotate any assisted parameters with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@Assisted&lt;/code&gt;, as shown below:&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;MyDataService&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;nd&quot;&gt;@AssistedInject&lt;/span&gt;
    &lt;span class=&quot;nc&quot;&gt;MyDataService&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;DataFetcher&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dataFetcher&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nd&quot;&gt;@Assisted&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Config&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Next, define a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;factory&lt;/code&gt; that can be used to create an instance of the object. The factory must be annotated with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@AssistedFactory&lt;/code&gt; and must contain an abstract method that returns the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@AssistedInject&lt;/code&gt; type and takes in all &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@Assisted&lt;/code&gt; parameters defined in its constructor (in the same order). This is shown below:&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nd&quot;&gt;@AssistedFactory&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;MyDataServiceFactory&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nc&quot;&gt;MyDataService&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;create&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Config&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Finally, Dagger will create the implementation for the assisted factory and provide a binding for it. The factory can be injected as a dependency as shown below.&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;MyApp&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nd&quot;&gt;@Inject&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;MyDataServiceFactory&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;serviceFactory&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;nc&quot;&gt;MyDataService&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;setupService&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Config&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;nc&quot;&gt;MyDataService&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;service&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;serviceFactory&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;create&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;// ...&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;service&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Disambiguating &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@Assisted&lt;/code&gt; parameters with the same type&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If multiple &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@Assisted&lt;/code&gt; parameters have the same type, you must distinguish them by giving them an identifier. This can be done by adding a name via the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@Assisted(&quot;name&quot;)&lt;/code&gt; annotation. These must be put on both the factory method and the&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt; @AssistedInject&lt;/code&gt; type.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;MyDataService&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nd&quot;&gt;@AssistedInject&lt;/span&gt;
    &lt;span class=&quot;nc&quot;&gt;MyDataService&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;nc&quot;&gt;DataFetcher&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dataFetcher&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;nd&quot;&gt;@Assisted&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;server&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Config&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;serverConfig&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;nd&quot;&gt;@Assisted&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;client&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Config&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;clientConfig&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;nd&quot;&gt;@AssistedFactory&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;MyDataServiceFactory&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nc&quot;&gt;MyDataService&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;create&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;nd&quot;&gt;@Assisted&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;server&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Config&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;serverConfig&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;nd&quot;&gt;@Assisted&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;client&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Config&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;clientConfig&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;dependency-injection-with-guice&quot;&gt;Dependency Injection with Guice&lt;/h2&gt;

&lt;p&gt;Here is a great article that explains how to use Guice DI &lt;a href=&quot;https://medium.com/bigeye/dependency-injection-102-instrumentation-with-guice-1c45dd238c95&quot;&gt;Dependency Injection 102 -Instrumentation with Guice&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;misc---faq&quot;&gt;Misc - FAQ&lt;/h2&gt;

&lt;h3 id=&quot;what-is-the-difference-between-javaxinjectinject-and-comgoogleinjectinject-&quot;&gt;What is the difference between &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;javax.inject.Inject&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;com.google.inject.Inject&lt;/code&gt; ?&lt;/h3&gt;

&lt;p&gt;TL;DR: they are interchangeable.&lt;/p&gt;

&lt;p&gt;Check Google Guice: JSR-330 specification https://github.com/google/guice/wiki/JSR330&lt;/p&gt;

&lt;h2 id=&quot;references&quot;&gt;References&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://medium.com/bigeye/dependency-injection-101-what-and-why-7bd11d53c528&quot;&gt;Dependency Injection 101 — What and Why&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://guides.codepath.com/android/dependency-injection-with-dagger-2&quot;&gt;Dependency Injection with Dagger 2&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.baeldung.com/dagger-2&quot;&gt;Introduction to Dagger 2&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=IKD2-MAkXyQ&quot;&gt;YouTube: Dependency Injection&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://dagger.dev/dev-guide/assisted-injection.html&quot;&gt;Dagger 2 Assisted Injection&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.digitalocean.com/community/tutorials/google-guice-dependency-injection-example-tutorial&quot;&gt;Google Guice Dependency Injection Example Tutorial&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://dagger.dev/tutorial/&quot;&gt;Dagger 2 Tutorial&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content><author><name>Zhenye Na</name><email>nazhenye@gmail.com</email></author><category term="Java" /><category term="Dependency Injection" /><summary type="html">How to design and implement Dependency Injection Pattern in Java</summary></entry><entry><title type="html">Distributed Transactions and Sagas in Microservices</title><link href="/2022/06/19/distributed-transactions-and-sagas-in-microservices.html" rel="alternate" type="text/html" title="Distributed Transactions and Sagas in Microservices" /><published>2022-06-19T00:00:00-07:00</published><updated>2022-06-19T00:00:00-07:00</updated><id>/2022/06/19/distributed-transactions-and-sagas-in-microservices</id><content type="html" xml:base="/2022/06/19/distributed-transactions-and-sagas-in-microservices.html">&lt;blockquote&gt;
  &lt;p&gt;In this article, I will explain the what is Distributed Transaction, what is Saga and how can we implement the Saga Pattern with AWS Services/Tools&lt;/p&gt;

  &lt;p&gt;But before we start, here are some books I would like to recommend you to read if you are interested and understand in more detailed way:&lt;/p&gt;

  &lt;ul&gt;
    &lt;li&gt;&lt;a href=&quot;https://www.oreilly.com/library/view/building-microservices-2nd/9781492034018/&quot;&gt;O’Reilly - Building Microservices, 2nd Edition (Chapter 6: Workflow)&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;https://www.oreilly.com/library/view/building-microservices-2nd/9781492034018/&quot;&gt;Manning Publication - Microservices Patterns With examples in Java (Chapter 4: Managing Transactions with Sagas)&lt;/a&gt;&lt;/li&gt;
  &lt;/ul&gt;
&lt;/blockquote&gt;

&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;/h2&gt;

&lt;p&gt;Generally speaking, it is very common to see that multiple microservices are needed together to implement some business logic. For example, the image below inllustrates an example that how an online shopping application implements the “Order Fullfillment workflow” with multiple microservices. As you may know, each step depends on the success execution of previous step.&lt;/p&gt;

&lt;div align=&quot;center&quot;&gt;
  &lt;img src=&quot;https://raw.githubusercontent.com/Zhenye-Na/img-hosting-picgo/master/img/order-fullfillment-workflow-example.png&quot; /&gt;
&lt;/div&gt;

&lt;p&gt;Now, the question comes to what do we do if one thing goes wrong in this entire workflow and how can we mitigate the impact of the workflow failure? You might heard of a concept in Software Engineering called “Transactions”.&lt;/p&gt;

&lt;p&gt;Transactions in this context has the meaning or implies that, serveral actions or requests are binded together to be treated as one single unit. Each action in this overall unit executes successfully, then the “single unit” is treated as running successfully. However, if any step failed, then the whole unit is marked as failed. Since it failed then we might need to clean up some stale resources - Compensation, which we will cover later.&lt;/p&gt;

&lt;p&gt;We can utilize database transactions to ensure that the series of changes could be made successfully. Let’s start with what is ACID Transactions and Transactions involved multiple tables.&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;acid-transactions&quot;&gt;ACID Transactions&lt;/h2&gt;

&lt;p&gt;ACID is a commonly used acronym for a property that database transactions, which represents&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Property&lt;/th&gt;
      &lt;th&gt;Explanation&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Atomicity&lt;/td&gt;
      &lt;td&gt;Each statement in a transaction (to read, write, update or delete data) is treated as a single unit. Either the entire statement is executed, or none of it is executed. This property prevents data loss and corruption from occurring if, for example, if your streaming data source fails mid-stream.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Consistency&lt;/td&gt;
      &lt;td&gt;Ensures that transactions only make changes to tables in predefined, predictable ways. Transactional consistency ensures that corruption or errors in your data do not create unintended consequences for the integrity of your table.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Isolation&lt;/td&gt;
      &lt;td&gt;When multiple users are reading and writing from the same table all at once, isolation of their transactions ensures that the concurrent transactions don’t interfere with or affect one another. Each request can occur as though they were occurring one by one, even though they’re actually occurring simultaneously.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Durability&lt;/td&gt;
      &lt;td&gt;Ensures that changes to your data made by successfully executed transactions will be saved, even in the event of system failure.&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;With the above restrictions, things work pretty well with a single database or single table, collection, etc.. But what about there are two tables that are involved in the action?&lt;/p&gt;

&lt;h3 id=&quot;acid-without-atomicity&quot;&gt;ACID without Atomicity&lt;/h3&gt;

&lt;p&gt;Take the following image as an example: when the customer of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;customerId=123&lt;/code&gt; has been verified, then the corresponding row in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Customer&lt;/code&gt; table will update the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Status&lt;/code&gt; column from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PENDING&lt;/code&gt; to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VERIFIED&lt;/code&gt;. Meanwhile &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;customerId=123&lt;/code&gt; in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PendingEnrollments&lt;/code&gt; table should be removed since the customer is already verified.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://raw.githubusercontent.com/Zhenye-Na/img-hosting-picgo/master/img/transaction-boundary.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;When this operation is done in one “boundary” which is a single database, it is easily achievable. However, when it comes to the case that responsibilities are seperated. It actually becomes to a tricky question.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://raw.githubusercontent.com/Zhenye-Na/img-hosting-picgo/master/img/single-transcation-boundary.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;What if we already updated in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Customer&lt;/code&gt; table but the request to delete record in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Enrollment&lt;/code&gt; table failed ?&lt;/li&gt;
  &lt;li&gt;What if we already removed the record in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Enrollment&lt;/code&gt; table but the update request to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Customer&lt;/code&gt; timed out because of some database issues ?&lt;/li&gt;
&lt;/ol&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;distributed-transactions&quot;&gt;Distributed Transactions&lt;/h2&gt;

&lt;p&gt;As we mentioned above, Distributed Transactions may leave us inconsistent data, so let’s see one of the most common algorithms for implementing distributed transactions&lt;/p&gt;

&lt;h3 id=&quot;two-phase-commit-2pc&quot;&gt;Two-Phase Commit (2PC)&lt;/h3&gt;

&lt;p&gt;&lt;img src=&quot;https://raw.githubusercontent.com/Zhenye-Na/img-hosting-picgo/master/img/distributed-transactions-2pc.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;This article, &lt;a href=&quot;https://medium.com/geekculture/distributed-transactions-two-phase-commit-c82752d69324&quot;&gt;Distributed Transactions &amp;amp; Two-phase Commit&lt;/a&gt; explains what is Two-Phase Commit pretty well. In summary, 2PC is broken down into two phases:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Voting Phase
    &lt;ol&gt;
      &lt;li&gt;Coordinator contacts all the workers to ask if any workers will be included in the transactions and get the confirmation&lt;/li&gt;
    &lt;/ol&gt;
  &lt;/li&gt;
  &lt;li&gt;Commit Phase
    &lt;ol&gt;
      &lt;li&gt;A rollback message will be sent if any worker does not vote for the commit, so anything left could be cleaned up&lt;/li&gt;
    &lt;/ol&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Another good point mentioned in the book: &lt;a href=&quot;https://www.oreilly.com/library/view/building-microservices-2nd/9781492034018/&quot;&gt;O’Reilly - Building Microservices, 2nd Edition (Chapter 6: Workflow)&lt;/a&gt; regarding the communication of Coordinator and Worker is that:&lt;/p&gt;

&lt;p&gt;If the latency between Cordinator and participated workers increases, the worker will process the request more slowly. Then we will observe the intermediate state during the transaction with some probability. This violates the “Isolation” property from ACID.&lt;/p&gt;

&lt;p&gt;So the main take away for learning the 2PC algorithm is that:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;2PC will inject a large amount of latency to your system, since each worker will have to apply a Lock to your database table to perform any operations&lt;/li&gt;
  &lt;li&gt;The longer the operation takes, the higher probabilty you will see intermediate state&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;So, genrally, dont use this algorithm&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;sagas&quot;&gt;Sagas&lt;/h2&gt;

&lt;p&gt;TODO&lt;/p&gt;

&lt;hr /&gt;

&lt;h3 id=&quot;orchestrated-saga&quot;&gt;Orchestrated Saga&lt;/h3&gt;

&lt;p&gt;TODO&lt;/p&gt;

&lt;p&gt;In summary:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Centralized corordination and tracking&lt;/li&gt;
  &lt;li&gt;Follow original solution space&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;h3 id=&quot;choreography-saga&quot;&gt;Choreography Saga&lt;/h3&gt;

&lt;p&gt;TODO&lt;/p&gt;

&lt;p&gt;Event-based&lt;/p&gt;

&lt;p&gt;In summary&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Avoid centralized coordination&lt;/li&gt;
  &lt;li&gt;Loosely coupled model&lt;/li&gt;
  &lt;li&gt;Complicated progress tracking&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;implement-saga-pattern-using-aws-step-functions&quot;&gt;Implement Saga Pattern using AWS Step Functions&lt;/h2&gt;

&lt;p&gt;TODO&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://raw.githubusercontent.com/Zhenye-Na/img-hosting-picgo/master/img/step-functions-sage-pattern.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;references&quot;&gt;References&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;a href=&quot;https://microservices.io/patterns/data/saga.html&quot;&gt;Microservice Architecture - Pattern: Saga&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.microsoft.com/en-us/azure/architecture/reference-architectures/saga/saga&quot;&gt;Saga distributed transactions pattern&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://workflow-core.readthedocs.io/en/latest/sagas/&quot;&gt;Example: Saga transaction with compensation&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.aws.amazon.com/prescriptive-guidance/latest/modernization-data-persistence/saga-pattern.html&quot;&gt;Saga pattern in AWS&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.aws.amazon.com/prescriptive-guidance/latest/patterns/implement-the-serverless-saga-pattern-by-using-aws-step-functions.html&quot;&gt;Implement the serverless saga pattern by using AWS Step Functions&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://aws.amazon.com/blogs/compute/building-a-serverless-distributed-application-using-a-saga-orchestration-pattern/&quot;&gt;Building a serverless distributed application using a saga orchestration pattern&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://aws.plainenglish.io/saga-pattern-misconceptions-and-serverless-solution-on-aws-3bee0c96212f&quot;&gt;Saga Pattern: Misconceptions and Serverless Solution on AWS&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;</content><author><name>Zhenye Na</name><email>nazhenye@gmail.com</email></author><category term="Microservice" /><category term="Workflow" /><category term="Distributed Transaction" /><category term="Saga" /><category term="AWS" /><summary type="html">How to implement workflows for chaining multiple microservices</summary></entry><entry><title type="html">Design Patterns: Strategy Pattern</title><link href="/2022/05/16/design-patterns-the-strategy-pattern.html" rel="alternate" type="text/html" title="Design Patterns: Strategy Pattern" /><published>2022-05-16T00:00:00-07:00</published><updated>2022-05-16T00:00:00-07:00</updated><id>/2022/05/16/design-patterns-the-strategy-pattern</id><content type="html" xml:base="/2022/05/16/design-patterns-the-strategy-pattern.html">&lt;h2 id=&quot;intent&quot;&gt;Intent&lt;/h2&gt;

&lt;p&gt;There are common situations when classes differ only in their behavior. For this cases is a good idea to isolate the algorithms in separate classes in order to have the ability to select different algorithms at runtime.&lt;/p&gt;

&lt;p&gt;Strategy Pattern defines a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.&lt;/p&gt;

&lt;p&gt;Let’s use a real life example to define the problem and see how to use Strategy Pattern can be applied to solve it&lt;/p&gt;

&lt;h2 id=&quot;real-life-example&quot;&gt;Real-life Example&lt;/h2&gt;

&lt;p&gt;Suppose you are trying to design a game, to be specific, it is about swiming racing. Here are different swimming strokes that we will support in the game:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://raw.githubusercontent.com/Zhenye-Na/img-hosting-picgo/master/img/b30c1eab225297bedfd43b719d109aa5.jpg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Now, let’s think how we could implement each swimming techniques in the programming point of view.&lt;/p&gt;

&lt;p&gt;Naively, the first solution or method in my mind is that could think about is using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Inheritance&lt;/code&gt; and override each method in the child class. Here is an UML example of how the class should be inherited.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://mermaid.live/edit#pako:eNp9lL9uwyAQh18FMbVS8gIeOkRJt05uNxYKZxeFPxGGWlaady9D3Bz2uRu-7xO6nw-4chU08IYrK4fhaGQfpRNe-ENOCWJnp3Y0zhnfs_3-54XNX8K_RoA2TRa2hEMEOaQ2xXDedqQ6_28I_0ca9m4csJPXOUqvoEKjjO7j8vRcFVUI9hhGvyh_ls7SFyyqZYO38L2sWujnKvVT1j1RzqM5iuIuKY7apTDqm8JVgPXQ1gEo5xGAojgAxVEACqMAFK4CbBwrYgy0hiZBC9UwaAXPgzbwSGijPlbrW0AEoiQUh8JVGErAUSiOg1AcxeA77iA6aXR5Ta7CMyZ42dqB4E1Zauhktklw4W9FzRctE5y0SSHyppN2gB2XOYV28oo3KWaYpfujdLduv1bbt2I&quot;&gt;&lt;img src=&quot;https://mermaid.ink/img/pako:eNp9lL9uwyAQh18FMbVS8gIeOkRJt05uNxYKZxeFPxGGWlaady9D3Bz2uRu-7xO6nw-4chU08IYrK4fhaGQfpRNe-ENOCWJnp3Y0zhnfs_3-54XNX8K_RoA2TRa2hEMEOaQ2xXDedqQ6_28I_0ca9m4csJPXOUqvoEKjjO7j8vRcFVUI9hhGvyh_ls7SFyyqZYO38L2sWujnKvVT1j1RzqM5iuIuKY7apTDqm8JVgPXQ1gEo5xGAojgAxVEACqMAFK4CbBwrYgy0hiZBC9UwaAXPgzbwSGijPlbrW0AEoiQUh8JVGErAUSiOg1AcxeA77iA6aXR5Ta7CMyZ42dqB4E1Zauhktklw4W9FzRctE5y0SSHyppN2gB2XOYV28oo3KWaYpfujdLduv1bbt2I&quot; alt=&quot;&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For example: in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BreastStrokeSwimming&lt;/code&gt; class, we will override the each method in the parent class (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Swimming&lt;/code&gt;). Then let’s assume that some of the swimming styles share the same implementation logic, for example, maybe &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;warmUp()&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;coolDown()&lt;/code&gt; are exactly the same across all styles but not for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BackStrokeSwimming&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;legMove()&lt;/code&gt; method is the same for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FreeStyleSwimming&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BackStrokeSwimming&lt;/code&gt;. Given these assumption, we will noticed that code blocks for method &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;warmUp()&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;coolDown()&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;legMove()&lt;/code&gt; will be duplicated to some extents.&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Swimming&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Time&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;endurance&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;warmUp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{}&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;coolDown&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{}&lt;/span&gt;

    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;breathe&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{}&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;armMove&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{}&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;legMove&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{}&lt;/span&gt;

&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;


&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;FreeStyleSwimming&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Swimming&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Time&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;endurance&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;nd&quot;&gt;@Override&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;warmUp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;// ...&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;nd&quot;&gt;@Override&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;coolDown&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;// ...&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;nd&quot;&gt;@Override&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;breathe&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{}&lt;/span&gt;

    &lt;span class=&quot;nd&quot;&gt;@Override&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;armMove&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{}&lt;/span&gt;

    &lt;span class=&quot;nd&quot;&gt;@Override&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;legMove&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;As a developer, we should always align with &lt;a href=&quot;https://www.wikiwand.com/en/Don%27t_repeat_yourself&quot;&gt;Don’t repeat yourself&lt;/a&gt; principlie when writing codes. But what is the solution?&lt;/p&gt;

&lt;p&gt;Actually this is when Strategy Pattern could come in to save out time. Consider the following UML for the changes we make:&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://mermaid.live/edit#pako:eNqNlkFvgkAQhf8K2VPb6B8gxkSjTZq0J9obly2MlsguZlk0xPrfu4i2w_Io6kXnvVlmvjyFk0iKlEQoklyW5SqTWyNVrGO9rKwls8nr6JgpleltMJ1-z4Pbt1g_G6LI1jkNGZaGZGkja4rdsEcmu_8drP4UvDRn2i93XSMtbWtfXhj1VhxIkbZDllfa9i3OlKl9Tt7x7Sz9iy7Sg9QJpXfam_cFb087xTpwr9ks0w1smdB83pY-W-PDY6zPrB8sOHyGck7_ALD-nQf8YgyDozTqY98orJgURb4qjtors0VYVbZ7eNW8Hc6rvmeKgrVOK9NAx9nkMyGVD4d0NiWS2bhIZnMj2V8AefxkXCf6CxFsAnFoZh3pAhloVuCB7f-4OWGkcsJIZ4SRzAgjmRFGsk8YeUYJw6ZRwrDrDsLw37ETY2zoJBlbeJixg-cZO3iksaOXamwbD_ZA33i2Bxrvgd-_7XTQI7kDHhk4dqRz6EjnyJHeA45M47hh1zhs2DaCWkyEIqNklroHjMttJhZuNkWxCN3HlDayym0s3B3GWat96rrWaWYLI8KNzEuaCFnZIqp1IkJrKrqZrs8pV9f5B7IjQFw&quot;&gt;&lt;img src=&quot;https://mermaid.ink/img/pako:eNqNlkFvgkAQhf8K2VPb6B8gxkSjTZq0J9obly2MlsguZlk0xPrfu4i2w_Io6kXnvVlmvjyFk0iKlEQoklyW5SqTWyNVrGO9rKwls8nr6JgpleltMJ1-z4Pbt1g_G6LI1jkNGZaGZGkja4rdsEcmu_8drP4UvDRn2i93XSMtbWtfXhj1VhxIkbZDllfa9i3OlKl9Tt7x7Sz9iy7Sg9QJpXfam_cFb087xTpwr9ks0w1smdB83pY-W-PDY6zPrB8sOHyGck7_ALD-nQf8YgyDozTqY98orJgURb4qjtors0VYVbZ7eNW8Hc6rvmeKgrVOK9NAx9nkMyGVD4d0NiWS2bhIZnMj2V8AefxkXCf6CxFsAnFoZh3pAhloVuCB7f-4OWGkcsJIZ4SRzAgjmRFGsk8YeUYJw6ZRwrDrDsLw37ETY2zoJBlbeJixg-cZO3iksaOXamwbD_ZA33i2Bxrvgd-_7XTQI7kDHhk4dqRz6EjnyJHeA45M47hh1zhs2DaCWkyEIqNklroHjMttJhZuNkWxCN3HlDayym0s3B3GWat96rrWaWYLI8KNzEuaCFnZIqp1IkJrKrqZrs8pV9f5B7IjQFw&quot; alt=&quot;&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Parent class still contains those methods and child classes override each method&lt;/li&gt;
  &lt;li&gt;However, child classes take dependency on a new Interface variable, for example: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IBreathStrategy&lt;/code&gt; and there will be two classes &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SimpleBreatheStrategy&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AdvancedBreatheStrategy&lt;/code&gt; which implement the interface for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;breathe()&lt;/code&gt; method.
    &lt;ul&gt;
      &lt;li&gt;note: in the UML, I just add the implement class for interface &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IBreathStrategy&lt;/code&gt; for simplification, actually every interface will have specific strategy class to implement the actual logic&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;What benefits does this approach give?&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Reduce Code Redundancy
    &lt;ol&gt;
      &lt;li&gt;Since actual logic of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;breathe()&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;move()&lt;/code&gt; method is abstracted benhind the interface, in the child class, we can simply do &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;breatheStrategy.breathe()&lt;/code&gt; in the method.&lt;/li&gt;
      &lt;li&gt;In this way, one logic could be used everywhere applicable, without duplicating the whole code block&lt;/li&gt;
    &lt;/ol&gt;
  &lt;/li&gt;
  &lt;li&gt;Single Responsibility
    &lt;ol&gt;
      &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;XXXStrategy&lt;/code&gt; Class is just for the logic/implementation of one single method, which the implementation is abstracted from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;XXXSwimming&lt;/code&gt; instances we instantiated&lt;/li&gt;
    &lt;/ol&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;problem&quot;&gt;Problem&lt;/h2&gt;

&lt;p&gt;Figure demonstrates how this is routinely achieved - encapsulate interface details in a base class, and bury implementation details in derived classes. Clients can then couple themselves to an interface, and not have to experience the upheaval associated with change: no impact when the number of derived classes changes, and no impact when the implementation of a derived class changes.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://raw.githubusercontent.com/Zhenye-Na/img-hosting-picgo/master/img/Strategy1.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;A generic value of the software community for years has been, “maximize cohesion and minimize coupling”. The object-oriented design approach shown in figure is all about minimizing coupling. Since the client is coupled only to an abstraction (i.e. a useful fiction), and not a particular realization of that abstraction, the client could be said to be practicing “abstract coupling” . an object-oriented variant of the more generic exhortation “minimize coupling”.&lt;/p&gt;

&lt;p&gt;A more popular characterization of this “abstract coupling” principle is “Program to an interface, not an implementation”.&lt;/p&gt;

&lt;p&gt;Clients should prefer the “additional level of indirection” that an interface (or an abstract base class) affords. The interface captures the abstraction (i.e. the “useful fiction”) the client wants to exercise, and the implementations of that interface are effectively hidden.&lt;/p&gt;

&lt;h2 id=&quot;pros-and-cons&quot;&gt;Pros and Cons&lt;/h2&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Pros&lt;/th&gt;
      &lt;th&gt;Cons&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Each strategy (algorithm/implementation) is easily inter-changable&lt;/td&gt;
      &lt;td&gt;Overkill if the strategy never changes&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Each strategy’s implementation is abstracted from the main class&lt;/td&gt;
      &lt;td&gt;Could confuse clients on which is the proper strategy to use&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Favor composition instead of inheritance&lt;/td&gt;
      &lt;td&gt;Complicated for programming language which support functional programming&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Open/Close Principle&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;(Optional) SIngleton Pattern could be applied to create instances for each strategy&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;reference&quot;&gt;Reference&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.oodesign.com/strategy-pattern.html&quot;&gt;Strategy Pattern - OODesign&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://refactoring.guru/design-patterns/strategy&quot;&gt;Strategy Pattern - Refactoring.guru&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.tutorialspoint.com/design_pattern/strategy_pattern.htm&quot;&gt;Design Patterns - Strategy Pattern&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content><author><name>Zhenye Na</name><email>nazhenye@gmail.com</email></author><category term="Design Patterns" /><category term="Strategy Pattern" /><category term="Object-Oriented Design" /></entry><entry><title type="html">Java Best Practices - Logging</title><link href="/2022/05/03/java-best-practices-logging.html" rel="alternate" type="text/html" title="Java Best Practices - Logging" /><published>2022-05-03T00:00:00-07:00</published><updated>2022-05-03T00:00:00-07:00</updated><id>/2022/05/03/java-best-practices-logging</id><content type="html" xml:base="/2022/05/03/java-best-practices-logging.html">&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;What is Logging?&lt;/strong&gt;&lt;/p&gt;

  &lt;p&gt;Logging is to keep the log that records either events that occur in an operating system or applications as a file, so that developer can trace in for more details when something bad happened.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2 id=&quot;logging-types&quot;&gt;Logging Types&lt;/h2&gt;

&lt;p&gt;Logging is an important topic in software development and I cannot stress how important and helpful enough when it comes to debugging a problem or error during daily development. There are several different types of logs in Java and let’s dive deep to look into them.&lt;/p&gt;

&lt;h3 id=&quot;application-logs&quot;&gt;Application Logs&lt;/h3&gt;

&lt;p&gt;Usually, the application produces the logs. for example&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;em&gt;Information&lt;/em&gt; on what is the request context, like what methods are triggered and what is the response&lt;/li&gt;
  &lt;li&gt;&lt;em&gt;Warning&lt;/em&gt; message of an abnormal invocation&lt;/li&gt;
  &lt;li&gt;&lt;em&gt;Error&lt;/em&gt; message like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;InternalServerException&lt;/code&gt; or just the client request is malformed&lt;/li&gt;
  &lt;li&gt;etc …&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;server-logs&quot;&gt;Server Logs&lt;/h3&gt;

&lt;p&gt;This could be produced by Tomcat/Netty Server, Nginx Server, etc. It might contain information concerning Server healthy status, Running status, etc.&lt;/p&gt;

&lt;p&gt;If you adopted AWS Lambda as the deploy method, your Lambda function comes with a CloudWatch Logs log group and a log stream for each instance of your function. The Lambda runtime environment sends details about each invocation to the log stream and relays logs and other output from your function’s code.&lt;/p&gt;

&lt;h3 id=&quot;jvm-logs&quot;&gt;JVM Logs&lt;/h3&gt;

&lt;p&gt;The JVM logs are created by redirecting the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;System.out&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;System.err&lt;/code&gt; streams of the JVM to independent log files.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;System.out&lt;/code&gt; log is used to monitor the health of the running application server.&lt;/li&gt;
  &lt;li&gt;The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;System.err&lt;/code&gt; log contains exception stack trace information that is useful when performing problem analysis.&lt;/li&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
  &lt;p&gt;Reference: &lt;a href=&quot;https://www.ibm.com/docs/en/was-nd/8.5.5?topic=logs-java-virtual-machine-jvm-log-settings&quot;&gt;Java virtual machine (JVM) log settings&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4 id=&quot;garbage-collector-logs&quot;&gt;Garbage collector Logs&lt;/h4&gt;

&lt;p&gt;Within the large set of JVM logs, there is one type of log which could provide some insights for developers which is the Garbage collector logs. By default, the implementation of Garbage Collection in Java is pretty efficient. However, if your application has a very large throughput and in case the default Garbage Collection implementation reaches to bottleneck&lt;/p&gt;

&lt;p&gt;By default the GC log is off, you can use the following properties to enable the GC log for Java 8 or earlier&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;-XX:+PrintGCDetails -Xloggc:
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;system-logs&quot;&gt;System Logs&lt;/h3&gt;

&lt;p&gt;Logs that are written by your OS. Since most developers use Linux/Unix system, it will be good if you know Syslog service&lt;/p&gt;

&lt;p&gt;To know more about System Logs, check the resource posted by Stackify here: https://stackify.com/syslog-101/&lt;/p&gt;

&lt;h2 id=&quot;basic-concepts&quot;&gt;Basic Concepts&lt;/h2&gt;

&lt;h3 id=&quot;logger&quot;&gt;Logger&lt;/h3&gt;

&lt;p&gt;Loggers are created in your Java application, for example:&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;LoggingDemo&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;org&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;apache&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;logging&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;log4j&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;Logger&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;log&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;org&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;apache&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;logging&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;log4j&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;LogManager&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;getLogger&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;LogExample&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;

    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;info&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Log something here&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

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

&lt;p&gt;They are objects that trigger/extract log events. They will capture it and pass the log events to Appender&lt;/p&gt;

&lt;h3 id=&quot;appender&quot;&gt;Appender&lt;/h3&gt;

&lt;p&gt;Appenders forward logs from Loggers to an &lt;strong&gt;output destination&lt;/strong&gt;. During this process, log messages are formatted using a &lt;strong&gt;Layou&lt;/strong&gt;t before being delivered to their final destination. Multiple Appenders can be combined to write log events to multiple destinations. For example, &lt;em&gt;a single event can be simultaneously displayed in a console and written to a file&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;I have listed some of the commonly used appenders below&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ConsoleAppender&lt;/code&gt;&lt;/strong&gt;: Console Appender appends the log events to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;System.out&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;System.err&lt;/code&gt; using a layout specified by the user. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;System.out&lt;/code&gt; is a default target. It is useful for debugging purposes, but not much beneficial to use in a production environment.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FileAppender&lt;/code&gt;&lt;/strong&gt;: Appends log events to a file. It supports two more appender classes. Both are the most widely used appenders that provide support to write logs to files.
    &lt;ul&gt;
      &lt;li&gt;&lt;em&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RollingFileAppender&lt;/code&gt;&lt;/em&gt;: Rotates based on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dateFormat&lt;/code&gt;&lt;/li&gt;
      &lt;li&gt;&lt;em&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DailyRollingFileAppender&lt;/code&gt;&lt;/em&gt;: Rotates based on a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;maximum file size&lt;/code&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SyslogAppender&lt;/code&gt;: It sends messages to a remote Syslog domain.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;WriterAppender&lt;/code&gt;: It is used to append log events to a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Writer&lt;/code&gt; or an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;OutputStream&lt;/code&gt; depending on the user’s choice.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AsyncAppender&lt;/code&gt;: The AsyncAppender allows users to log events &lt;em&gt;asynchronously&lt;/em&gt;. The AsyncAppender will save the events sent to it and then dispatch them to all the appenders that are attached to it. We can attach multiple appenders to an AsyncAppender.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;JMSAppender&lt;/code&gt;: A simple appender that publishes events to a JMS (Java Message Service) Topic. The events are serialized and transmitted as JMS message type ObjectMessage.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;layout&quot;&gt;Layout&lt;/h3&gt;

&lt;p&gt;This property is to define the preferred format of your logs.&lt;/p&gt;

&lt;p&gt;For example, you can use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;JSONLayout&lt;/code&gt; for the Appender, you will have a log entry message that looks like the following (from Log4j2 website: https://logging.apache.org/log4j/2.x/manual/layouts.html)&lt;/p&gt;

&lt;div class=&quot;language-json highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
  &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;instant&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;epochSecond&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1493121664&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;nanoOfSecond&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;118000000&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
  &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
  &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;thread&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;main&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
  &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;level&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;INFO&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
  &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;loggerName&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;HelloWorld&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
  &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;marker&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;name&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;child&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;parents&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
      &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;name&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;parent&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
      &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;parents&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;name&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;grandparent&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
      &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
  &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
  &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;message&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;Hello, world!&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
  &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;thrown&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;commonElementCount&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;message&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;error message&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;name&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;java.lang.RuntimeException&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;extendedStackTrace&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
      &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;class&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;logtest.Main&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
      &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;method&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;main&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
      &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;file&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;Main.java&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
      &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;line&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;29&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
      &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;exact&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
      &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;location&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;classes/&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
      &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;version&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;?&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
  &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
  &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;contextStack&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;one&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;two&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
  &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;endOfBatch&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
  &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;loggerFqcn&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;org.apache.logging.log4j.spi.AbstractLogger&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
  &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;contextMap&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;bar&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;BAR&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;foo&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;FOO&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
  &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
  &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;threadId&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
  &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;threadPriority&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
  &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;source&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;class&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;logtest.Main&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;method&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;main&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;file&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;Main.java&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;line&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;29&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
  &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;logging-frameworks&quot;&gt;Logging Frameworks&lt;/h2&gt;

&lt;p&gt;In Java, there are several logging libraries and you don’t have to be familiar with each one, but we should be familiar with the fundamental concepts and know which library we should use.&lt;/p&gt;

&lt;p&gt;Commonly used logging libraries include but not limit to: &lt;em&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Slf4j&lt;/code&gt;&lt;/em&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Log4j&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Logback&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Log4j2&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Apache Common-logging&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;java.util.logging&lt;/code&gt; etc. Below is the diagram which illustrates the relationship between them&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://raw.githubusercontent.com/Zhenye-Na/img-hosting-picgo/master/img/slf4j-to-other-log.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;SLF4j: &lt;strong&gt;Simple Logging Facade for Java&lt;/strong&gt; (SLF4j) serves as a simple &lt;strong&gt;facade or abstraction&lt;/strong&gt; for various logging frameworks allowing the end-user to plug in the desired logging framework at &lt;em&gt;deployment time&lt;/em&gt;
    &lt;ul&gt;
      &lt;li&gt;It is not itself a logging library, but a generic &lt;strong&gt;interface&lt;/strong&gt; to one of many logging libraries.&lt;/li&gt;
      &lt;li&gt;IF you are not familiar with Facade Pattern, check my previous article on Facade Pattern &lt;a href=&quot;https://zhenye-na.github.io/blog/2021/12/09/design-patterns-the-facade-pattern.html&quot;&gt;here&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Log4j: Apache Java Logging framework&lt;/li&gt;
  &lt;li&gt;Logback - Successor to the Log4j project&lt;/li&gt;
  &lt;li&gt;Log4j2 - Improved and fixed issues in Logback and Log4j&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Compared to Logback and Log4j, Log4j2 is preferred to use as the logging framework nowadays since the performance is better as well as more features. If performance is not an important factor then Lobback could be the second option&lt;/p&gt;

&lt;h2 id=&quot;log-levels&quot;&gt;Log levels&lt;/h2&gt;

&lt;p&gt;There are several levels of logs, each representing different “severity” of the logged event, here are some:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TRACE&lt;/code&gt;: This should ONLY be used during development to track bugs, but NEVER committed to your VCS.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DEBUG&lt;/code&gt;: This is mostly used during debugging. Leave only the most meaningful entries before going into production, and can be activated during troubleshooting.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;INFO&lt;/code&gt;: This is for all user-driven actions, or system-specific (ie regularly scheduled operations…)&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NOTICE&lt;/code&gt;: This will be the level at which the program will run when in production. Log at this level all the notable events that are not considered an error.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;WARN&lt;/code&gt;: This is for all events that could potentially become an error. For example, one API call took more than a predefined time&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ERROR&lt;/code&gt;: This is for logging error/exception conditions at this level. That can be API calls that return errors or internal error conditions.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FATAL&lt;/code&gt;: Don’t use this until Dooms Day&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;log-messages&quot;&gt;Log messages&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Meaningful&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;When writing your log entry messages, always anticipate that there are emergencies where the only thing you have is the log file, from which you have to understand what happened. Doing it right might be the subtle difference between getting fired and promoted.&lt;/li&gt;
  &lt;li&gt;Keep the audience in mind, it is not only you who read the log, customers, developers, DevOps engs, test engineers, etc.&lt;/li&gt;
  &lt;li&gt;Don’t log message that depends on a previous message’s content, this is bad because:
    &lt;ul&gt;
      &lt;li&gt;previous message maybe in another logging level&lt;/li&gt;
      &lt;li&gt;the other log message may appear far before or after if this is a multi-threading environment&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;With Context&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;As mentioned above, imagine that give a SEV2 ticket, the only thing you have is your CloudWatch Logs. Adding proper context to your log messages will save you time to find the root cause.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Machine Parse-able (Optional)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If we perform automation/monitoring on the log messages, with a good machine-readable format, like JSON, it will be easy to be parsed and sent to the monitoring system.&lt;/p&gt;

&lt;p&gt;Compare the two messages below, you can easily find out that the second one is more readable, for both humans and machines.&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;info&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;User {} plays game {}&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;userId&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;gameId&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// =&amp;gt; [INFO] c.d.g.UserRequest  User 2333 plays GTA5&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// vs&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// just for the idea&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;info&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gson&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;toJson&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;User&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// =&amp;gt; [INFO] c.d.g.UserRequest {&quot;User&quot;:&quot;2333&quot;, &quot;Game&quot;:&quot;GTA5&quot;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;dont-log-sensitive-message&quot;&gt;Don’t log sensitive message&lt;/h4&gt;

&lt;ul&gt;
  &lt;li&gt;Credentials, Session Tokens, etc.&lt;/li&gt;
  &lt;li&gt;Don’t expose internal services to external customers&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;mapped-diagnostic-context-mdc&quot;&gt;Mapped Diagnostic Context (MDC)&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;What is MDC?
    &lt;ul&gt;
      &lt;li&gt;MDC is short for “Mapped Diagnostic Context”, it helps to log the information that normal logging cannot&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Why we wanna use it?
    &lt;ul&gt;
      &lt;li&gt;suppose we wanna add the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;requestId&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;orderId&lt;/code&gt; for every request that comes in, we can store &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;orderId&lt;/code&gt; in MDC and access it later&lt;/li&gt;
      &lt;li&gt;MDC can enable us to use a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Map-like&lt;/code&gt; structure to store and access information&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;threadcontext&quot;&gt;ThreadContext&lt;/h3&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ThreadContext&lt;/code&gt; data structure is the MDC implementation in Log4j2, which is based on &lt;a href=&quot;https://docs.oracle.com/javase/6/docs/api/java/lang/ThreadLocal.html&quot;&gt;ThreadLocal&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;- At the start of the thread, clear the `ThreadContext` object and fill in information that you want for log messages, like `requestId`, `awsAccountId` etc.
- Log the message with accessing it like `ThreadContext.get(&quot;awsAccountId&quot;)`
    - the best way is to update the Log4j2 configuration file to make it automatically add the variables in ThreadContext without accessing `ThreadContext.get(&quot;awsAccountId&quot;)` explicitly in codes
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Example codes are as follows:&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nd&quot;&gt;@NoArgsConstructor&lt;/span&gt;
&lt;span class=&quot;nd&quot;&gt;@AllArgsConstructor&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Log4j2MDCDemo&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;implements&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Runnable&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Transaction&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tx&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;initiateContext&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;

        &lt;span class=&quot;c1&quot;&gt;// invoke methods ...&lt;/span&gt;

        &lt;span class=&quot;n&quot;&gt;clearContext&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;// before execution, add information to threadContext object&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;initiateContext&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;nc&quot;&gt;ThreadContext&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;put&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;transaction.id&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tx&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;getTransactionId&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;());&lt;/span&gt;
        &lt;span class=&quot;nc&quot;&gt;ThreadContext&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;put&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;transaction.owner&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tx&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;getOwner&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;());&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;// after execution, clear the threadContext object&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;clearContext&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;nc&quot;&gt;ThreadContext&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;clearAll&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

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

&lt;h2 id=&quot;lombok-log-and-friends&quot;&gt;Lombok @Log (and friends)&lt;/h2&gt;

&lt;p&gt;During the daily usage of Logging, we have to create &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Logger&lt;/code&gt; in each class and this is pretty verbose. Take &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Log4j2&lt;/code&gt; for example:&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;LoggingDemo&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;org&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;apache&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;logging&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;log4j&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;Logger&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;log&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;org&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;apache&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;logging&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;log4j&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;LogManager&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;getLogger&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;LogExample&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;

    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;info&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Log something here&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

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

&lt;p&gt;With the help of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lombok.extern.log4j&lt;/code&gt; package, it could be simplified as&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nd&quot;&gt;@Log4j2&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;LoggingDemo&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;info&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Log something here&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

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

&lt;p&gt;The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Log4j2&lt;/code&gt; annotation will automatically generate a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;static final log&lt;/code&gt; variable for you. If a field called &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;log&lt;/code&gt; already exists, a warning will be emitted, and &lt;strong&gt;no code will be generated&lt;/strong&gt;.&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;references&quot;&gt;References&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.loggly.com/ultimate-guide/java-logging-basics/&quot;&gt;Java Logging Basics&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://stackoverflow.com/a/39563140&quot;&gt;What is the difference between Log4j, SLF4J, and Logback?&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://stackify.com/syslog-101/&quot;&gt;Syslog Tutorial: How It Works, Examples, Best Practices, and More&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://stackoverflow.com/a/5884722&quot;&gt;What is the difference between log4j RollingFileAppender vs DailyRollingFileAppender&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content><author><name>Zhenye Na</name><email>nazhenye@gmail.com</email></author><category term="Java" /><summary type="html">Learn logging concepts and effectively using logging in Java</summary></entry><entry><title type="html">Java Best Practices - Lombok, JUnit 5 and Mockito</title><link href="/2022/04/23/java-best-practices-lombok-junit5-and-mockito.html" rel="alternate" type="text/html" title="Java Best Practices - Lombok, JUnit 5 and Mockito" /><published>2022-04-23T00:00:00-07:00</published><updated>2022-04-23T00:00:00-07:00</updated><id>/2022/04/23/java-best-practices-lombok-junit5-and-mockito</id><content type="html" xml:base="/2022/04/23/java-best-practices-lombok-junit5-and-mockito.html">&lt;h2 id=&quot;lombok&quot;&gt;Lombok&lt;/h2&gt;

&lt;p&gt;Lombok is a very handy library that is used pretty commonly for Java development. The main use case of Lombok is that it generates the boilerplate code for you silently during &lt;strong&gt;compile-time&lt;/strong&gt;. So that you don’t need to manually create those “utility” methods every time, and you have access to all these methods as usual within IDE while writing code from the generated &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.class&lt;/code&gt; file.&lt;/p&gt;

&lt;p&gt;I have listed some commonly used annotations in Lombok here and will elaborate more on each of those:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;var&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;val&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@Data&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@Builder&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@SuperBuilder&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@NonNull&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@Cleanup&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@RequiredArgsConstructor(onConstructor = @__(@Inject))&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;1-var-and-val&quot;&gt;1. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;var&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;val&lt;/code&gt;&lt;/h3&gt;

&lt;p&gt;These two aren’t annotations, but they are still super helpful. They can be easily explained in almost two lines&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;// without Lombok var or val&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;VeryComplexOperationRequest&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;request&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;VeryComplexOperationRequest&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;withRequestBody&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// with Lombok var&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt;                         &lt;span class=&quot;n&quot;&gt;request&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;VeryComplexOperationRequest&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;withRequestBody&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// with Lombok val&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;val&lt;/span&gt;                               &lt;span class=&quot;n&quot;&gt;request&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;VeryComplexOperationRequest&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;withRequestBody&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p class=&quot;info&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;var&lt;/code&gt; works exactly like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;val&lt;/code&gt;, except the local variable is not marked as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;final&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;So basically, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Lombok.var&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Lombok.val&lt;/code&gt; can help you clean up your codebase and make it compact without those interruptions with the complex class name.&lt;/p&gt;

&lt;h3 id=&quot;2-data&quot;&gt;2. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@Data&lt;/code&gt;&lt;/h3&gt;

&lt;p&gt;With &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@Data&lt;/code&gt; annotation, we create an example class as follows:&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nd&quot;&gt;@Data&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Person&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;age&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;firstName&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;LastName&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Occupation&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;occupation&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Behind the scene, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@Data&lt;/code&gt; annotation is a set of&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@Getter&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@Setter&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@ToString&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@EqualsAndHashCode&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@RequiredArgsConstructor&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It generates boilerplate for Getter/Setter in Java, and other common methods like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;toString()&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;hashCode()&lt;/code&gt;, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;equals()&lt;/code&gt; in a default way. So if you wanna have the flexibility to implement &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;toString()&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;hashCode()&lt;/code&gt; with your custom logic, just add them to the class definition and they will overwrite what they are in Lombok default boilerplate.&lt;/p&gt;

&lt;p&gt;For example, based on the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Person&lt;/code&gt; class we defined, we can:&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;// initialize a Person object&lt;/span&gt;
&lt;span class=&quot;nc&quot;&gt;Person&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;p&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Person&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;


&lt;span class=&quot;c1&quot;&gt;// Getter&lt;/span&gt;
&lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fullName&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;getFirstName&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot; &quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;getLastName&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// Setter&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;setAge&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;25&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;


&lt;span class=&quot;c1&quot;&gt;// equals()&lt;/span&gt;
&lt;span class=&quot;nc&quot;&gt;Person&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;p2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Person&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;equals&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// do something ..&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;3-builder-and-superbuilder&quot;&gt;3. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@Builder&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@SuperBuilder&lt;/code&gt;&lt;/h3&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@Builder&lt;/code&gt; is the Lombok boilerplate implemenetation of &lt;a href=&quot;https://www.wikiwand.com/en/Builder_pattern&quot;&gt;Builder Pattern&lt;/a&gt;. It is handy when your class has several optional fields, which makes the initialization a little complex.&lt;/p&gt;

&lt;p&gt;With the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@Builder&lt;/code&gt; annotation, we can create an example class as follows&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nd&quot;&gt;@Builder&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Engineer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;yearOfExperience&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;specialization&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Project&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;contributeTo&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Then we can initialize as follows:&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;engineer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Engineer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;builder&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;yearOfExperience&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;specialization&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Civil Engineering&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;build&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;You might notice that the value of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;contributeTo&lt;/code&gt; is not given for the initialization, considering with more optional fields, the Builder Pattern saves a lot of time for object creation (no need for a specific constructor for each case)&lt;/p&gt;

&lt;p&gt;Meanwhile, how does &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@Builder&lt;/code&gt; work with inheritance? - We can use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@SuperBuilder&lt;/code&gt; which is designed for the inheritance use case.&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nd&quot;&gt;@SuperBuilder&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Engineer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;yearOfExperience&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;specialization&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Project&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;contributeTo&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;nd&quot;&gt;@SuperBuilder&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;SoftwareEngineer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;frameworks&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sde&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;SoftwareEngineer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;builder&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;yearOfExperience&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;specialization&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Large-scale Distributed Systems&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;frameworks&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Collections&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;singletonList&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Spring&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;))&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;build&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;data-and-builder&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@Data&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@Builder&lt;/code&gt;&lt;/h4&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nd&quot;&gt;@Data&lt;/span&gt;
&lt;span class=&quot;nd&quot;&gt;@Builder&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;exampleClass&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;attributes&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;exampleMethod&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;exampleClass&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;example&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;exampleClass&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;builder&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;attributes&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;test&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;builder&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
        &lt;span class=&quot;nc&quot;&gt;ObjectMapper&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mapper&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;ObjectMapper&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
        &lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;json&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mapper&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;writeValueAsString&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;exampleClass&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;example2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mapper&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;readValue&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;json&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;exampleClass&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;nc&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;example2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;When you combine the use case of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@Data&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@Builder&lt;/code&gt; like the way above, you will have some exception errors during compilation, the reason behind this is that when only &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@Data&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@Builder&lt;/code&gt; annotations are used in your class. The default constructor is missing, in this case, when you integrate the usage with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Jackson&lt;/code&gt; (which is a serialization/deserialization library in Java and we will cover this next time), the deserialization step will fail because of the missing default constructor.&lt;/p&gt;

&lt;p&gt;To fix it, simply add the two other annotations below:&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nd&quot;&gt;@Data&lt;/span&gt;
&lt;span class=&quot;nd&quot;&gt;@Builder&lt;/span&gt;
&lt;span class=&quot;nd&quot;&gt;@NoArgsConstructor&lt;/span&gt;
&lt;span class=&quot;nd&quot;&gt;@AllArgsConstructor&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;exampleClass&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;attributes&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;exampleMethod&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;exampleClass&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;example&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;exampleClass&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;builder&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;attributes&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;test&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;builder&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
        &lt;span class=&quot;nc&quot;&gt;ObjectMapper&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mapper&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;ObjectMapper&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
        &lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;json&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mapper&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;writeValueAsString&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;exampleClass&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;example2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mapper&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;readValue&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;json&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;exampleClass&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;nc&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;example2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;where each annotation means something like:&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;// @NoArgsConstructor&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;exampleClass&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{}&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// @AllArgsConstructor&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;exampleClass&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;attributes&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;attributes&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;attributes&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;4-nonnull&quot;&gt;4. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@NonNull&lt;/code&gt;&lt;/h3&gt;

&lt;p&gt;With the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@NonNull&lt;/code&gt; annotation, Lombok will generate a null-check for the argument you pass. For example:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;The null-check looks like&lt;/p&gt;

  &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;if (param == null) throw new NullPointerException(&quot;param is marked non-null but is null&quot;);&lt;/code&gt;&lt;/p&gt;

  &lt;p&gt;and will be inserted at the very top of your method.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;exampleMethod&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nd&quot;&gt;@NonNull&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;SoftwareEngineer&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sde&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;nc&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;Println&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sde&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;specialization&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// no need to worry about NPE here&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;5-cleanup&quot;&gt;5. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@Cleanup&lt;/code&gt;&lt;/h3&gt;

&lt;p&gt;With the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@Cleanup&lt;/code&gt; annotation, Lombok will help you clean up the resource use in the context after the reference. For example:&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;CleanupExample&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;throws&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;IOException&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;nd&quot;&gt;@Cleanup&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;InputStream&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;FileInputStream&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]);&lt;/span&gt;
        &lt;span class=&quot;nd&quot;&gt;@Cleanup&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;OutputStream&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;out&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;FileOutputStream&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]);&lt;/span&gt;

        &lt;span class=&quot;kt&quot;&gt;byte&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;byte&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10000&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;];&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;in&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;read&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;break&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;write&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;InputStream&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;OutputStream&lt;/code&gt; created in the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;main()&lt;/code&gt; method will be automatically closed after the reference. So you don’t explicitly do&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;// if else check, then&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;in&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;close&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// if else check, then&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;close&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;6-requiredargsconstructoronconstructor--__inject&quot;&gt;6. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@RequiredArgsConstructor(onConstructor = @__(@Inject))&lt;/code&gt;&lt;/h3&gt;

&lt;p&gt;By default, the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@RequiredArgsConstructor&lt;/code&gt; annotation is contained in the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@Data&lt;/code&gt; annotation, but I wanna highlight another usage of it here, just for its “integration” with &lt;a href=&quot;https://zhenye-na.github.io/blog/2021/12/05/design-patterns-the-singleton-pattern.html&quot;&gt;Singleton Pattern&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(onConstructor = @__(@Inject))&lt;/code&gt;: Lombok will generate &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@Inject&lt;/code&gt; annotation for the constructor&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Another common use case is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@RequiredArgsConstructor(onConstructor = @__(@Autowired))&lt;/code&gt; which means, for the constructor of this class, add the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Autowired&lt;/code&gt; annotation to the constructor&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;junit&quot;&gt;JUnit&lt;/h2&gt;

&lt;blockquote&gt;
  &lt;p&gt;What is Junit?&lt;/p&gt;

  &lt;p&gt;Junit is a Java Testing framework, which provides annotations, assertions, etc to help test the application you are creating.&lt;/p&gt;

  &lt;p&gt;What is Unit Testing?&lt;/p&gt;

  &lt;p&gt;There are different categories of testing, to name a few: Unit Test, Integration Test, Canary Test, Load Test, and Smoke Test&lt;/p&gt;

  &lt;p&gt;Unit Test kind of self-explanatory. It tests against individual modules within the application in an isolated fashion, while Integration Test “enables” those dependencies and checks if it works fine following the flow.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Annotation&lt;/th&gt;
      &lt;th&gt;Description&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@Test&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Denotes that a method is a test method. Unlike JUnit 4’s &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@Test&lt;/code&gt; annotation, this annotation does not declare any attributes, since test extensions in JUnit Jupiter operate based on their dedicated annotations. Such methods are &lt;em&gt;inherited&lt;/em&gt; unless they are &lt;em&gt;overridden&lt;/em&gt;.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@ParameterizedTest&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Denotes that a method is a &lt;a href=&quot;https://junit.org/junit5/docs/current/user-guide/#writing-tests-parameterized-tests&quot;&gt;parameterized test&lt;/a&gt;. Such methods are &lt;em&gt;inherited&lt;/em&gt; unless they are &lt;em&gt;overridden&lt;/em&gt;.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@RepeatedTest&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Denotes that a method is a test template for a &lt;a href=&quot;https://junit.org/junit5/docs/current/user-guide/#writing-tests-repeated-tests&quot;&gt;repeated test&lt;/a&gt;. Such methods are &lt;em&gt;inherited&lt;/em&gt; unless they are &lt;em&gt;overridden&lt;/em&gt;.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@TestFactory&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Denotes that a method is a test factory for &lt;a href=&quot;https://junit.org/junit5/docs/current/user-guide/#writing-tests-dynamic-tests&quot;&gt;dynamic tests&lt;/a&gt;. Such methods are &lt;em&gt;inherited&lt;/em&gt; unless they are &lt;em&gt;overridden&lt;/em&gt;.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@TestInstance&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Used to configure the &lt;a href=&quot;https://junit.org/junit5/docs/current/user-guide/#writing-tests-test-instance-lifecycle&quot;&gt;test instance lifecycle&lt;/a&gt; for the annotated test class. Such annotations are &lt;em&gt;inherited&lt;/em&gt;.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@TestTemplate&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Denotes that a method is a &lt;a href=&quot;https://junit.org/junit5/docs/current/user-guide/#writing-tests-test-templates&quot;&gt;template for test cases&lt;/a&gt; designed to be invoked multiple times depending on the number of invocation contexts returned by the registered &lt;a href=&quot;https://junit.org/junit5/docs/current/user-guide/#extensions-test-templates&quot;&gt;providers&lt;/a&gt;. Such methods are &lt;em&gt;inherited&lt;/em&gt; unless they are &lt;em&gt;overridden&lt;/em&gt;.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@DisplayName&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Declares a custom display name for the test class or test method. Such annotations are not &lt;em&gt;inherited&lt;/em&gt;.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@BeforeEach&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Denotes that the annotated method should be executed &lt;em&gt;before&lt;/em&gt; &lt;strong&gt;each&lt;/strong&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@Test&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@RepeatedTest&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@ParameterizedTest&lt;/code&gt;, or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@TestFactory&lt;/code&gt; method in the current class; analogous to JUnit 4’s &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@Before&lt;/code&gt;. Such methods are &lt;em&gt;inherited&lt;/em&gt; unless they are &lt;em&gt;overridden&lt;/em&gt;.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@AfterEach&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Denotes that the annotated method should be executed &lt;em&gt;after&lt;/em&gt; &lt;strong&gt;each&lt;/strong&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@Test&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@RepeatedTest&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@ParameterizedTest&lt;/code&gt;, or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@TestFactory&lt;/code&gt; method in the current class; analogous to JUnit 4’s &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@After&lt;/code&gt;. Such methods are &lt;em&gt;inherited&lt;/em&gt; unless they are &lt;em&gt;overridden&lt;/em&gt;.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@BeforeAll&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Denotes that the annotated method should be executed &lt;em&gt;before&lt;/em&gt; &lt;strong&gt;all&lt;/strong&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@Test&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@RepeatedTest&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@ParameterizedTest&lt;/code&gt;, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@TestFactory&lt;/code&gt; methods in the current class; analogous to JUnit 4’s &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@BeforeClass&lt;/code&gt;. Such methods are &lt;em&gt;inherited&lt;/em&gt; (unless they are &lt;em&gt;hidden&lt;/em&gt; or &lt;em&gt;overridden&lt;/em&gt;) and must be &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;static&lt;/code&gt; (unless the “per-class” &lt;a href=&quot;https://junit.org/junit5/docs/current/user-guide/#writing-tests-test-instance-lifecycle&quot;&gt;test instance lifecycle&lt;/a&gt; is used).&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@AfterAll&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Denotes that the annotated method should be executed &lt;em&gt;after&lt;/em&gt; &lt;strong&gt;all&lt;/strong&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@Test&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@RepeatedTest&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@ParameterizedTest&lt;/code&gt;, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@TestFactory&lt;/code&gt; methods in the current class; analogous to JUnit 4’s &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@AfterClass&lt;/code&gt;. Such methods are &lt;em&gt;inherited&lt;/em&gt; (unless they are &lt;em&gt;hidden&lt;/em&gt; or &lt;em&gt;overridden&lt;/em&gt;) and must be &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;static&lt;/code&gt; (unless the “per-class” &lt;a href=&quot;https://junit.org/junit5/docs/current/user-guide/#writing-tests-test-instance-lifecycle&quot;&gt;test instance lifecycle&lt;/a&gt; is used).&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@Nested&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Denotes that the annotated class is a nested, non-static test class. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@BeforeAll&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@AfterAll&lt;/code&gt;methods cannot be used directly in a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@Nested&lt;/code&gt; test class unless the “per-class” &lt;a href=&quot;https://junit.org/junit5/docs/current/user-guide/#writing-tests-test-instance-lifecycle&quot;&gt;test instance lifecycle&lt;/a&gt; is used. Such annotations are not &lt;em&gt;inherited&lt;/em&gt;.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@Tag&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Used to declare &lt;em&gt;tags&lt;/em&gt; for filtering tests, either at the class or method level; analogous to test groups in TestNG or Categories in JUnit 4. Such annotations are &lt;em&gt;inherited&lt;/em&gt; at the class level but not at the method level.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@Disabled&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Used to &lt;em&gt;disable&lt;/em&gt; a test class or test method; analogous to JUnit 4’s &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@Ignore&lt;/code&gt;. Such annotations are not &lt;em&gt;inherited&lt;/em&gt;.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@ExtendWith&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Used to register custom &lt;a href=&quot;https://junit.org/junit5/docs/current/user-guide/#extensions&quot;&gt;extensions&lt;/a&gt;. Such annotations are &lt;em&gt;inherited&lt;/em&gt;.&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h3 id=&quot;commonly-used-annotations&quot;&gt;Commonly Used Annotations&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@Test&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@BeforeEach&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@AfterEach&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@Disabled&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;test&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@Test&lt;/code&gt;&lt;/h4&gt;

&lt;p&gt;Define a Test case&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nd&quot;&gt;@Test&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;exampleTest&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;assertTrue&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;beforeeach-and-aftereach&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@BeforeEach&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@AfterEach&lt;/code&gt;&lt;/h4&gt;

&lt;p&gt;These two annotations help if the test cases will share a common object, the methods under these annotations will be invoked every time before/after each test&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;exampleTest&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;SomeClient&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;client&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;AutoCloseable&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;closeable&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;nd&quot;&gt;@BeforeEach&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;setUp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;client&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;SomeClient&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;


    &lt;span class=&quot;nd&quot;&gt;@AfterEach&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;tearDown&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;closeable&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;close&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

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

&lt;h4 id=&quot;disabled&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@Disabled&lt;/code&gt;&lt;/h4&gt;

&lt;p&gt;This annotation is to skip a Test case&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nd&quot;&gt;@Disabled&lt;/span&gt;
&lt;span class=&quot;nd&quot;&gt;@Test&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;disabledTest&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;

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

&lt;h3 id=&quot;assertion-and-assumption&quot;&gt;Assertion and Assumption&lt;/h3&gt;

&lt;p&gt;Both Assertion and Assumption stop when a test fails and moves on to the next test. But the difference is:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;A failed Assertion registered the failed test case, and it means if your code went to production, it will not work&lt;/li&gt;
  &lt;li&gt;A failed Assumption JUST moved to the next test and you don’t know what exactly happened&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
  &lt;p&gt;Java Doc for Assume&lt;/p&gt;

  &lt;p&gt;A set of methods useful for stating assumptions about the conditions in which a test is meaningful. A failed assumption does not mean the code is broken, but that the test provides no useful information.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3 id=&quot;advanced-usage&quot;&gt;Advanced Usage&lt;/h3&gt;

&lt;p&gt;Some advanced use cases are related to specific tests based on the Operating System the Unit Tests are running on or the Java Runtime version it is using, etc. These are helpful if the logic in different OS is different or you have separate logic for Java Runtime.&lt;/p&gt;

&lt;h4 id=&quot;based-on-operating-system&quot;&gt;Based on Operating System&lt;/h4&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nd&quot;&gt;@Test&lt;/span&gt;
&lt;span class=&quot;nd&quot;&gt;@EnabledOnOs&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;({&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;LINUX&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;MAC&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;})&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;enabledOnLinuxOrMac&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;

&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;nd&quot;&gt;@Test&lt;/span&gt;
&lt;span class=&quot;nd&quot;&gt;@DisabledOnOs&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;WINDOWS&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;disabledOnWindows&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;

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

&lt;h4 id=&quot;based-on-java-runtime&quot;&gt;Based on Java Runtime&lt;/h4&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nd&quot;&gt;@Test&lt;/span&gt;
&lt;span class=&quot;nd&quot;&gt;@EnabledOnJre&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;({&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;JAVA_9&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;JAVA_10&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;})&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;enabledOnJava9Or10&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;

&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;nd&quot;&gt;@Test&lt;/span&gt;
&lt;span class=&quot;nd&quot;&gt;@DisabledOnJre&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;JAVA_9&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;disabledOnJava9&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;

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

&lt;h4 id=&quot;based-on-system-property&quot;&gt;Based on System Property&lt;/h4&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nd&quot;&gt;@Test&lt;/span&gt;
&lt;span class=&quot;nd&quot;&gt;@EnabledIfSystemProperty&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;named&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;os.arch&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;matches&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;.*64.*&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;enabledOn64BitArchitectures&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;

&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;nd&quot;&gt;@Test&lt;/span&gt;
&lt;span class=&quot;nd&quot;&gt;@DisabledIfSystemProperty&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;named&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;ci-server&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;matches&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;true&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;disabledOnCiServer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;

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

&lt;hr /&gt;

&lt;h2 id=&quot;mockito&quot;&gt;Mockito&lt;/h2&gt;

&lt;p&gt;Mocktio is a mocking framework for Java Unit Test, the reason why we need it can be explained in this way:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Suppose we have a Class to be unit-tested against, and it has a dependency on the Database or another Client library code (eg RPC), then to mock the target class, we need to “mock” the dependencies and return different kinds of dummy responses for the target class to test the functionality.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;An example Unit Test that utilizes the Mockito could be:&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;ExampleClassTest&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;nd&quot;&gt;@InjectMocks&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;ExampleClass&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;example&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;nd&quot;&gt;@Test&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;simpleTest&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;

        &lt;span class=&quot;c1&quot;&gt;// define depMock object is a `Mock` object of the depdendencyClass&lt;/span&gt;
        &lt;span class=&quot;kd&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;depMock&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mock&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;depdendencyClass&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;

        &lt;span class=&quot;c1&quot;&gt;// define strCaptor object is a `ArgumentCaptor` object of the depdendencyClass&lt;/span&gt;
        &lt;span class=&quot;kd&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;strCaptor&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;ArgumentCaptor&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;forClass&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;

        &lt;span class=&quot;c1&quot;&gt;// mock the behavior of the dependency for a successful invocation&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;when&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;depMock&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;getInvoked&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;any&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;())).&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;thenReturn&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;targetResponse&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;());&lt;/span&gt;

        &lt;span class=&quot;c1&quot;&gt;// what if the dependency throw exception?&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;when&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;depMock&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;getInvoked&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;any&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;())).&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;thenReturn&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dependencyException&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Exception Message&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;));&lt;/span&gt;

        &lt;span class=&quot;c1&quot;&gt;// what if the dependency does not have output (or `void`) ?&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;doNothing&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;when&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;depMock&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;getInvoked&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;any&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;());&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;doThrow&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dependencyException&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Exception Message&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)).&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;when&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;depMock&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;getInvoked&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;any&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;());&lt;/span&gt;

        &lt;span class=&quot;c1&quot;&gt;// invoke the targetClass object and assert behaviors&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;example&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;getInvoked&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt;

        &lt;span class=&quot;c1&quot;&gt;// verify the dependency is actually get invoked&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;verify&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;depMock&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;getInvoked&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;

        &lt;span class=&quot;c1&quot;&gt;// with ArgumentCaptor, you can inspect the parameters that dependency get invoked with&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;verify&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;depMock&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;getInvoked&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;strCaptor&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;capture&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;());&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;assertEquals&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;expectedParam&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;strCaptor&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;getValue&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;())&lt;/span&gt;

        &lt;span class=&quot;c1&quot;&gt;// ...&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;However, Mockito cannot mock:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Final class&lt;/li&gt;
  &lt;li&gt;Primitive type&lt;/li&gt;
  &lt;li&gt;Anonymous class&lt;/li&gt;
  &lt;li&gt;Static method&lt;/li&gt;
  &lt;li&gt;Constructor method&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Mock Final Classes and Methods with Mockito&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now, Mockito 2 support mocking on final types with Mockito extensions&lt;/p&gt;

&lt;p&gt;In your project, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;src/test/resources/mockito-extensions&lt;/code&gt; directory, create a file with name &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;org.mockito.plugins.MockMaker&lt;/code&gt;, with the following content&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;mock-maker-inline
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The extension get loaded when Mockito is instantiated. For more, you can check https://site.mockito.org/javadoc/current/org/mockito/plugins/MockMaker.html&lt;/p&gt;

&lt;h3 id=&quot;powermock&quot;&gt;PowerMock&lt;/h3&gt;

&lt;p&gt;Based on the limitations above, &lt;a href=&quot;https://github.com/powermock/powermock&quot;&gt;PowerMock&lt;/a&gt; is the one framework that comes to save your life. It also provides an extension for Mockito API, &lt;a href=&quot;https://www.javadoc.io/doc/org.powermock/powermock-api-mockito/1.7.0/index.html&quot;&gt;(PowerMockito)&lt;/a&gt; which can be easily integrated with Mockito for Unit Testing.&lt;/p&gt;

&lt;p&gt;To make your test case run-able with PowerMockito, we need the following configuration beforehand&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nd&quot;&gt;@RunWith&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;PowerMockRunner&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;nd&quot;&gt;@PrepareForTest&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fullyQualifiedNames&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;ExampleFinalClass.class&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;exampleTest&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;

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

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@RunWith&lt;/code&gt;: required if using JUnit 4.x or the following code block if using Junit 3.x&lt;/p&gt;

    &lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;  &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;TestSuite&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;suite&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;throws&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Exception&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;PowerMockSuite&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;MyTestCase&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@PrepareForTest&lt;/code&gt;: This annotation tells PowerMock to prepare a certain class for testing. Typically, final classes, classes with final, static methods, which need to be byte-code manipulated&lt;/p&gt;
    &lt;ul&gt;
      &lt;li&gt;This annotation can be placed on both class and individual test methods. If placed on class means: all test methods will be handled by PowerMock&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Sample class definition we will use for illustration:&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;CollaboratorWithFinalAndStaticMethods&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;helloMethod&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Hello World!&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;firstMethod&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Hello &quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot; !&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;secondMethod&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Hello no one!&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;thirdMethod&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Hello no one again!&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;mocking-constructors&quot;&gt;Mocking Constructors&lt;/h4&gt;

&lt;p&gt;First, we create a mock object using the PowerMockito API:&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nc&quot;&gt;CollaboratorWithFinalAndStaticMethods&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mock&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mock&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;CollaboratorWithFinalAndStaticMethods&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Next, set an expectation telling that whenever the no-arg constructor of that class is invoked, a mock instance should be returned rather than a real one:&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;whenNew&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;CollaboratorWithFinalAndStaticMethods&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;withNoArguments&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;thenReturn&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mock&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Let’s see how this construction mocking works in action by instantiating the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CollaboratorWithFinalMethods&lt;/code&gt; class using its default constructor, and then verify the behaviors of PowerMock:&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nc&quot;&gt;CollaboratorWithFinalAndStaticMethods&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;collaborator&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;CollaboratorWithFinalAndStaticMethods&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;verifyNew&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;CollaboratorWithFinalAndStaticMethods&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;withNoArguments&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;mocking-final-methods&quot;&gt;Mocking Final Methods&lt;/h4&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;when&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;collaborator&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;helloMethod&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()).&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;thenReturn&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Hello World!&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This method is then executed:&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;welcome&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;collaborator&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;helloMethod&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The following assertions confirm that the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;helloMethod&lt;/code&gt; method has been called on the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;collaborator&lt;/code&gt; object, and returns the value set by the mocking expectation:&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nc&quot;&gt;Mockito&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;verify&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;collaborator&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;helloMethod&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;assertEquals&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Hello World!&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;welcome&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;mocking-static-methods&quot;&gt;Mocking Static Methods&lt;/h4&gt;

&lt;p&gt;Mock static method&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;mockStatic&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;CollaboratorWithFinalAndStaticMethods&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Define the output value&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;when&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;CollaboratorWithFinalAndStaticMethods&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;firstMethod&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Mockito&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;anyString&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;())).&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;thenReturn&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Hello World!&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;when&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;CollaboratorWithFinalAndStaticMethods&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;secondMethod&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()).&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;thenReturn&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Nothing special&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Or throw an exception&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;doThrow&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;RuntimeException&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()).&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;when&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;CollaboratorWithFinalAndStaticMethods&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;nc&quot;&gt;CollaboratorWithFinalAndStaticMethods&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;thirdMethod&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;firstWelcome&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;CollaboratorWithFinalAndStaticMethods&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;firstMethod&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Whoever&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;secondWelcome&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;CollaboratorWithFinalAndStaticMethods&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;firstMethod&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Whatever&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;


&lt;span class=&quot;n&quot;&gt;assertEquals&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Hello World!&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;firstWelcome&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;assertEquals&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Hello World!&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;secondWelcome&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Verify the behavior of the mock’s method&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;verifyStatic&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Mockito&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;times&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;));&lt;/span&gt;
&lt;span class=&quot;nc&quot;&gt;CollaboratorWithStaticMethods&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;firstMethod&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Mockito&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;anyString&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;());&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;verifyStatic&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Mockito&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;never&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;());&lt;/span&gt;
&lt;span class=&quot;nc&quot;&gt;CollaboratorWithStaticMethods&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;secondMethod&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p class=&quot;info&quot;&gt;Note: The verifyStatic method must be called right before any static method verification for PowerMockito to know that the successive method invocation is what needs to be verified.&lt;/p&gt;

&lt;h4 id=&quot;partial-mocking&quot;&gt;Partial Mocking&lt;/h4&gt;

&lt;p&gt;Partial mocks allow you to mock some of the methods of a class while keeping the rest intact. Thus, you keep your original object, not a mock object, and you are still able to write your test methods in isolation.&lt;/p&gt;

&lt;p&gt;Given this example Class:&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;CustomerService&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Customer&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;customer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;someCondition&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;subscribeToNewsletter&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;customer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;subscribeToNewsletter&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Customer&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;customer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;// ...subscribing stuff&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;So you want to test the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;add()&lt;/code&gt; method for actually invoking &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;subscribeToNewsletter()&lt;/code&gt; and do &lt;strong&gt;NOT&lt;/strong&gt; want to execute the logic from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;subscribeToNewsletter()&lt;/code&gt; in this test – e.g. since you’ve already unit tested &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;subscribeToNewsletter()&lt;/code&gt; somewhere else.&lt;/p&gt;

&lt;p&gt;Then you create a &lt;strong&gt;PARTIAL&lt;/strong&gt; mock of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CustomerService&lt;/code&gt;, giving a list of methods you want to mock.&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nc&quot;&gt;CustomerService&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;customerService&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;PowerMock&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;createPartialMock&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;CustomerService&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;subscribeToNewsletter&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;customerService&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;subscribeToNewsletter&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;anyObject&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Customer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;));&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;replayAll&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;customerService&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;createMock&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Customer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Note:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Partial Mock only works with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PUBLIC&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DEFAULT&lt;/code&gt; methods&lt;/li&gt;
  &lt;li&gt;Using method name could potentially break your test cases sooner or later if the method name is changed&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;reference&quot;&gt;Reference&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://projectlombok.org/features/all&quot;&gt;Project Lombok - Features&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.vogella.com/tutorials/Mockito/article.html&quot;&gt;Unit tests with Mockito - Tutorial&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://stackoverflow.com/questions/44628483/assume-vs-assert-in-junit-tests&quot;&gt;Assume vs assert in JUnit tests&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.baeldung.com/intro-to-powermock&quot;&gt;Introduction to PowerMock&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.telerik.com/devtools/justmock/advanced-usage/partial-mocking#:~:text=Partial%20mocks%20allow%20you%20to,both%20static%20and%20instance%20calls.&quot;&gt;Partial Mocking&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content><author><name>Zhenye Na</name><email>nazhenye@gmail.com</email></author><category term="Java" /><summary type="html">How to properly use Lombok, Junit and Mockito in Java</summary></entry><entry><title type="html">Implementing Microservice Communication</title><link href="/2022/04/03/implementing-microservice-communication.html" rel="alternate" type="text/html" title="Implementing Microservice Communication" /><published>2022-04-03T00:00:00-07:00</published><updated>2022-04-03T00:00:00-07:00</updated><id>/2022/04/03/implementing-microservice-communication</id><content type="html" xml:base="/2022/04/03/implementing-microservice-communication.html">&lt;blockquote&gt;
  &lt;p&gt;This article is a follow-up from my previous post: &lt;a href=&quot;https://zhenye-na.github.io/blog/2022/03/05/microservice-communication-patterns.html&quot;&gt;Microservice Communication Patterns&lt;/a&gt;&lt;/p&gt;

  &lt;p&gt;Feel free to check that out first then come back to read the article.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2 id=&quot;technology-choices&quot;&gt;Technology Choices&lt;/h2&gt;

&lt;p&gt;From the previous article, we know that there are two typical patterns for inter-microservices communication:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Synchronous blocking&lt;/li&gt;
  &lt;li&gt;Asynchronous unblocking&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In order to implement, there are several commonly used technology choices to select:&lt;/p&gt;

&lt;div align=&quot;center&quot;&gt;
  &lt;img src=&quot;https://content.altexsoft.com/media/2020/05/word-image-52.png&quot; width=&quot;80%&quot; /&gt;
  &lt;p&gt;Image source: &lt;a href=&quot;https://www.altexsoft.com/blog/soap-vs-rest-vs-graphql-vs-rpc/&quot;&gt;https://www.altexsoft.com/blog/soap-vs-rest-vs-graphql-vs-rpc/&lt;/a&gt;&lt;/p&gt;
&lt;/div&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Remote Procedure Calls (RPC)&lt;/strong&gt;
    &lt;ol&gt;
      &lt;li&gt;Local method invocation to a remote process/service.&lt;/li&gt;
      &lt;li&gt;Common framework options for RPC development: &lt;a href=&quot;https://grpc.io/&quot;&gt;gRPC&lt;/a&gt;, &lt;a href=&quot;https://awslabs.github.io/smithy/&quot;&gt;Smithy&lt;/a&gt;&lt;/li&gt;
    &lt;/ol&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;REST&lt;/strong&gt;
    &lt;ol&gt;
      &lt;li&gt;RESTful API with Http verbs&lt;/li&gt;
    &lt;/ol&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;GraphQL&lt;/strong&gt;
    &lt;ol&gt;
      &lt;li&gt;Define custom queries that can fetch information from multiple downstream services and aggregate/filter out the needed information
        &lt;ol&gt;
          &lt;li&gt;Github is utilizing GraphQL, see &lt;a href=&quot;https://docs.github.com/en/graphql&quot;&gt;ref&lt;/a&gt;&lt;/li&gt;
        &lt;/ol&gt;
      &lt;/li&gt;
    &lt;/ol&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Message brokers&lt;/strong&gt;
    &lt;ol&gt;
      &lt;li&gt;Message queue/middleware
        &lt;ol&gt;
          &lt;li&gt;This could be open source tool like Kafka, RabbitMQ, or AWS provided tools like SNS Topic and SQS Queue&lt;/li&gt;
        &lt;/ol&gt;
      &lt;/li&gt;
    &lt;/ol&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;div align=&quot;center&quot;&gt;
  &lt;img src=&quot;https://content.altexsoft.com/media/2020/05/word-image-53.png&quot; width=&quot;80%&quot; /&gt;
  &lt;p&gt;Image source: &lt;a href=&quot;https://www.altexsoft.com/blog/soap-vs-rest-vs-graphql-vs-rpc/&quot;&gt;https://www.altexsoft.com/blog/soap-vs-rest-vs-graphql-vs-rpc/&lt;/a&gt;&lt;/p&gt;
&lt;/div&gt;

&lt;h3 id=&quot;remote-procedure-calls&quot;&gt;Remote Procedure Calls&lt;/h3&gt;

&lt;p&gt;For protocols that fall into RPC land, most of them require the specific schema. Check the image below&lt;/p&gt;

&lt;p&gt;The schema that referred to in the context of RPC is called &lt;strong&gt;Interface Definition Language&lt;/strong&gt; (IDL), (with SOAP, it is called Web Service Definition Language (WSDL)). The usage of this schema is that it make easier for developers to generate the client and server stubs for different tech stack. This behavior solves the problem that if you have an exposed RPC Java server, then you can utilize the feature provided by different IDL frameworks to generate a Golang client from the schema you defined. For example, the following is an example from &lt;a href=&quot;https://grpc.io/&quot;&gt;gRPC&lt;/a&gt;&lt;/p&gt;

&lt;div class=&quot;language-go highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;// The greeting service definition.&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;service&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Greeter&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;c&quot;&gt;// Sends a greeting&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;rpc&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SayHello&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;HelloRequest&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;returns&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;HelloReply&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;// The request message containing the user's name.&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;message&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;HelloRequest&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;// The response message containing the greetings&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;message&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;HelloReply&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;message&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Once you defined the input/output or request/response schema for your API, you can easily generate the cleint stubs based on the tech stack you would like to provide, for people to use.&lt;/p&gt;

&lt;h4 id=&quot;pros-and-cons&quot;&gt;Pros and Cons&lt;/h4&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Pros&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;Abstraction. It provides the abstraction, since the invocation to your service or your library is just like a “inter-process” call, but acutally it is executed by a different process on difference server&lt;/li&gt;
      &lt;li&gt;Simplification. Client-code generation can be easily done with no extra effort&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Cons&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;Remote call is expensive. With RPC, the cost of marshaling and unmarshaling input/output payloads could be very significant&lt;/li&gt;
      &lt;li&gt;Vulnerable. Networks are always not reliable&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;rest&quot;&gt;REST&lt;/h3&gt;

&lt;p&gt;REST is all about “resources”. You might hear of this multiple times, but what is a resource in REST?&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;In my opinion, every object that we can obtain information from, like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;entity&lt;/code&gt; in database is a “resource” in REST&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;# create a post
POST /posts

# obtain information of a post
GET /posts/&amp;lt;id&amp;gt;

# obtain tagging information of a post
GET /posts/&amp;lt;id&amp;gt;/tags
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;To know more, you can check &lt;a href=&quot;https://restfulapi.net/&quot;&gt;What is REST&lt;/a&gt; by Lokesh Gupta.&lt;/p&gt;

&lt;h3 id=&quot;graphql&quot;&gt;GraphQL&lt;/h3&gt;

&lt;p&gt;GraphQL is a new technology in recent years. It helps user to make a query that avoid the need to make &lt;em&gt;multiple&lt;/em&gt; requests to retrieve the &lt;em&gt;same information&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;For example, suppose &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Amazon.com&lt;/code&gt; would like to migrate to use GraphQL, and in the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Orders&lt;/code&gt; page. It will show your recent products you ordered, together with shipping information. Suppose &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Amazon.com&lt;/code&gt; adopts the microservice architecture, then:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;In order to render the view, backend system need to query different component to retrieve the needed information
    &lt;ol&gt;
      &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Shipping&lt;/code&gt; service to check what is the status of this order&lt;/li&gt;
      &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Payment&lt;/code&gt; service to check if this order is completed, if not we might need a button to resume payment&lt;/li&gt;
    &lt;/ol&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;But with GraphQL, this is perfectly solved because we can use GraphQL to send out a single request that query the needed information, as aggregated query is also supported in GraphQL&lt;/p&gt;

&lt;h3 id=&quot;message-brokers&quot;&gt;Message brokers&lt;/h3&gt;

&lt;p&gt;There are two categories of Message brokers:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Queues
    &lt;ol&gt;
      &lt;li&gt;Queue is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1:1&lt;/code&gt; relationship&lt;/li&gt;
      &lt;li&gt;A producer puts the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;message&lt;/code&gt; into a queue, then the consumer reads the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;message&lt;/code&gt; from it&lt;/li&gt;
    &lt;/ol&gt;
  &lt;/li&gt;
  &lt;li&gt;Topics
    &lt;ol&gt;
      &lt;li&gt;Topic is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1:Many&lt;/code&gt; relationship&lt;/li&gt;
      &lt;li&gt;A producer puts the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;message&lt;/code&gt; into a topic, the consumer group which subsribes to the topic will receive a copy of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;message&lt;/code&gt;&lt;/li&gt;
    &lt;/ol&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Topics work well in a event-based system while queue work well in a request/response system&lt;/p&gt;

&lt;h3 id=&quot;asynchronous-in-aws&quot;&gt;Asynchronous in AWS&lt;/h3&gt;

&lt;p&gt;AWS offers solutions for almost each communication pattern metioned above:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Message brokers
    &lt;ul&gt;
      &lt;li&gt;Topic-based: SNS topics&lt;/li&gt;
      &lt;li&gt;Queue-based: SQS Queue&lt;/li&gt;
      &lt;li&gt;MSK: Amazon Managed Streaming for Apache Kafka&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Event-based
    &lt;ul&gt;
      &lt;li&gt;EventBridge&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Generally asychronous
    &lt;ul&gt;
      &lt;li&gt;Async Step Functions workflow&lt;/li&gt;
      &lt;li&gt;Async Lambda invocation&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;</content><author><name>Zhenye Na</name><email>nazhenye@gmail.com</email></author><category term="Microservice" /><summary type="html">Implementing Microservice Communication effectively</summary></entry><entry><title type="html">What you don’t know about AWS API Gateway</title><link href="/2022/03/13/what-you-dont-know-about-aws-api-gateway.html" rel="alternate" type="text/html" title="What you don’t know about AWS API Gateway" /><published>2022-03-13T00:00:00-08:00</published><updated>2022-03-13T00:00:00-08:00</updated><id>/2022/03/13/what-you-dont-know-about-aws-api-gateway</id><content type="html" xml:base="/2022/03/13/what-you-dont-know-about-aws-api-gateway.html">&lt;h2 id=&quot;access-control-on-api-gateway&quot;&gt;Access Control on API Gateway&lt;/h2&gt;

&lt;p&gt;By default, API Gateway will accept all requests to your services. This means there will be a lot of malicious requests/attacks to your service and API Gateway is the role to “filter out” those and secure your infrastructure.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Zero Trust Model&lt;/p&gt;

  &lt;p&gt;Zero Trust Model means that all edge services does not trust/approve incoming requests, until it can prove/establish trust in the backend system&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;API Gateway supports Zero Trust Model by using “Authorizers”. They are considered as an “Interceptor” which check each request’s identification. API Gateway applies “Facade Design Pattern” to the authorization implementation so that the authorization logic can be abstracted away.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Not familiar wiht Facade Pattern? Check what I wrote before: &lt;a href=&quot;https://zhenye-na.github.io/blog/2021/12/09/design-patterns-the-facade-pattern.html&quot;&gt;Design Patterns: Facade Pattern&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href=&quot;https://mermaid.live/edit#pako:eNqNkU1PAjEQhv_KpKclARNIuOzBhEU0JB4M6q2X2g5s4267tlPXlfDfnWVBiCfn0mbyPu987YX2BkUutpVvdakCweNGOumAI6a3XVBNCfdKK4NDsg9jA2qy3kHxcskWmRSLpzU8KMJWdVK6RaIy-G-LATb4kTASrB1h0NiQDyzIBueRFKOLj532RlpjjKC9o-ArqPzOagZs3VRYoyN1LD9lECY3k1sorvjZv_nZXx6dOU-_yFbOwGvEwBJWDK0ev8vsak5gWeOto9EvyZZ9FhLD0CpHEciDdZ_-HYFKhB7vqy4H4LRu6iqEgt_AuklrDZX5rPkanxJGRT5QUF0Oc5iLsagx1Moavt6-d5CCrWuUIuevwa1KFUkh3YGlqTHc7cpY3rvIt6qKOBYqkX_unBY5hYRn0Z1VfPX6pDr8AJH9ruw&quot;&gt;&lt;img src=&quot;https://mermaid.ink/img/pako:eNqNkU1PAjEQhv_KpKclARNIuOzBhEU0JB4M6q2X2g5s4267tlPXlfDfnWVBiCfn0mbyPu987YX2BkUutpVvdakCweNGOumAI6a3XVBNCfdKK4NDsg9jA2qy3kHxcskWmRSLpzU8KMJWdVK6RaIy-G-LATb4kTASrB1h0NiQDyzIBueRFKOLj532RlpjjKC9o-ArqPzOagZs3VRYoyN1LD9lECY3k1sorvjZv_nZXx6dOU-_yFbOwGvEwBJWDK0ev8vsak5gWeOto9EvyZZ9FhLD0CpHEciDdZ_-HYFKhB7vqy4H4LRu6iqEgt_AuklrDZX5rPkanxJGRT5QUF0Oc5iLsagx1Moavt6-d5CCrWuUIuevwa1KFUkh3YGlqTHc7cpY3rvIt6qKOBYqkX_unBY5hYRn0Z1VfPX6pDr8AJH9ruw&quot; alt=&quot;&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There are three type of autorization methods&lt;/p&gt;

&lt;h3 id=&quot;iam-autorizer&quot;&gt;IAM Autorizer&lt;/h3&gt;

&lt;p&gt;The most intuitive way to authorize the request, by signing your request using SigV4 of your AWS credentials. API Gateway will go to IAm and verify the request identity and decide if let the request pass in or not.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://raw.githubusercontent.com/Zhenye-Na/img-hosting-picgo/master/img/api-gateway-iam-authorizer.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;h3 id=&quot;aws-cognito&quot;&gt;AWS Cognito&lt;/h3&gt;

&lt;p&gt;In order to use Cognito for authorization, you must create a cognito user pool. Then you can use the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Authorizer&lt;/code&gt; tab in API Gateway Console to select &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Cognito&lt;/code&gt; as the authorization method&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;AWS Cognito return a JWT (JSON Web Token)&lt;/li&gt;
  &lt;li&gt;User makes request to API Gateway with the JWT&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;customed-lambda-authorizer&quot;&gt;Customed Lambda Authorizer&lt;/h3&gt;

&lt;p&gt;If you or your oganization already has the implementation of the Authorization, you can leverage Lambda (AWS Serverless Function) as the authorization implementation of your APi Gateway.&lt;/p&gt;

&lt;p&gt;Lambda authorizer requires identity information coming from request, so&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;JWT that passed along in the request&lt;/li&gt;
  &lt;li&gt;Request header or request body contains the identity information&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;infrastructure-security-on-api-gateway&quot;&gt;Infrastructure Security on API Gateway&lt;/h2&gt;

&lt;h3 id=&quot;rate-limitimg&quot;&gt;Rate Limitimg&lt;/h3&gt;

&lt;blockquote&gt;
  &lt;p&gt;What is rate limiting and why we need it?&lt;/p&gt;

  &lt;p&gt;What Rate Limiting means on Wiki, &lt;a href=&quot;https://www.wikiwand.com/en/Rate_limiting&quot;&gt;here&lt;/a&gt;&lt;/p&gt;

  &lt;p&gt;In my opinion, it is a term that means that we “limit” the rate that this service can be accessed within a time unit.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Since API Gateway could serve as publicly facing, we should be aware of that our service could be suffered from a wider DDoS attack, which means a lot of traffic comes into your service and then if your server is not able to handle such many traffic, then it will “down”.&lt;/p&gt;

&lt;p&gt;In order to prevent this to happen, API Gateway gives us the ability to “limit” the access to your service to two important parameters:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Sustained request rate: average rate number which your application gets request over a time unit&lt;/li&gt;
  &lt;li&gt;Burst request rate: same meaning as itself, this is a buffer which helps your application in case there is a large amount of requests comes in (request spikes), over a time unit&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Token Bucket Algorithm is used by API Gateway team under the hood, to read more, &lt;a href=&quot;https://www.wikiwand.com/en/Token_bucket&quot;&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you want your Rate Limiting to be more fine-grained, there is also a stage-level or method-level rate limiting that API Gateway supports&lt;/p&gt;

&lt;h3 id=&quot;mutual-tls&quot;&gt;Mutual TLS&lt;/h3&gt;

&lt;blockquote&gt;
  &lt;p&gt;This topic will be covered in more details in the upcoming post.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;TLS helps validate the server uysing a standardized handshake and digital certificates signed by trusted certificate authorities for each domain.&lt;/p&gt;

&lt;p&gt;mTLS asks each client to authenticate every request they sent in to increase the security level&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;mTLS is commonly used for B2B applications&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2 id=&quot;binary-files-support&quot;&gt;Binary Files support&lt;/h2&gt;

&lt;p&gt;API Gateway also supports Binary Media files support, but you have to enable the configuration and re-deploy the gateway to make the change take effect.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Binary Files as Input
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-payload-encodings-configure-with-console.html&quot;&gt;Enabling binary support using the API Gateway console&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;https://docs.aws.amazon.com/cdk/api/v1/docs/@aws-cdk_aws-apigateway.LambdaRestApiProps.html#binarymediatypes&quot;&gt;Binary Media Types in CDK&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Binary Files as Output
    &lt;ul&gt;
      &lt;li&gt;You should return a Base64 encoded String of the Binary files you wanna return and set the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;isBase64Encoded&lt;/code&gt; to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;true&lt;/code&gt; from the API output&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;https://aws.amazon.com/blogs/compute/handling-binary-data-using-amazon-api-gateway-http-apis/&quot;&gt;Handling binary data using Amazon API Gateway HTTP APIs&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;references&quot;&gt;References&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.oreilly.com/library/view/security-and-microservice/9781098101459/&quot;&gt;Security and Microservice Architecture on AWS&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-request-throttling.html&quot;&gt;Throttle API requests for better throughput&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://intronetworks.cs.luc.edu/current/html/tokenbucket.html&quot;&gt;Token Bucket Rate Limiting&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content><author><name>Zhenye Na</name><email>nazhenye@gmail.com</email></author><category term="AWS" /><category term="API Gateway" /><summary type="html">Secure your microservices with AWS API Gateway for your Public-facing APIs</summary></entry><entry><title type="html">Microservice Communication Patterns</title><link href="/2022/03/05/microservice-communication-patterns.html" rel="alternate" type="text/html" title="Microservice Communication Patterns" /><published>2022-03-05T00:00:00-08:00</published><updated>2022-03-05T00:00:00-08:00</updated><id>/2022/03/05/microservice-communication-patterns</id><content type="html" xml:base="/2022/03/05/microservice-communication-patterns.html">&lt;p&gt;&lt;img src=&quot;https://raw.githubusercontent.com/Zhenye-Na/img-hosting-picgo/master/img/service-mesh-illustration.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Image source: https://us.nttdata.com/en/blog/tech-blog/service-mesh-use-cases&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In the image above, we can see&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;API Gateway: external to internal microservices&lt;/li&gt;
  &lt;li&gt;Service Mesh: inter-internal microservices&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Today, we are focusing on different patterns/style of communication on “Service Mesh” level&lt;/p&gt;

&lt;p&gt;There are several ways for inter-microservice communication.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Synchronous Blocking
    &lt;ul&gt;
      &lt;li&gt;serviceA calls serviceB and blocks upcoming operation and waits for the results&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Asynchronous Non-blocking
    &lt;ul&gt;
      &lt;li&gt;Request-response: serviceA sends a request to serviceB and wait for the response&lt;/li&gt;
      &lt;li&gt;Event-driven: serviceA emits out a “event” with context, but serviceA is unaware of which service will pick up the event and processing&lt;/li&gt;
      &lt;li&gt;Common data: communicate with a shared data, like a shared database&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It is hard to select the best one for your business logic, sometimes you might even mix and match. Next, let’s dive deep for each pattern and let’s see the pros and cons of each pattern.&lt;/p&gt;

&lt;h2 id=&quot;synchronous-blocking&quot;&gt;Synchronous Blocking&lt;/h2&gt;

&lt;p&gt;A microservice send an API call/request to downstream service and blocks until the call has complete processing.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Pros&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;easy to understand, like reading codes from top to bottom&lt;/li&gt;
  &lt;li&gt;monolithic way&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Cons&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;if the downstream service is temporarily unreachable, the API call will fail
    &lt;ol&gt;
      &lt;li&gt;same idea, if the upstream service is unable to receive the response&lt;/li&gt;
    &lt;/ol&gt;
  &lt;/li&gt;
  &lt;li&gt;latency issue when invocation chain is long (serviceA -&amp;gt; serviceB -&amp;gt; serviceC -&amp;gt; serviceD)&lt;/li&gt;
&lt;/ol&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;asychronous-non-blocking&quot;&gt;Asychronous Non-blocking&lt;/h2&gt;

&lt;h3 id=&quot;request-response-pattern&quot;&gt;Request-response Pattern&lt;/h3&gt;

&lt;p&gt;By term of request-response you might think this is similar to the synchronous blocking pattern. I would not deny since theoratically there is some similarity, but when you try to implement, you will find out the difference dramatically.&lt;/p&gt;

&lt;p&gt;With this pattern, microservice sends a request to the downtream service to process something, and expects to receive a response that contains the processing results, in a async way, like message broker or message queue&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Pros&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;If serviceA need several downstream services to finish this task, we can parallelized all the requests if there is no dependency for each downstream service&lt;/li&gt;
  &lt;li&gt;good for retry&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Cons&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;We need a better way to define the “expiration time” or time-out handling, to avoid issues that the system is blocked and may never get back to the original caller&lt;/li&gt;
&lt;/ol&gt;

&lt;hr /&gt;

&lt;h3 id=&quot;event-driven-pattern&quot;&gt;Event-Driven Pattern&lt;/h3&gt;

&lt;blockquote&gt;
  &lt;p&gt;When it comes to event-driven the first thing I think about is Node.js&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;With Event-Driven pattern, the microservice emits an event and it may or may not be received by downstream service. Also, the event emitter is unaware of which service will pick up the event, it just emitted the event. In this way, the coupling is greatly reduced since we want “High cohesion and low coupling”.&lt;/p&gt;

&lt;h4 id=&quot;what-should-be-in-the-event-&quot;&gt;What should be in the event ?&lt;/h4&gt;

&lt;p&gt;It should be a fully detailed events contains all the information that downstream service need to know, otherwise, the downstream service have to make an API call to the event emitter to request some context data, then what is the point of this pattern ?&lt;/p&gt;

&lt;p&gt;For example, from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Order&lt;/code&gt; service to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Shipping&lt;/code&gt; service, what kind of information should we include in the event? just a orderId and customerId? of course not, we should include something like, shipping address, what is the ETA for delievery (shipping priority) etc. So that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Shipping&lt;/code&gt; can continue processing the order.&lt;/p&gt;

&lt;hr /&gt;

&lt;h3 id=&quot;common-data-pattern&quot;&gt;Common Data Pattern&lt;/h3&gt;

&lt;p&gt;This is pattern is called common data pattern, basically you will see this pattern when using data lake or data warehouse. In both case, the same record can be retrieved and updated in two or more microservices.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Pros&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;very easy to understand and implement&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Cons&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;latency: the polling mechanism is the problem&lt;/li&gt;
  &lt;li&gt;common data store become part of the comupling, one the structure is defined, cannot be changed easily&lt;/li&gt;
  &lt;li&gt;we need a rule on which service can alter what kind of data&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;references&quot;&gt;References&lt;/h2&gt;

&lt;p&gt;[1]. &lt;a href=&quot;https://www.oreilly.com/library/view/building-microservices-2nd/9781492034018/&quot;&gt;Building Microservices, 2nd Edition&lt;/a&gt;&lt;/p&gt;</content><author><name>Zhenye Na</name><email>nazhenye@gmail.com</email></author><category term="Microservice" /><summary type="html">Different patterns for microservices to communicate</summary></entry><entry><title type="html">AWS DynamoDB Best Practices</title><link href="/2022/02/13/effective-dynamodb.html" rel="alternate" type="text/html" title="AWS DynamoDB Best Practices" /><published>2022-02-13T00:00:00-08:00</published><updated>2022-02-13T00:00:00-08:00</updated><id>/2022/02/13/effective-dynamodb</id><content type="html" xml:base="/2022/02/13/effective-dynamodb.html">&lt;h2 id=&quot;dynamodb&quot;&gt;DynamoDB&lt;/h2&gt;

&lt;p&gt;Just a quick overview, what is DynamoDB ?&lt;/p&gt;

&lt;p class=&quot;info&quot;&gt;DynamoDB is a fully managed &lt;strong&gt;NoSQL database&lt;/strong&gt; (key-value pair and document) service provided by AWS that offers fast and predictable performance with seamless scalability&lt;/p&gt;

&lt;h2 id=&quot;item-1-prefer-query-to-scan&quot;&gt;Item 1: Prefer Query to Scan&lt;/h2&gt;

&lt;p&gt;First, let’s check the definition of scan and query operation in DynamoDB&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Scan&lt;/code&gt; operation always scans the &lt;strong&gt;entire table or secondary index&lt;/strong&gt;. It then filters out values to provide the result you want.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Query&lt;/code&gt; operation searches &lt;strong&gt;only primary key attribute values&lt;/strong&gt; and supports a subset of comparison operators on key attribute values to refine the search process.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;From here, we can easily tell that Scan operation is VERY slow/inefficient and resource consuming (since you need RCU to read all the items in the DDB). So you should design you DDB structure so that query operaion can fit almost all the case of your backend. In case it does not, you can always update your DDB design&lt;/p&gt;

&lt;p&gt;What you can do:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Utilize Partition Key and Sort Key for the most common request pattern&lt;/li&gt;
  &lt;li&gt;Add LSI or GSI to accomodate advanced query patterns. BUT only add GSI/LSI when needed, since this consume RCU/WCU&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;item-2-secondary-indexes&quot;&gt;Item 2: Secondary Indexes&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Rule 1: Keep the number of indexes to a minimum&lt;/li&gt;
  &lt;li&gt;Rule 2: If you have to use an Index, choose wisely&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There are two types of secondary index in DynamoDB:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Global secondary index (GSI)&lt;/strong&gt;: An index with a &lt;strong&gt;partition key&lt;/strong&gt; and a &lt;strong&gt;sort key&lt;/strong&gt; that can be different from those on the base table.
    &lt;ul&gt;
      &lt;li&gt;A global secondary index is considered “global” because queries on the index can span all of the data in the base table, across all partitions.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Local secondary index (LSI)&lt;/strong&gt;: An index that has the &lt;strong&gt;same partition key&lt;/strong&gt; as the base table, but a &lt;strong&gt;different sort key&lt;/strong&gt;.
    &lt;ul&gt;
      &lt;li&gt;A local secondary index is “local” in the sense that every partition of a local secondary index is scoped to a base table partition that has the same partition key value.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So the basic difference is the “scope” of each index is different. GSI has the global view while LSI has view only those data share the same partition key&lt;/p&gt;

&lt;p&gt;When you use these two types of indexes, there is also a difference when you try to retrieve data back&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;LSI supports Strongly Consistency and Eventual Consistency
    &lt;ol&gt;
      &lt;li&gt;which will always return the latest version of records if you can enable this by changing the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ConsistentRead&lt;/code&gt; property when sending out a query&lt;/li&gt;
    &lt;/ol&gt;
  &lt;/li&gt;
  &lt;li&gt;GSI supports Eventual Consistency
    &lt;ol&gt;
      &lt;li&gt;which might return a stale version of records, and you dont have the option to enable strong consistency&lt;/li&gt;
    &lt;/ol&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;So, how can I choose the correct DDB index? I found this flow chart very helpful:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://user-images.githubusercontent.com/6509926/72526710-a66b7c80-382c-11ea-8923-dbb9c9589881.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Image source: https://www.dynamodbguide.com/local-or-global-choosing-a-secondary-index-type-in-dynamo-db/#the-too-long-didnt-read-version-of-choosing-an-index&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2 id=&quot;item-3-savebehavior-configuration&quot;&gt;Item 3: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SaveBehavior&lt;/code&gt; Configuration&lt;/h2&gt;

&lt;p&gt;When you use DynamoDB for CRUD operations, you will find out that DynamoDB does not have the actual “Update” concept. However, there is a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SaveBehavior&lt;/code&gt; configuration which serves as the missed role in DynamoDB. Let’s take a look.&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nc&quot;&gt;DynamoDBMapper&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mapper&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;DynamoDBMapper&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dynamoDBClient&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// SaveBehavior.UPDATE is the default value&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;mapper&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;save&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;something&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;DynamoDBMapperConfig&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;SaveBehavior&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;UPDATE&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;There are four different configurations for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SaveBehavior&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UPDATE&lt;/code&gt; (default)&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UPDATE_SKIP_NULL_ATTRIBUTE&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CLOBBER&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;APPEND_SET&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Given this existing record and DynamoDB schema&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;AttributeName&lt;/th&gt;
      &lt;th&gt;key&lt;/th&gt;
      &lt;th&gt;modeled_scalar&lt;/th&gt;
      &lt;th&gt;modeled_set&lt;/th&gt;
      &lt;th&gt;unmodeled&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;KeyType&lt;/td&gt;
      &lt;td&gt;Hash&lt;/td&gt;
      &lt;td&gt;Non-key&lt;/td&gt;
      &lt;td&gt;Non-key&lt;/td&gt;
      &lt;td&gt;Non-key&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;AttributeType&lt;/td&gt;
      &lt;td&gt;Number&lt;/td&gt;
      &lt;td&gt;String&lt;/td&gt;
      &lt;td&gt;String set&lt;/td&gt;
      &lt;td&gt;String&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;div class=&quot;language-json highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;key&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;99&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;modeled_scalar&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;foo&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; 
    &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;modeled_set&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;foo0&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; 
        &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;foo1&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; 
    &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;unmodeled&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;bar&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; 
&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;together with this POJO class object&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nc&quot;&gt;TestTableItem&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;TestTableItem&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;setKey&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;99&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;setModeledScalar&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;setModeledSet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Collections&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;singleton&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;foo2&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;hr /&gt;

&lt;p&gt;&lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SaveBehavior.UPDATE&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UPDATE&lt;/code&gt; will not affect unmodeled attributes on a save operation, and a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;null&lt;/code&gt; value for the modeled attribute will &lt;strong&gt;remove&lt;/strong&gt; it from that item in DynamoDB.&lt;/p&gt;

&lt;p&gt;so basically after onvoking &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mapper.save()&lt;/code&gt;, the record in DDB will be as follows&lt;/p&gt;

&lt;div class=&quot;language-json highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
  &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;key&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;99&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
  &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;modeled_set&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;foo2&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
  &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; 
  &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;unmodeled&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;bar&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; 
&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;You can see that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;modeled_set&lt;/code&gt; has been updated, but since &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;modeled_scalar&lt;/code&gt; is passed in using a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;null&lt;/code&gt; value, so it is removed from DDB.&lt;/p&gt;

&lt;p&gt;In this case. if you wanna use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SaveBehavior.UPDATE&lt;/code&gt; to update something, you have to include all the fields with proper value in your DynamoDB POJO class. Otherwise, it will be removed from DDB table instead of keep them as is.&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;&lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SaveBehavior.UPDATE_SKIP_NULL_ATTRIBUTES&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UPDATE_SKIP_NULL_ATTRIBUTES&lt;/code&gt; is similar to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UPDATE&lt;/code&gt;, except that it ignores any &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;null&lt;/code&gt; value attribute(s) and will &lt;strong&gt;NOT&lt;/strong&gt; remove them from that item in DynamoDB.&lt;/p&gt;

&lt;div class=&quot;language-json highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
  &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;key&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;99&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
  &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;modeled_scalar&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;foo&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
  &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;modeled_set&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;foo2&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
  &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; 
  &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;unmodeled&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;bar&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; 
&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;As you can find out, even though we set &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;modeled_scalar&lt;/code&gt; to be &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;null&lt;/code&gt;, this field is still persisted in DDB. So with this configuration, when you trying to update something with only specifying the hashKey and the field you wanna alter, it will properly update the field and keep the others the same without deleting.&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;&lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SaveBehavior.CLOBBER&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;CLOBBER will &lt;em&gt;clear and replace all attributes&lt;/em&gt;, including unmodeled ones&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;(delete and recreate) on save.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class=&quot;language-json highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
  &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;key&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;99&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; 
  &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;modeled_set&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;foo2&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
  &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This is already self-explained. delete the record with same hashKey and re-create a new one with the specified fields.&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;&lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SaveBehavior.APPEND_SET&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;APPEND_SET&lt;/code&gt; treats scalar attributes (String, Number, Binary) the same as UPDATE_SKIP_NULL_ATTRIBUTES does.&lt;/p&gt;

&lt;p&gt;However, for &lt;strong&gt;set attributes, it will append to the existing attribute value&lt;/strong&gt;, instead of overriding it.&lt;/p&gt;

&lt;div class=&quot;language-json highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
  &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;key&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;99&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
  &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;modeled_scalar&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;foo&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
  &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;modeled_set&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;foo0&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; 
    &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;foo1&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; 
    &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;foo2&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
  &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; 
  &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;unmodeled&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;bar&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; 
&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Scalar attributes are kept, but set attributes are appended.&lt;/p&gt;

&lt;hr /&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;SaveBehavior&lt;/th&gt;
      &lt;th&gt;On unmodeled attribute&lt;/th&gt;
      &lt;th&gt;On null-value attribute&lt;/th&gt;
      &lt;th&gt;On set attribute&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;UPDATE&lt;/td&gt;
      &lt;td&gt;keep&lt;/td&gt;
      &lt;td&gt;remove&lt;/td&gt;
      &lt;td&gt;override&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;UPDATE_SKIP_NULL_ATTRIBUTES&lt;/td&gt;
      &lt;td&gt;keep&lt;/td&gt;
      &lt;td&gt;keep&lt;/td&gt;
      &lt;td&gt;override&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;CLOBBER&lt;/td&gt;
      &lt;td&gt;remove&lt;/td&gt;
      &lt;td&gt;remove&lt;/td&gt;
      &lt;td&gt;override&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;APPEND_SET&lt;/td&gt;
      &lt;td&gt;keep&lt;/td&gt;
      &lt;td&gt;keep&lt;/td&gt;
      &lt;td&gt;append&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;references&quot;&gt;References&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/bp-query-scan.html&quot;&gt;Best Practices for Querying and Scanning Data&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.dynamodbguide.com/local-or-global-choosing-a-secondary-index-type-in-dynamo-db/#the-too-long-didnt-read-version-of-choosing-an-index&quot;&gt;Local or global: Choosing a secondary index type in DynamoDB&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://aws.amazon.com/blogs/developer/using-the-savebehavior-configuration-for-the-dynamodbmapper/&quot;&gt;Using the SaveBehavior Configuration for the DynamoDBMapper&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content><author><name>Zhenye Na</name><email>nazhenye@gmail.com</email></author><category term="AWS" /><category term="DynamoDB" /><summary type="html">Tips for working with AWS DynamoDB</summary></entry><entry><title type="html">Design Patterns: Proxy Pattern</title><link href="/2022/01/06/design-patterns-the-proxy-pattern.html" rel="alternate" type="text/html" title="Design Patterns: Proxy Pattern" /><published>2022-01-06T00:00:00-08:00</published><updated>2022-01-06T00:00:00-08:00</updated><id>/2022/01/06/design-patterns-the-proxy-pattern</id><content type="html" xml:base="/2022/01/06/design-patterns-the-proxy-pattern.html">&lt;h2 id=&quot;intent&quot;&gt;Intent&lt;/h2&gt;

&lt;p&gt;Proxy pattern provides a surrogate or placeholder for another object to control access to it. This help control the access to the real object. You can think of proxy to real object as puppet to the person who is in real power.&lt;/p&gt;

&lt;h2 id=&quot;motivation&quot;&gt;Motivation&lt;/h2&gt;

&lt;p&gt;Suppose you have a small file to read, ~100 lines, the naive approach to read the file and process may look similar to the following Pseudocode:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;for line in readFile(&quot;filename.ext&quot;):
    process(line)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Since the file is not large, this solution may work fine.&lt;/p&gt;

&lt;p&gt;However, if the requirements changed:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;The file is pretty large. ~ 1M lines&lt;/li&gt;
  &lt;li&gt;The processing may last for the entire file or a few lines and then stops&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Will the above solution still on the table? I personally doubt.&lt;/p&gt;

&lt;p&gt;There is a better concept for this, which is called &lt;a href=&quot;https://www.baeldung.com/java-iterator&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;iterator&lt;/code&gt;&lt;/a&gt;. Behind the scenes, it introduces another concept which is similar to the meaning, lazy loading, where this may be more popular in Front-end development.&lt;/p&gt;

&lt;p class=&quot;info&quot;&gt;Lazy loading is the practice of delaying load or initialization of resources or objects until they’re actually needed to improve performance and save system resources.&lt;/p&gt;

&lt;p&gt;Similarly, we can apply this “idea” when we do for object creation, and only forward the request to the real object when client’s code wanna retrieve or alter something.&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;Another good example is in the Gang of Four - Design Patterns book:&lt;/p&gt;

&lt;p&gt;When you are developing a text editor, like Microsoft Word or Markdown editor. You can upload and change the image during the editor. But you dont wanna create all the objects that refer to the image creation, updates, etc.&lt;/p&gt;

&lt;p&gt;What you can do is that use an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ImageProxy&lt;/code&gt; which pretends to be a real image, but not, and forward a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;display&lt;/code&gt; request or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;crop&lt;/code&gt; request to the real image when these methods are invoked.&lt;/p&gt;

&lt;p class=&quot;info&quot;&gt;For a pretty expensive object, during creation, we can do the creation &lt;em&gt;on demand&lt;/em&gt; - &lt;em&gt;lazy initialization&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;This “expensive object”could also be a third-party library or tools, like a local DynamoDB container. (?)&lt;/p&gt;

&lt;h2 id=&quot;applicability&quot;&gt;Applicability&lt;/h2&gt;

&lt;p&gt;There are four different types of Proxy pattern:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Remote proxy
    &lt;ol&gt;
      &lt;li&gt;provides a local representative of an object in a different address space.
        &lt;ol&gt;
          &lt;li&gt;one example which gives the feeling similar to this proxy pattern is the &lt;a href=&quot;https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBLocal.html&quot;&gt;local version of AWS DynamoDB&lt;/a&gt;. though this may not the perfect example&lt;/li&gt;
        &lt;/ol&gt;
      &lt;/li&gt;
    &lt;/ol&gt;
  &lt;/li&gt;
  &lt;li&gt;Virtual Proxy
    &lt;ol&gt;
      &lt;li&gt;creates expensive objects on demand
        &lt;ol&gt;
          &lt;li&gt;delays the time creating the object, only create when you need communicate with the real object&lt;/li&gt;
        &lt;/ol&gt;
      &lt;/li&gt;
    &lt;/ol&gt;
  &lt;/li&gt;
  &lt;li&gt;Protection Proxy
    &lt;ol&gt;
      &lt;li&gt;controls access to the real object
        &lt;ol&gt;
          &lt;li&gt;no detailed example on code level. However, I think AWS Bastion Host has some similarity to this type of Proxy pattern. =&amp;gt; to communicate with the production server, you need to pass the Bastion Host “proxy”. though this may not the perfect example&lt;/li&gt;
        &lt;/ol&gt;
      &lt;/li&gt;
    &lt;/ol&gt;
  &lt;/li&gt;
  &lt;li&gt;Smart reference
    &lt;ol&gt;
      &lt;li&gt;checking the reference to the real object so that it could be freed automatically when no references to it =? smart pointers&lt;/li&gt;
      &lt;li&gt;loading a persistent object into memory when it is first referenced =&amp;gt; Caching Proxy&lt;/li&gt;
    &lt;/ol&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;references&quot;&gt;References&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://refactoring.guru/design-patterns/proxy&quot;&gt;Proxy Pattern - Refactoring.Guru&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.amazon.com/Design-Patterns-Elements-Reusable-Object-Oriented/dp/0201633612&quot;&gt;Design Patterns: Elements of Reusable Object-Oriented Software&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.amazon.com/Head-First-Design-Patterns-Object-Oriented/dp/149207800X/ref=pd_lpo_1?pd_rd_i=149207800X&amp;amp;psc=1&quot;&gt;Head First Design Patterns&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content><author><name>Zhenye Na</name><email>nazhenye@gmail.com</email></author><category term="Design Patterns" /><category term="Proxy Pattern" /><category term="Object-Oriented Design" /><summary type="html">With you as my proxy, I will be able to triple the amount of lunch money I can extract from friends!</summary></entry></feed>