title: How to Test void Method with spring repository using Junit and Mockito
body: <p>i have a java spring service that call a spring data repository and i want to do a Junit test with mockito , this is my class and the service that i want to test :</p>
  
  <pre><code>@Service
  public class DataServiceImpl implements DataService{
  
      @Autowired
      private CautionRepository cautionRepository;
  
  
  
          @Override
          public void addCautions(List&lt;CautionsDTO&gt; cautions, Contrat contrat) {
  
              if(cautions != null &amp;&amp; !cautions.isEmpty()) {
                  cautions.forEach(caution -&gt; {
  
                          Caution caution = new Caution();
  
                          cautionContrat.setAmount(caution.getAmount());
                          cautionContrat.setDate(caution.getDate());
  
                          caution.setContrat(contrat);
  
                          cautionRepository.save(caution);
              });
              }
          }
      }
  </code></pre>
  
  <p>and this is my Unit test</p>
  
  <pre><code>@RunWith(SpringRunner.class)
  public class DataServiceImplTest{
  
  @InjectMocks
  private DataServiceImpl dataService;
  
  @Mock
  private CautionRepository cautionRepository;
  
      @Test
      public void addListCautionsTest() {
  
          List&lt;CautionsDTO&gt; cautions = new ArrayList&lt;&gt;(); 
          ContratExportation contrat = new ContratExportation();
  
          Caution caution = new Caution();
  
           dataDelService.addCautions(cautions,contrat);
  
          Mockito.verify(cautionRepository, times(1)).save(caution);
  
      }
  }
  </code></pre>
  
  <p>When i run the test i got the folowwing error : </p>
  
  <pre><code>Wanted but not invoked:
  cautionRepository.save(
      org.model.Caution@2abe9173
  );
  -&gt; at org.service.DataServiceImplTest.addListCautionsTest(DataServiceImplTest.java:292)
  Actually, there were zero interactions with this mock.
  </code></pre>
  
  <p>Do you have any idea please what is the mistake with my test</p>