<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Chuck1sn</title>
    <description>The latest articles on DEV Community by Chuck1sn (@chuck1sn).</description>
    <link>https://dev.to/chuck1sn</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1215209%2F72596b32-d632-41ca-9b6d-938d8de0bdd1.png</url>
      <title>DEV Community: Chuck1sn</title>
      <link>https://dev.to/chuck1sn</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/chuck1sn"/>
    <language>en</language>
    <item>
      <title>"You Don't Know Java" 💘 The Philosophy of Unit Test Naming</title>
      <dc:creator>Chuck1sn</dc:creator>
      <pubDate>Mon, 17 Feb 2025 08:43:23 +0000</pubDate>
      <link>https://dev.to/chuck1sn/you-dont-know-java-the-philosophy-of-unit-test-naming-1j2c</link>
      <guid>https://dev.to/chuck1sn/you-dont-know-java-the-philosophy-of-unit-test-naming-1j2c</guid>
      <description>&lt;h1&gt;
  
  
  The Design Philosophy of Unit Testing
&lt;/h1&gt;

&lt;p&gt;Programs are written primarily for humans to read, and only incidentally for machines to execute.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Programs must be written for people to read, and only incidentally for machines to execute.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The first step in writing code is naming, and the same applies to unit tests.&lt;/p&gt;

&lt;h2&gt;
  
  
  WWW
&lt;/h2&gt;

&lt;p&gt;How can you make the names of your unit tests understandable? You need to reflect three elements in the naming, abbreviated as the WWW principle.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What are you testing? (what)&lt;/li&gt;
&lt;li&gt;Under what conditions are you testing? (when)&lt;/li&gt;
&lt;li&gt;What behavior do you expect? (want)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's take a look at a &lt;a href="https://github.com/ccmjga/mjga-scaffold/blob/main/src/test/java/com/zl/mjga/unit/JwtUnitTest.java" rel="noopener noreferrer"&gt;unit test example&lt;/a&gt; from the MJGA scaffolding.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;  &lt;span class="nd"&gt;@Test&lt;/span&gt;
  &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;createVerifyGetSubjectJwt_givenUserIdentify_shouldReturnTrueAndGetExpectIdentify&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;jwt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cookieJwt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;createJwt&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"1"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;assertThat&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cookieJwt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;verifyToken&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;jwt&lt;/span&gt;&lt;span class="o"&gt;)).&lt;/span&gt;&lt;span class="na"&gt;isTrue&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="n"&gt;assertThat&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cookieJwt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getSubject&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;jwt&lt;/span&gt;&lt;span class="o"&gt;)).&lt;/span&gt;&lt;span class="na"&gt;isEqualTo&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"1"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Breaking down the name into three parts:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;createVerifyGetSubjectJwt&lt;/code&gt; reflects the object being tested.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;givenUserIdentify&lt;/code&gt; indicates the condition under which the test is conducted.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;shouldReturnTrueAndGetExpectIdentify&lt;/code&gt; describes the expected behavior.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is a good name—one that a business person can understand.&lt;/p&gt;

&lt;h3&gt;
  
  
  AAA
&lt;/h3&gt;

&lt;p&gt;The first hurdle is over; now let's implement the test logic. In fact, with the WWW naming design, the logic implementation already has a clear path. Like naming, test logic also has principles to follow, commonly known as the AAA principle.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Arrange&lt;/li&gt;
&lt;li&gt;Act&lt;/li&gt;
&lt;li&gt;Assert&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's continue with a &lt;a href="https://github.com/ccmjga/mjga-scaffold/blob/main/src/test/java/com/zl/mjga/integration/mvc/SignMvcTest.java" rel="noopener noreferrer"&gt;test case&lt;/a&gt; from the MJGA scaffolding:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Test&lt;/span&gt;
  &lt;span class="nd"&gt;@WithMockUser&lt;/span&gt;
  &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;signIn_givenValidHttpRequest_shouldSucceedWith200&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="kd"&gt;throws&lt;/span&gt; &lt;span class="nc"&gt;Exception&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;stubUsername&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"test_04cb017e1fe6"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;stubPassword&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"test_567472858b8c"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="nc"&gt;SignInDto&lt;/span&gt; &lt;span class="n"&gt;signInDto&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;SignInDto&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="n"&gt;signInDto&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setUsername&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stubUsername&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;signInDto&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setPassword&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stubPassword&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;when&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;signService&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;signIn&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;signInDto&lt;/span&gt;&lt;span class="o"&gt;)).&lt;/span&gt;&lt;span class="na"&gt;thenReturn&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1L&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

    &lt;span class="n"&gt;mockMvc&lt;/span&gt;
        &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;perform&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;
            &lt;span class="n"&gt;post&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/auth/sign-in"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
                &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;contentType&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;MediaType&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;APPLICATION_JSON&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
                &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;content&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;
                    &lt;span class="sh"&gt;"""
                {
                  "username": "test_04cb017e1fe6",
                  "password": "test_567472858b8c"
                }
                 """&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
                &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;with&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;csrf&lt;/span&gt;&lt;span class="o"&gt;()))&lt;/span&gt;
        &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;andExpect&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;isOk&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Arrange
&lt;/h4&gt;

&lt;p&gt;This is the preparation phase of the test. In this phase, you need to construct some code that provides context, such as inserting data, building objects, or even more complex mocks and stubs. In the example, we constructed the SignInDto object for subsequent use and assumed the return value of the signIn method—all in preparation for the subsequent test.&lt;/p&gt;

&lt;h4&gt;
  
  
  Act
&lt;/h4&gt;

&lt;p&gt;This is the execution phase, corresponding to &lt;code&gt;mockMvc.perform&lt;/code&gt; in the example. The API is just a bit complex here, so you can think of it as a single line of code.&lt;/p&gt;

&lt;h4&gt;
  
  
  Assert
&lt;/h4&gt;

&lt;p&gt;You need to clearly state what result you expect. In the example, this corresponds to &lt;code&gt;.andExpect(status().isOk());&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Often, you may need to write some test methods to verify whether a certain logic can run without errors, as the method itself has no return value. This is easy to solve using &lt;code&gt;assertDoesNotThrow&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt; &lt;span class="c1"&gt;// pause &amp;amp; resume job&lt;/span&gt;
    &lt;span class="nc"&gt;JobKey&lt;/span&gt; &lt;span class="n"&gt;firstDataBackupJobKey&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;dataBackupJobKeys&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;iterator&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;next&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="n"&gt;assertDoesNotThrow&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;
        &lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
          &lt;span class="n"&gt;dataBackupScheduler&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;pauseJob&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;firstDataBackupJobKey&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
          &lt;span class="n"&gt;dataBackupScheduler&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;resumeJob&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;firstDataBackupJobKey&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="o"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These coding philosophies are independent of the language and framework you use. Any unit test is written with this mindset. More content on unit testing will be shared in the future.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;I am Chuck1sn, a developer long committed to promoting the modern JVM ecosystem.&lt;/li&gt;
&lt;li&gt;Your replies, likes, and bookmarks are the motivation for my continuous updates.&lt;/li&gt;
&lt;li&gt;A simple triple action (like, comment, share) means a lot to me and is greatly appreciated!&lt;/li&gt;
&lt;li&gt;Follow my account to receive article updates as soon as they are published.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;PS: All the code examples above can be found in the &lt;a href="https://github.com/ccmjga/mjga-scaffold/blob/main/README_EN.md" rel="noopener noreferrer"&gt;Github repository&lt;/a&gt;. If it helps, please give it a Star—it's a great encouragement to me. Thank you!&lt;/p&gt;

</description>
      <category>java</category>
      <category>springboot</category>
      <category>unittest</category>
      <category>docker</category>
    </item>
    <item>
      <title>"You Don’t Know Java" 💫 Dto, Vm, Vo, Entity, Domain, Bean, and Pojo reference Table</title>
      <dc:creator>Chuck1sn</dc:creator>
      <pubDate>Wed, 14 Feb 2024 03:28:44 +0000</pubDate>
      <link>https://dev.to/chuck1sn/a-table-for-easy-reference-on-dto-vm-vo-entity-domain-bean-and-pojo-available-for-everyone-to-consult-at-any-time-2o4j</link>
      <guid>https://dev.to/chuck1sn/a-table-for-easy-reference-on-dto-vm-vo-entity-domain-bean-and-pojo-available-for-everyone-to-consult-at-any-time-2o4j</guid>
      <description>&lt;p&gt;I have compiled a table for easy reference on Dto, Vm, Vo, Entity, Domain, Bean, and Pojo, which are commonly used concepts in object-oriented programming.&lt;/p&gt;

&lt;p&gt;Please note that this table is not a strict convention to be followed, and in actual development, you should refer to it based on your specific needs rather than blindly applying it.&lt;/p&gt;

&lt;p&gt;In this scaffold, I have not strictly adhered to all the concepts listed in the table but have used some of them based on the actual requirements.&lt;/p&gt;

&lt;p&gt;You can find the scaffold at the following URLs:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/ccmjga/mjga-scaffold" rel="noopener noreferrer"&gt;https://github.com/ccmjga/mjga-scaffold&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.mjga.cc" rel="noopener noreferrer"&gt;https://www.mjga.cc&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Most of the time, I use entities, DTOs, and view models (VMs). When my project grows larger, I introduce domain objects and value objects (VOs).&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Suffix&lt;/th&gt;
&lt;th&gt;Definition&lt;/th&gt;
&lt;th&gt;Purpose&lt;/th&gt;
&lt;th&gt;Note&lt;/th&gt;
&lt;th&gt;Three-Layer Architecture&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;DTO&lt;/td&gt;
&lt;td&gt;Data Transfer Object. Usually contains only properties and getter/setter methods, no business logic.&lt;/td&gt;
&lt;td&gt;Used to transfer data between different layers of the application&lt;/td&gt;
&lt;td&gt;Immutable.&lt;/td&gt;
&lt;td&gt;Service&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;VM&lt;/td&gt;
&lt;td&gt;View Model. Typically represented by immutable objects such as records.&lt;/td&gt;
&lt;td&gt;Maps data in the view and decouples the view from the model&lt;/td&gt;
&lt;td&gt;Immutable.&lt;/td&gt;
&lt;td&gt;Controller&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Entity&lt;/td&gt;
&lt;td&gt;Entity Object. Typically contains the properties and behavior of an entity, but not complex business logic.&lt;/td&gt;
&lt;td&gt;Represents entities or objects in the business domain, such as users, orders, products, etc. Part of the domain model. Typically created and managed by the data access layer.&lt;/td&gt;
&lt;td&gt;Has an independent lifecycle.&lt;/td&gt;
&lt;td&gt;DAO&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Vo&lt;/td&gt;
&lt;td&gt;Value Object. An immutable object whose value does not change after creation. It does not contain business logic or complex computations.&lt;/td&gt;
&lt;td&gt;A standalone class that does not directly depend on business logic or the data access layer. Typically created and managed by the domain model or service layer to represent simple values.&lt;/td&gt;
&lt;td&gt;Does not have an independent lifecycle. Requires defining the equals method.&lt;/td&gt;
&lt;td&gt;Service&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;DO (Domain Object)&lt;/td&gt;
&lt;td&gt;Domain Model. Typically holds multiple class members such as entities, value objects, etc., that constitute the business aspects of the domain, and includes complex methods implementing business logic.&lt;/td&gt;
&lt;td&gt;Used to represent the business concepts and rules of a specific domain, encapsulating the business logic and behavior of the application.&lt;/td&gt;
&lt;td&gt;Has an independent lifecycle. The lifecycle of the value objects it holds depends on the domain model.&lt;/td&gt;
&lt;td&gt;Service&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Name&lt;/th&gt;
&lt;th&gt;Definition&lt;/th&gt;
&lt;th&gt;Purpose&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Bean&lt;/td&gt;
&lt;td&gt;Follows specific rules. Must have a no-arg constructor and properties accessed using getter and setter methods.&lt;/td&gt;
&lt;td&gt;Suitable for transferring data between different layers or encapsulating business-agnostic functionality such as objects managed by the Spring container.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Pojo&lt;/td&gt;
&lt;td&gt;Plain Old Java Object, with no specific restrictions.&lt;/td&gt;
&lt;td&gt;Suitable for various scenarios, especially those related to business, such as mapping database entities.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;By the way, as a side note, if this table or scaffold is helpful to you, you may consider giving me a star. It would be a great encouragement for me. Thank you for your generosity!&lt;/p&gt;

</description>
      <category>oop</category>
      <category>java</category>
      <category>computerscience</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
