title: How to write JUnit test cases with ignoring database activities
body: <p>The following is my test class</p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration("/ds-context.xml")
  @WebAppConfiguration
  public class PaidListTest {
  
      @Autowired
      PaymentService paymentService;
  
      @Test
      public void getPaidList() {
          List&lt;PaymentGetServiceDO&gt; response = null;
          try {
              response = paymentService.setPaidStatusList();          
              if(response != null &amp;&amp; response.size() &gt; 0){
                  for(int i = 0; i &lt; response.size(); i++){
                      assertNotNull(response.get(i).getAgentcode());
                  }
              }
  
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
  }
  </code></pre>
  
  <p>In the DAO layer of the service call <strong>paymentService.setPaidStatusList()</strong> have some operations with database activities of save and update, like <code>em.merge(renewalPoliciesDO);</code>  <br> But I don't want to execute them while calling the test method, they needs to get called only when actual business logic is called. <br>How can I restrict or rollback the database transactions here?<br>
  The service and DAO methods are tedious here. However, I have simplified them for your reference.<br> <strong><em>Service method</em></strong>
  <code>if(!updateList.isEmpty()){
  HashMap&lt;String,String&gt; recordset = new HashMap&lt;String,String&gt;();
      recordset = paymentDAO.setRenewalStatus(updateList);
  }</code><br><strong><em>DAO implementation</em></strong> <br></p>
  
  <pre><code>if(paymentUpdateResDO.getPaymentstatus().equalsIgnoreCase("MANUAL") &amp;&amp; 
  !responseUpdateStatus.getPolicystatusid().equals(renewedStatusId)){
  CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
  CriteriaQuery&lt;Integer&gt; payQuery = criteriaBuilder.createQuery(Integer.class);
  Root&lt;PaymentStatusDO&gt; payRoot = payQuery.from(PaymentStatusDO.class);
  payQuery.multiselect(payRoot.get("paymentstatusid"));
  payQuery.where(criteriaBuilder.equal(payRoot.get("paymentstatusdescription"), "Manual"));
  Integer paymentStatusId = em.createQuery(payQuery).getSingleResult();
  insertOldPolicy(responseUpdateStatus);
  responseUpdateStatus.setNewpolicyno(paymentUpdateResDO.getNewpolicyno());
  responseUpdateStatus.setPreviousstatusid(responseUpdateStatus.getPolicystatusid());
  responseUpdateStatus.setPolicystatusid(renewedStatusId);
  Timestamp modifiedDate = new Timestamp(System.currentTimeMillis());
  responseUpdateStatus.setModifieddatetime(modifiedDate);
  responseUpdateStatus.setModifiedby("RPA");
  responseUpdateStatus.setPaymentstatusid(paymentStatusId);
  responseUpdateStatus.setActiveindicator("Y");
  em.merge(responseUpdateStatus);
  successRecords++;
  }
  </code></pre>
  
  <p>In my case, I need the result arraylists, but the <code>em.merge</code> and <code>em.persist</code> activities need to be ignored.</p>
  
  <p>When I try with <code>MockitoJUnitRunner</code> as <code>@GauravRai1512</code> preferred, I get my testcase executed but the program is terminated so that I am unable to get the result ArrayLists.<br><br>
  <a href="https://i.stack.imgur.com/TGRx5.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/TGRx5.png" alt="Screenshot for successful execution of testcase"></a> <br> <strong><em>Refer this image</em></strong> <a href="https://i.stack.imgur.com/QR4Le.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/QR4Le.png" alt="Program terminated, so that nothing got executed"></a></p>