<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Jayson GCS on Medium]]></title>
        <description><![CDATA[Stories by Jayson GCS on Medium]]></description>
        <link>https://medium.com/@jsongcs?source=rss-13eeafc60e6d------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*jwzX8AMRITadIn7StIr9aA.jpeg</url>
            <title>Stories by Jayson GCS on Medium</title>
            <link>https://medium.com/@jsongcs?source=rss-13eeafc60e6d------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 11:16:35 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@jsongcs/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Manage Java Versions with jEnv]]></title>
            <link>https://medium.com/javarevisited/manage-java-versions-with-jenv-6b8228552661?source=rss-13eeafc60e6d------2</link>
            <guid isPermaLink="false">https://medium.com/p/6b8228552661</guid>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[jenv]]></category>
            <category><![CDATA[command-line]]></category>
            <category><![CDATA[developer-experience]]></category>
            <category><![CDATA[java]]></category>
            <dc:creator><![CDATA[Jayson GCS]]></dc:creator>
            <pubDate>Mon, 11 Sep 2023 15:41:07 GMT</pubDate>
            <atom:updated>2023-09-11T15:41:07.420Z</atom:updated>
            <content:encoded><![CDATA[<h4>Master your Java Environment</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*XflGzt-qC0cvU5Dx" /><figcaption>Photo by <a href="https://unsplash.com/@nickgrantphoto?utm_source=medium&amp;utm_medium=referral">Nick Grant</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>Introduction</h3><p>There are times where we are required to work on multiple Java projects and find ourselves needing to <strong>switch between different Java versions</strong>. It can be pretty troublesome to manage different Java versions without some quick and reliable tools.</p><p>Today, we will be looking at one such command line tool called <strong>jEnv</strong>, to improve the <strong>developer experience</strong> for the Java version management workflow.</p><h3>Installation</h3><h4>On MacOS</h4><pre>brew install jenv</pre><h4>On Linux</h4><pre>git clone https://github.com/jenv/jenv.git ~/.jenv<br># Shell: bash<br>echo &#39;export PATH=&quot;$HOME/.jenv/bin:$PATH&quot;&#39; &gt;&gt; ~/.bash_profile<br>echo &#39;eval &quot;$(jenv init -)&quot;&#39; &gt;&gt; ~/.bash_profile<br># Shell: zsh<br>echo &#39;export PATH=&quot;$HOME/.jenv/bin:$PATH&quot;&#39; &gt;&gt; ~/.zshrc<br>echo &#39;eval &quot;$(jenv init -)&quot;&#39; &gt;&gt; ~/.zshrc</pre><h4>On Windows</h4><p><strong>jEnv</strong> supports <strong>Linux</strong> and <strong>MacOS</strong> only, but it is possible to install it on <strong>Windows</strong> by installing <a href="https://learn.microsoft.com/en-us/windows/wsl/install">Windows Subsystem for Linux</a>.</p><p>Alternatively, there is another similar project called <strong>“JEnv-for-Windows” </strong>, which is written in <strong>cmd</strong> and <strong>powershell</strong> with similar syntax to fulfil the same purpose.</p><p><a href="https://github.com/FelixSelter/JEnv-for-Windows">GitHub - FelixSelter/JEnv-for-Windows: Change your current Java version with one line</a></p><h4>Verify installation</h4><p>Run the below command to verify the installation.</p><pre>jenv doctor</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MKbz0MDs1SgsX-_CNJj8eQ.png" /></figure><p>In my case, I am using <strong>ZSH shell</strong> for my terminal. So if we see the error “Jenv is not loaded in your zsh”, follow the suggested command to update the .zshrc config file and restart my ZSH session.</p><blockquote>Take note that for some Apple Silicon Mac, you may need to include an additional command to setup the path before running “echo eval “$(jenv init -)” &gt;&gt; ~/.zshrc” (reference: <a href="https://github.com/jenv/jenv/issues/339#issuecomment-1190698110">link</a>)</blockquote><pre># Take note to run below command if you are running on Apple Silicon Mac<br># echo &#39;export PATH=&quot;$HOME/.jenv/bin:$PATH&quot;&#39; &gt;&gt; ~/.zshrc<br><br># Append jEnv init config to .zshrc<br>echo eval &quot;$(jenv init -)&quot; &gt;&gt; ~/.zshrc<br># Restart ZSH session<br>source ~/.zshrc<br># Alternative way to restart<br>exec $SHELL -l</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*r9fKrs9xlCxofHm-_w9jTQ.png" /></figure><p>Also, in order for <strong>jEnv</strong> to automatically set our <strong>JAVA_HOME</strong>, run the following commands to enable export plugin</p><pre># Enable export plugin<br>jenv enable-plugin export<br># Restart ZSH session<br>source ~/.zshrc<br># Alternative way to restart<br>exec $SHELL -l</pre><h3>Add Java to jEnv</h3><p>Next step, let us add Java to our newly installed jEnv.</p><h4>Existing Java Setup</h4><p>If we have an existing Java setup, we may just simply add it to jEnv without additional installation. Let’s run the following command to find out.</p><pre>/usr/libexec/java_home</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tzFR4CrJ3Ei6X2pyAvPmXw.png" /></figure><p>So to add existing Java installation to jEnv, simply run the below command.</p><pre>jenv add &quot;$(/usr/libexec/java_home)&quot;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*voxUecBftjMPJ_0jUp4mLQ.png" /></figure><p>Finally, run the next command to verify if we have successfully loaded the intended Java to jEnv.</p><pre>jenv versions</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4UcXFt6_n7JFpII2etB2oQ.png" /></figure><h4>Other existing Java installation</h4><p>If we know that we have multiple Java versions installed locally, we can add them to jEnv. In my case, I am using “<strong>openjdk</strong>” so I perform the following steps to retrieve all my existing “<strong>openjdk</strong>” installations. My example here is based on “<strong>brew</strong>”, as my example setup is done in <strong>MacOS</strong>, do adjust the steps accordingly if you are using <strong>Linux</strong> or <strong>Windows</strong>.</p><pre># For instance, I have existing Java installations via brew<br>brew ls | grep &#39;openjdk&#39;<br># Find out the path<br>brew --cellar openjdk@17</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6LtSM1vyBkCPBOI96CoKEg.png" /></figure><p>Navigate to the ../Home sub-directory and add that path to jEnv.</p><pre>jenv add /opt/homebrew/Cellar/openjdk@17/17.0.8.1/libexec/openjdk.jdk/Contents/Home/</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hyFY_cILZ7dk-wsjAthoeQ.png" /></figure><p>Verify that all the intended versions are correctly added to jEnv.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Anarn_uXl8SRJwryY8TLNQ.png" /></figure><h4>Switch between desired Java versions</h4><p>Finally, we have a convenient setup to switch between Java versions seamlessly.</p><pre># Switch the local directory to the desired Java version<br>jenv local 11.0.20.1<br># Verify the currently selected Java version<br>cat .java-version</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Z0j-vvcoV_nCnZ_vf-SILQ.png" /></figure><p>There are also other ways to manage the Java version in the <strong>global</strong> and <strong>shell</strong> scope.</p><pre># Setting global Java version<br>jenv global 11.0.20.1<br># Setting shell Java version<br>jenv shell 11.0.20.1</pre><h3>Plugins</h3><p>There are also various <strong>plugins included</strong> with jEnv. For examples, we may also use jEnv to manage our <strong>Maven</strong> and <strong>Gradle</strong>.</p><blockquote>Feel free to play around with various plugins, if you find that the plugin does not fulfil your needs, we can also disable it afterwards via “disable-plugin” option.</blockquote><pre># Enable plugin for maven<br>jenv enable-plugin maven<br># Disable plugin for maven<br>jenv disable-plugin maven</pre><p>More information regarding the supported plugins.</p><p><a href="https://github.com/jenv/jenv/tree/master/available-plugins">jenv/available-plugins at master · jenv/jenv</a></p><h3>Resources</h3><ul><li><a href="https://github.com/jenv/jenv/tree/master">GitHub - jenv/jenv: Manage your Java environment</a></li><li><a href="https://www.jenv.be/">jEnv</a></li></ul><h3>Finally</h3><p><strong>Thanks for reading</strong> and I hope this article may offer some valuable information to you. I look forward to your following and feel free to check out my other articles <a href="https://medium.com/@jsongcs">here</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6b8228552661" width="1" height="1" alt=""><hr><p><a href="https://medium.com/javarevisited/manage-java-versions-with-jenv-6b8228552661">Manage Java Versions with jEnv</a> was originally published in <a href="https://medium.com/javarevisited">Javarevisited</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[9 Tips on Defensive Programming]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://levelup.gitconnected.com/9-tips-on-defensive-programming-79edf0d35c3b?source=rss-13eeafc60e6d------2"><img src="https://cdn-images-1.medium.com/max/1920/1*JDbMP6zZS6GlKk-EEuewdw.jpeg" width="1920"></a></p><p class="medium-feed-snippet">Minimising the Risk of Production Bugs with Defensive Programming</p><p class="medium-feed-link"><a href="https://levelup.gitconnected.com/9-tips-on-defensive-programming-79edf0d35c3b?source=rss-13eeafc60e6d------2">Continue reading on Level Up Coding »</a></p></div>]]></description>
            <link>https://levelup.gitconnected.com/9-tips-on-defensive-programming-79edf0d35c3b?source=rss-13eeafc60e6d------2</link>
            <guid isPermaLink="false">https://medium.com/p/79edf0d35c3b</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[defensive-programming]]></category>
            <category><![CDATA[best-practices]]></category>
            <category><![CDATA[technology]]></category>
            <category><![CDATA[software-development]]></category>
            <dc:creator><![CDATA[Jayson GCS]]></dc:creator>
            <pubDate>Sun, 16 Apr 2023 20:09:09 GMT</pubDate>
            <atom:updated>2023-04-16T20:09:09.848Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Redux Gotchas — useSelector]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://levelup.gitconnected.com/redux-gotchas-useselector-8f2aeb8973c9?source=rss-13eeafc60e6d------2"><img src="https://cdn-images-1.medium.com/max/1444/1*7pnp2sY7ctt6yFXapx5eFw.jpeg" width="1444"></a></p><p class="medium-feed-snippet">Prevent unexpected re-rendering and potential bugs</p><p class="medium-feed-link"><a href="https://levelup.gitconnected.com/redux-gotchas-useselector-8f2aeb8973c9?source=rss-13eeafc60e6d------2">Continue reading on Level Up Coding »</a></p></div>]]></description>
            <link>https://levelup.gitconnected.com/redux-gotchas-useselector-8f2aeb8973c9?source=rss-13eeafc60e6d------2</link>
            <guid isPermaLink="false">https://medium.com/p/8f2aeb8973c9</guid>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[redux]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[software-engineering]]></category>
            <dc:creator><![CDATA[Jayson GCS]]></dc:creator>
            <pubDate>Fri, 16 Dec 2022 00:55:30 GMT</pubDate>
            <atom:updated>2022-12-16T00:55:30.573Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Do More with Less Java — Lombok and Experimental Features]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/javarevisited/do-more-with-less-java-lombok-and-experimental-features-8d71d0bcb27c?source=rss-13eeafc60e6d------2"><img src="https://cdn-images-1.medium.com/max/2600/1*jIKyLKtUat0fY68JDst2aw.jpeg" width="3000"></a></p><p class="medium-feed-snippet">Lombok makes Java cool again</p><p class="medium-feed-link"><a href="https://medium.com/javarevisited/do-more-with-less-java-lombok-and-experimental-features-8d71d0bcb27c?source=rss-13eeafc60e6d------2">Continue reading on Javarevisited »</a></p></div>]]></description>
            <link>https://medium.com/javarevisited/do-more-with-less-java-lombok-and-experimental-features-8d71d0bcb27c?source=rss-13eeafc60e6d------2</link>
            <guid isPermaLink="false">https://medium.com/p/8d71d0bcb27c</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[backend]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[software-development]]></category>
            <dc:creator><![CDATA[Jayson GCS]]></dc:creator>
            <pubDate>Thu, 02 Jun 2022 15:37:49 GMT</pubDate>
            <atom:updated>2022-06-02T15:37:49.967Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[How to Transition from Backend to Full-Stack Developer]]></title>
            <link>https://levelup.gitconnected.com/how-to-transition-from-backend-to-full-stack-developer-9796d2b926eb?source=rss-13eeafc60e6d------2</link>
            <guid isPermaLink="false">https://medium.com/p/9796d2b926eb</guid>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[backend-development]]></category>
            <category><![CDATA[front-end-development]]></category>
            <category><![CDATA[full-stack-developer]]></category>
            <dc:creator><![CDATA[Jayson GCS]]></dc:creator>
            <pubDate>Mon, 02 May 2022 00:06:54 GMT</pubDate>
            <atom:updated>2022-12-07T02:24:16.365Z</atom:updated>
            <content:encoded><![CDATA[<h4>A practical and honest guide from personal experience</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RJE-HFf9O16iPFEQMBA4Xg.jpeg" /></figure><p>First of all, ask yourself: why do you want to be a full-stack developer? Before jumping on to the bandwagon, make sure you are aware of what you are getting yourself into.</p><p>Here is a non-exhaustive list (IMO) of what a full-stack developer is:</p><ul><li>A <strong>sponge</strong>: flexible enough to work on multiple domains at times and absorbent to soak up all the knowledge.</li><li>An <strong>architect</strong>: being a jack of all trades does not mean that you are a master of none, it simply means that your problem statement is different from a pure backend or frontend engineer. The craftsmanship of a full-stack developer is hard to summarize in one sentence, but a good enough description would be — a backend and frontend engineer solves problems from a first-person view, while a full-stack developer solves problem from the third-person view.</li><li>A <strong>diplomat</strong>: since you get to see the big picture, you will most likely interact more with the PMs as well. You are the glue and cement between the frontend and backend teams, because you travel between realms, and therefore you should be objective and not have any bias towards a certain tech stack.</li></ul><p>But don’t fret, if you are already a proficient backend developer, you are probably more than 50% closer to becoming a full-stack developer. Think of full-stack development as consisting of 50% backend and 50% frontend. Let’s say the passing score is 70%, then you just need another 20% to be qualified as a full-stack developer!</p><p>Ok, sorry for the oversimplification, but you should definitely give yourself some credit for being proficient in the backend domain. So if you are determined to embark on this journey, let’s get started.</p><h3>Preparation Stage</h3><h4>Avoid the tutorial hell — build something!</h4><p>The best way to gain experience and credentials is to get your hands dirty.</p><ul><li>Look for opportunities in your team or surroundings. Get involved in an existing UI project, or spearhead a new idea. Aim to improve a certain process or to solve a real problem.</li><li>Otherwise, get yourself exposed to the frontend world by building pet projects such as personal websites, budgeting apps and so on.</li></ul><blockquote>The key here is to gain insight of what it means by building a user facing tool. This provides you with a realistic experience and a good story to tell.</blockquote><h4>Master your frontend basics</h4><p><strong>Bread and butter</strong></p><ul><li><strong>JavaScript </strong>is a must, but absolutely learn <strong>TypeScript </strong>if you have time on your side.</li><li><strong>CSS </strong>or CSS preprocessor such as <strong>SASS</strong></li><li><strong>HTML </strong>(of course)</li></ul><p><strong>Frontend framework or library</strong></p><ul><li>Focus your full attention to build a solid foundation in one of these popular frontend framework/libraries — <strong>React</strong>, <strong>Angular </strong>or <strong>Vue</strong>.</li><li>What do I mean by foundation? Here are some examples for React:</li></ul><blockquote>— <strong>Class-based component </strong>basics (don’t delve too much into it, most places are migrating to hook-based React).</blockquote><blockquote>—<strong>Basic hooks</strong> such as useState, useEffect, useCallback, useMemo, useRef, useContext and useReducer. Understand when to use them and compare their differences.</blockquote><blockquote>— <strong>State management</strong> in React, e.g. Redux + Saga, React context, prop drill and etc.</blockquote><blockquote>—<strong> Design pattern</strong>: e.g. Presentational &amp; Container Component Pattern and Compound Component Pattern</blockquote><blockquote>— Articulate the <strong>benefit of React</strong> and <strong>how React works</strong> under the hood.</blockquote><blockquote>Pay your dues and grind your foundation, treat it as your sledgehammer to break into the first door</blockquote><p><strong>Good to have</strong></p><p>The list here is not necessarily crucial, but it would give you an edge when applying to a new team or a new job as a full-stack developer:</p><ul><li><strong>Frontend/JavaScript testing</strong> — jest, chai, enzyme/react-testing-library (React specific)</li><li><strong>UI component tool </strong>— storybook</li><li><strong>End-to-end testing</strong> — Cypress or TestCafe</li></ul><h4>Cover your back(end)</h4><p>If you have paid your dues in your current backend engineering work, you should have no problem in your backend domain knowledge.</p><p>Some interviewers will challenge your backend knowledge more than your frontend because this is where you should be more comfortable with, so make sure you are well-prepared.</p><p>I won’t go deep into how to prepare for the backend, but here are some reminders:</p><ul><li>design pattern, testing and OOP (if relevant)</li><li>Error and exception handling</li><li>CRUD and HTTP</li><li>Security related topics (XSS, XSRF, Injection, authentication &amp; authorization and etc.)</li><li>Performance optimization and caching strategy</li></ul><h4>Test yourself with open-ended questions</h4><p>Now, once you have gained sufficient exposure in frontend, test yourself with these open-ended questions. Try to tackle them from a full-stack perspective:</p><ul><li>How would you build an e-commerce website?</li><li>Thin client &amp; thick server VS thick client &amp; thin server</li><li>How would you optimize an application’s or feature’s load time</li><li>Microservice vs Monolith architecture</li></ul><h4>Communication skills</h4><p>One of the most underrated skills. This is purely my opinion, as a full-stack developer, you tend to work with more people as compared to other engineering roles.</p><p>Since you have an overarching understanding of the application end-to-end, you are likely to work closer with the stakeholders and PMs.</p><p>You will almost definitely be the burger patty between the frontend and backend engineers, so it is important to have empathy and understanding of both domains.</p><h3>Additional Resources</h3><p>Here are some resources, which I personally like, that might help you in your preparation:</p><ul><li><a href="https://github.com/yangshun/front-end-interview-handbook">GitHub - yangshun/front-end-interview-handbook: ⚡️ Front End interview preparation materials for busy engineers</a></li><li><a href="https://www.youtube.com/c/WebDevSimplified"></a></li></ul><p>Transitioning into a new role is scary, but don’t let the unknowns deter you. Set small goals and an achievable timeline, then you will be fine. Don’t wait for the perfect moment and just do it.</p><blockquote>If you found this article helpful, do share it with your friends and give a few claps. Feel free to check out my other articles as well:</blockquote><ul><li><a href="https://medium.com/javarevisited/do-more-with-less-java-lombok-and-experimental-features-8d71d0bcb27c">Do More with Less Java — Lombok and Experimental Features</a></li><li><a href="https://medium.com/javarevisited/generate-typescript-interfaces-from-java-classes-d53f8c590500">Generate TypeScript interfaces from Java Classes</a></li></ul><p><em>If you enjoy such stories, do consider becoming a </em><a href="https://jsongcs.medium.com/membership"><em>medium member</em></a><em>. It costs $5 per month and gives you unlimited access to Medium content. I’ll get a little commission if you sign up via </em><a href="https://jsongcs.medium.com/membership"><em>link</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9796d2b926eb" width="1" height="1" alt=""><hr><p><a href="https://levelup.gitconnected.com/how-to-transition-from-backend-to-full-stack-developer-9796d2b926eb">How to Transition from Backend to Full-Stack Developer</a> was originally published in <a href="https://levelup.gitconnected.com/">Level Up Coding</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Generate TypeScript interfaces from Java Classes]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/javarevisited/generate-typescript-interfaces-from-java-classes-d53f8c590500?source=rss-13eeafc60e6d------2"><img src="https://cdn-images-1.medium.com/max/2107/1*TqH0ZS7mUcbPGYakA43xUA.jpeg" width="2107"></a></p><p class="medium-feed-snippet">Why spend time writing interfaces when you can enjoy a cup of afternoon flat white instead?</p><p class="medium-feed-link"><a href="https://medium.com/javarevisited/generate-typescript-interfaces-from-java-classes-d53f8c590500?source=rss-13eeafc60e6d------2">Continue reading on Javarevisited »</a></p></div>]]></description>
            <link>https://medium.com/javarevisited/generate-typescript-interfaces-from-java-classes-d53f8c590500?source=rss-13eeafc60e6d------2</link>
            <guid isPermaLink="false">https://medium.com/p/d53f8c590500</guid>
            <category><![CDATA[code-generation]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[java]]></category>
            <dc:creator><![CDATA[Jayson GCS]]></dc:creator>
            <pubDate>Mon, 14 Mar 2022 15:05:25 GMT</pubDate>
            <atom:updated>2022-03-14T15:05:25.963Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[A Quick Start Guide to Java Unit Testing — Mockito and JUnit 5]]></title>
            <link>https://medium.com/javarevisited/a-quick-start-guide-to-java-unit-testing-mockito-and-junit-5-eebb49d7bcd3?source=rss-13eeafc60e6d------2</link>
            <guid isPermaLink="false">https://medium.com/p/eebb49d7bcd3</guid>
            <category><![CDATA[unit-testing]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[mockito]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Jayson GCS]]></dc:creator>
            <pubDate>Sat, 22 May 2021 05:10:59 GMT</pubDate>
            <atom:updated>2022-12-17T15:45:21.544Z</atom:updated>
            <content:encoded><![CDATA[<h3>A Quick Start Guide to Java Unit Testing — Mockito and JUnit 5</h3><h4>A simple guide for junior developer who just started their journey</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*w-M_ODgweasL1JiFakM5ZA.jpeg" /><figcaption>Photo by me (taken at Thomson Nature Park)</figcaption></figure><p>Developers tend to have a love-hate relationship with unit testing. We all love that dopamine hit when we see all the green ticks ✔️ from test suite, but not everyone likes to write it.</p><p><a href="https://medium.com/javarevisited/5-best-junit-and-test-driven-development-books-for-java-developers-2d3fecb5c9ac">Unit testing </a>is a critical aspect of software engineering as it gives the following advantages to your development process:</p><ol><li>Boosts confidence in the <strong>reliability </strong>of your codebase</li><li>Enforces <strong>inversion of control/dependency injection</strong> pattern</li><li>Pushes developer to write <strong>good code</strong> (bad code is oftentimes difficult to test and interpret)</li><li>Protects your <strong>requirements</strong> and prevents others from accidentally breaking them.</li></ol><pre><strong>Table of Contents</strong><br> — Dependencies Setup<br> — Sample Project<br> — Test Naming Convention<br> — Writing Unit Test<br> — Mocking Service Object<br> — Argument Captor for Mocked Object<br> — Mocking Public Static Method<br> — Testing Private Method (Reflection)<br> — Review Test Coverage</pre><h3>Dependencies Setup</h3><p>To begin, we will use <strong>mockito-core</strong>, which is sufficient for mocking or partial mocking. Later, there will be examples where we will switch to using <strong>mokito-inline</strong>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/93e925c59bfb951ca04495b819835513/href">https://medium.com/media/93e925c59bfb951ca04495b819835513/href</a></iframe><p>Take note that we need at least <strong>Java 8</strong> for this sample project.</p><h3>Sample Project</h3><p>Let us imagine that we are building a <strong>simple message service</strong> that can send some string message via email service:</p><figure><a href="https://javarevisited.blogspot.com/2021/04/junit-interview-questions-with-answers.html"><img alt="" src="https://cdn-images-1.medium.com/max/227/1*N5lKZH6QzdcS4rgkTjGBjw.png" /></a><figcaption>Project Structure for This Demo</figcaption></figure><p>The <strong>MessageService </strong>class is the main service that provides 2 methods to send the email. For security purposes, the message service has the option to send the email with or without encryption. The method which provides encryption will use a different encryption method provided by <strong>EncryptionUtils </strong>based on the input length of the message:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fd78ae66c600ed7ada770a808ad5a194/href">https://medium.com/media/fd78ae66c600ed7ada770a808ad5a194/href</a></iframe><p><strong>EmailService </strong>is a simple service which serves as an <a href="https://javarevisited.blogspot.com/2010/10/abstraction-in-java.html">abstraction </a>to perform email functionality. The implementation is totally not usable and is only for this simple demo purpose:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3062eca0af10685189e601527c69069b/href">https://medium.com/media/3062eca0af10685189e601527c69069b/href</a></iframe><p><strong>EncryptionUtils </strong>provides static methods to perform encryption for the input messages:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b85ec9854a8649211ba303b70836d6b9/href">https://medium.com/media/b85ec9854a8649211ba303b70836d6b9/href</a></iframe><p><strong>EmailEntity </strong>is the wrapper class that wraps the message before passing it to the <strong>EmailService </strong>instance. The implementation of the wrapper class is very simple in this example; in the real world scenario it is very common to see more complicated classes with similar use case:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d7167397c50e61693757237974d28a42/href">https://medium.com/media/d7167397c50e61693757237974d28a42/href</a></iframe><h3>Test Naming Convention</h3><p>There are many common naming conventions when it comes to writing a test case; however, we will not go through all the conventions in this article. You may check them out here to see which is most suitable for your use case:</p><p><a href="https://dzone.com/articles/7-popular-unit-test-naming">7 Popular Strategies: Unit Test Naming Conventions</a></p><p>Personally, I prefer to use the format of <strong>given_when_then </strong>and name the method as comprehensively as possible, so that should the test are broken, it would takes less effort to understand what has been broken.</p><h3>Writing Unit Test</h3><p>Below is the basic setup for <strong>MessageServiceTest</strong>. We will use this class to demonstrate the following ideas:</p><ul><li>the idea of Mock and Argument Captor</li><li>asserting results</li><li>verifying method calls</li></ul><p>The class has an <strong>ExtendWith </strong>annotation and a specified <strong>MockitoExtension.class</strong> — this is to initialize Mockito behind the scenes. There are 3 more annotations in the class —</p><ul><li><a href="https://javarevisited.blogspot.com/2022/08/difference-between-mock-and-mockbean-in.html"><strong>Mock</strong></a>: This indicates that the object will be used for stubbing the results or verifying method calls</li><li><strong>Captor</strong>: This indicates that the <strong>ArgumentCaptor </strong>object is used to capture the argument values used by mocked object (in this case, by emailService)</li><li><strong>BeforeEach</strong>: This indicates that the init() method will be called each time before each test starts. There are a few more <strong>JUnit5 lifecycle</strong> annotations for your reference — <strong>BeforeAll</strong>, <strong>AfterEach </strong>and <strong>AfterAll</strong></li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cc9f90b1f767a600f42e3c255aa50cde/href">https://medium.com/media/cc9f90b1f767a600f42e3c255aa50cde/href</a></iframe><h3>Mocking Service Object</h3><p>Mocking is basically creating a controlled environment for you to<em> fake the response of your mocked object</em>; you are not initializing or calling the actual object. By using the <a href="https://javarevisited.blogspot.com/2022/08/difference-between-mock-and-mockbean-in.html"><strong>Mock </strong>annotation</a>, we are mocking the <strong>EmailService </strong>behavior.</p><p>The test below should be easily understood given the method name:</p><ul><li><strong>given </strong>that the emailService is active</li><li><strong>when </strong>we call the sendEmailMsg method</li><li><strong>then </strong>result from the method call should be true</li><li>we also verify that while the method is called, the send method from email service should also be called once</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9f39072447b2312fe75854c3902c0862/href">https://medium.com/media/9f39072447b2312fe75854c3902c0862/href</a></iframe><p>So if we did not mock the email service, the <strong>isServiceActive()</strong> method will return false:</p><pre>public boolean isServiceActive() {<br>    return false;<br>}</pre><p>By using <strong>Mockito.when</strong> and <strong>thenReturn</strong>, we <em>hijacked the behavior</em> of the mocked emailService. Moreover, we have visibility over how the mocked emailService is interacting throughout the whole method call, which allows us to <em>verify method call </em>for <strong>send() </strong>method<strong> </strong>in this case.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/14f7f0a40b99a314aeba2b745702445a/href">https://medium.com/media/14f7f0a40b99a314aeba2b745702445a/href</a></iframe><h3>Argument Captor for Mocked Object</h3><p>How do we verify that the emailEntity passing to <strong>emailService.send()</strong> via <strong>sendEmailMsg()</strong> is sending the expected message?</p><p>In this test, instead of expecting the send method to be any EmailEntity class, we swap it with our argument captor object. By doing so, after the method call is verified, we can call the <strong>getValue()</strong> method to retrieve the EmailEntity that is actually passed to the method. Therefore, we may perform further evaluation to see if the object is as expected:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e9b79148db86248cc0456b8312945760/href">https://medium.com/media/e9b79148db86248cc0456b8312945760/href</a></iframe><p>What about the <strong>sendEncryptedEmailMsg() </strong>method? Let us verify that the emailEntity passing to <strong>emailService.send()</strong> is indeed sending the encrypted message:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/598e0b1b863408aea3759769803a87a6/href">https://medium.com/media/598e0b1b863408aea3759769803a87a6/href</a></iframe><h3>Mocking Public Static Method</h3><p>Looking back at the <strong>sendEncryptedEmailMsg()</strong> method again:</p><pre>public boolean sendEncryptedEmailMsg(String message) {<br>    if (emailService.isServiceActive()) {<br>        String encryptedMsg;<br>        if (message.length() &lt; 15) {<br>            encryptedMsg = EncryptionUtils.<em>simpleEncrypt</em>(message);<br>        } else {<br>            encryptedMsg = EncryptionUtils.<em>notSoSimpleEncrypt</em>(message);<br>        }<br><br>        EmailEntity emailEntity = new EmailEntity();<br>        emailEntity.setMessage(encryptedMsg);<br>        emailService.send(emailEntity);<br>        return true;<br>    } else {<br>        return false;<br>    }<br>}</pre><p>Based on the message length, different encryptions will be used to process the message. But currently there is no way to mock a static method, right?</p><blockquote>Mockito-inline provides implementation for us to mock a public static method.</blockquote><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/226bd9c06d3e4a1cd42ca6e1b9a4c4f2/href">https://medium.com/media/226bd9c06d3e4a1cd42ca6e1b9a4c4f2/href</a></iframe><p>Now let us take it one step further with <strong>mockito-inline</strong> dependency, it provides <strong>MockedStatic </strong>class to mock any public static method. This is only supported after version 3.4.0. In the below examples, we can finally mock the <strong>EncryptionUtils </strong>class and all its methods:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/928009b72b5a3c1160222eeae00970a6/href">https://medium.com/media/928009b72b5a3c1160222eeae00970a6/href</a></iframe><h3>Testing Private Method (Reflection)</h3><p>Although testing private method is discouraged (you should probably refactor your code if you really need to test it), there are still techniques that allow us to do so.</p><p>To test the private method, we can use <strong>Java Reflection API</strong>. It allows runtime behaviour modification on methods and objects. In order to showcase this, let us now switch to <strong>EmailServiceTest </strong>class. Here is the basic setup for <strong>MessageServiceTest</strong>:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5fb9823984c23f39c7ee07ee1846f43c/href">https://medium.com/media/5fb9823984c23f39c7ee07ee1846f43c/href</a></iframe><p>The following test utilizes Reflection API to modify the accessor at runtime and allows us to invoke the previously private method:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/95329c7516aaa0610182fd4a0d4a54bf/href">https://medium.com/media/95329c7516aaa0610182fd4a0d4a54bf/href</a></iframe><blockquote>Take note that Reflection will have overhead when used, so we should try not to use it in our application code.</blockquote><p>You may read more about <strong>Java Reflection API</strong> here:</p><p><a href="https://www.baeldung.com/java-reflection">Guide to Java Reflection | Baeldung</a></p><h3>Review Test Coverage</h3><p>Test coverage provides a measurable, but non-exhaustive, metrics on your codebase:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/900/1*Z9azrrAT2QSvHXAS_SbyhA.png" /><figcaption>Run Test with Coverage (Intellij)</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/983/1*RiouQjzVwnXulny5rwUQxA.png" /><figcaption>All the Test Cases are Passed ✔️</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/396/1*z3AUkAWFC_DA5LjryyvmFA.png" /><figcaption>Test Coverage Results</figcaption></figure><p>These metrics can be incorporated into <strong>continuous integration (CI)</strong> that flags issues before or during integration process.</p><h3>Mocking Private Method…?</h3><p>If you have reached the point where you need to mock private method, it probably means you should consider refactoring the code. Remember to always refer to the <a href="https://www.baeldung.com/solid-principles">SOLID principle</a>. If for some reason that you are not allowed to modify the accessor or refactor the code, yet still want to mock private method and so on, do take a look at <a href="https://www.baeldung.com/intro-to-powermock">PowerMock </a>(highly discouraged).</p><p>I have uploaded all the examples on my repository if you need some references:</p><p><a href="https://github.com/JaysonGCS/Mockito-JUnit5-Simple-Examples">JaysonGCS/Mockito-JUnit5-Simple-Examples</a></p><p>If you are interested in generating TypeScript from Java Object, check out my next article:</p><p><a href="https://medium.com/javarevisited/generate-typescript-interfaces-from-java-classes-d53f8c590500">Generate TypeScript interfaces from Java Classes</a></p><p>Or perhaps you are keen to do more with less Java by utilising Lombok:</p><p><a href="https://medium.com/javarevisited/do-more-with-less-java-lombok-and-experimental-features-8d71d0bcb27c">Do More with Less Java — Lombok and Experimental Features</a></p><p><em>If you enjoy such stories, do consider becoming a </em><a href="https://jsongcs.medium.com/membership"><em>medium member</em></a><em>. It costs $5 per month and gives you unlimited access to Medium content. I’ll get a little commission if you sign up via </em><a href="https://jsongcs.medium.com/membership"><em>link</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=eebb49d7bcd3" width="1" height="1" alt=""><hr><p><a href="https://medium.com/javarevisited/a-quick-start-guide-to-java-unit-testing-mockito-and-junit-5-eebb49d7bcd3">A Quick Start Guide to Java Unit Testing — Mockito and JUnit 5</a> was originally published in <a href="https://medium.com/javarevisited">Javarevisited</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>