title: Junit - Testing a method that uses Foreach
body: <p>How do I write a unit for the method below? </p>
  
  <p>Basically the <strong>planDetailRepository.findOnGoingModules</strong> method returns a list of <strong>UserStatus</strong> entity
  then apply forEach method on that list setting the status to 1 then save the current <strong>UserStatus</strong> entity
  to the database.</p>
  
  <p>My idea of testing this is to verify the <strong>UserStatusRepository</strong> save method number of times it's been called.</p>
  
  <p><strong>planDetailRepository.findOnGoingModules:</strong></p>
  
  <pre><code>@Query("SELECT a FROM UserStatus a join a.planDetail b where b.startDate = CURRENT_DATE AND a.userPlan.id = (SELECT c.id from UserPlan c WHERE c.plan.id = :plan_id and c.user.id = :user_id)")
      List&lt;UserStatus&gt; findOngoingModules(@Param("plan_id") Integer plan_id, @Param("user_id") Integer user_id);
  </code></pre>
  
  <p>The method to be tested:</p>
  
  <pre><code>public void updateModuleStatusPerPlan(planId, userId){
          planDetailRepository.findOngoingModules(planId, userId)
                  .forEach(userstatus -&gt; {
                      userstatus.setStatus(1);
                      userStatusRepository.save(userstatus);
                  });
      }
  </code></pre>