<?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: Mohammed Ghallab</title>
    <description>The latest articles on DEV Community by Mohammed Ghallab (@mohammed_ghallab_19c89665).</description>
    <link>https://dev.to/mohammed_ghallab_19c89665</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%2F3884916%2Fd9efffa3-26d8-40c1-b6d8-6b8f3766d053.jpg</url>
      <title>DEV Community: Mohammed Ghallab</title>
      <link>https://dev.to/mohammed_ghallab_19c89665</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mohammed_ghallab_19c89665"/>
    <language>en</language>
    <item>
      <title>Mastering Unit Testing in Spring Boot: How I Achieved 70%+ Coverage for Open Source</title>
      <dc:creator>Mohammed Ghallab</dc:creator>
      <pubDate>Fri, 17 Apr 2026 18:10:31 +0000</pubDate>
      <link>https://dev.to/mohammed_ghallab_19c89665/mastering-unit-testing-in-spring-boot-how-i-achieved-70-coverage-for-open-source-4dk5</link>
      <guid>https://dev.to/mohammed_ghallab_19c89665/mastering-unit-testing-in-spring-boot-how-i-achieved-70-coverage-for-open-source-4dk5</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhpik9k6tl45v69dcbuu5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhpik9k6tl45v69dcbuu5.png" alt=" " width="800" height="436"&gt;&lt;/a&gt;&lt;strong&gt;Testing is not about finding bugs; it's about gaining the confidence to ship code.&lt;br&gt;
Recently, while contributing to an Open Source project for GSSoC 2026, I took on the challenge of writing unit tests for a complex DecisionService. My goal was 70%+ coverage, but the journey was filled with NullPointerExceptions and mocking hurdles. Here is how I solved them.&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The Challenge
The DecisionService I was testing relied on multiple repositories and complex DTO mapping. When I first ran my tests, I kept hitting a wall:
java.lang.NullPointerException: Cannot invoke "OffsetDateTime.toInstant()" because ... is null&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This happened because the service was converting entity timestamps to Instants for the API response, but in a Mockito environment, these fields aren't automatically populated like they are in a real database.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The Technical Solution
To fix this, I had to ensure that every Mock object was fully initialized with the fields required by the Mapper. Here’s a snippet of the successful test setup:
&lt;/li&gt;
&lt;/ol&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;@DisplayName&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Should create decision successfully"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;createDecision_Success&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// 1. Prepare the Request&lt;/span&gt;
    &lt;span class="nc"&gt;CreateDecisionRequest&lt;/span&gt; &lt;span class="n"&gt;request&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;CreateDecisionRequest&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;
        &lt;span class="s"&gt;"Feature"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Body"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"repo-1"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;DecisionStatus&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;PROPOSED&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;of&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"java"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="o"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;// 2. Build the Mock Entity with required fields (Crucial Step!)&lt;/span&gt;
    &lt;span class="nc"&gt;Decision&lt;/span&gt; &lt;span class="n"&gt;savedDecision&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Decision&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;builder&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
            &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"dec-1"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
            &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;author&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mockUser&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
            &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;repo&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mockRepo&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
            &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;createdAt&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;OffsetDateTime&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;now&lt;/span&gt;&lt;span class="o"&gt;())&lt;/span&gt; &lt;span class="c1"&gt;// The fix for NPE!&lt;/span&gt;
            &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;status&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;DecisionStatus&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;PROPOSED&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
            &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;build&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

    &lt;span class="c1"&gt;// 3. Mock the behavior&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;decisionRepository&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;save&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;any&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Decision&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;class&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="n"&gt;savedDecision&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;// 4. Act &amp;amp; Assert&lt;/span&gt;
    &lt;span class="nc"&gt;DecisionResponse&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;decisionService&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;createDecision&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;currentUserId&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;response&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;title&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;"Feature"&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;ol&gt;
&lt;li&gt;Key Takeaways&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;Mocks are not Magic: If your service maps an entity to a DTO, your Mock entity must have all the fields the Mapper touches.&lt;/li&gt;
&lt;li&gt;Context Matters: In Spring Boot tests, pay attention to OffsetDateTime and relational objects like Repo or User.&lt;/li&gt;
&lt;li&gt;Clean over Complex: Using thenReturn() is often more readable and predictable than thenAnswer() for simple Unit Tests.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Call to Action&lt;/p&gt;

&lt;p&gt;Writing these tests increased the project's reliability and helped me understand the inner workings of Spring Boot even better.&lt;/p&gt;

&lt;p&gt;Are you working on GSSoC 2026? Let's connect!&lt;br&gt;
Check out my full contribution and other projects on my &lt;a href="https://github.com/MohammedGhallab" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;: &lt;a href="https://github.com/MohammedGhallab" rel="noopener noreferrer"&gt;MohammedGhallab&lt;/a&gt;.&lt;/p&gt;

&lt;h1&gt;
  
  
  java #springboot #testing #opensource
&lt;/h1&gt;

</description>
      <category>java</category>
      <category>testing</category>
      <category>webdev</category>
      <category>security</category>
    </item>
  </channel>
</rss>
