title: NullPointerException thrown when Mocking a service method call (from an internal forEach loop), what is the correct way to implement this mock?
body: <p>I have a very basic Spring Boot application.<br>
  It consists of a <code>Controller</code> a <code>Service</code> and a <code>Repository</code>.<br>
  I want to test the <code>Controller</code> which basically just calls the <code>Service</code>, which in turn calls the <code>Repository</code>.<br>
  I'm trying to mock the <code>Service</code> and inject it into the <code>Controller</code> for testing, but it fails at this row:   </p>
  
  <pre><code>when(service.findEntry(1L)).thenReturn(model);
  </code></pre>
  
  <p>The error is <code>NullPointerException</code> and when I go through the stack trace it's because of a <code>HashSet.forEach()</code> call.<br>
  How can I fix this? In the row above the method <code>service.findEntry(1L)</code> is actually called and throws a <code>NullPointerException</code>
  The Error:   </p>
  
  <pre><code>java.lang.NullPointerException
      at com.efl.journal.controller.JournalEntryControllerITests.testGetJournalEntryById(JournalEntryControllerITests.java:46)
      at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.base/java.lang.reflect.Method.invoke(Method.java:566)
      at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
      at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
      at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
      at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
      at org.mockito.internal.runners.DefaultInternalRunner$1$1.evaluate(DefaultInternalRunner.java:44)
      at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
      at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
      at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
      at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
      at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
      at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
      at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
      at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
      at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
      at org.mockito.internal.runners.DefaultInternalRunner$1.run(DefaultInternalRunner.java:74)
      at org.mockito.internal.runners.DefaultInternalRunner.run(DefaultInternalRunner.java:80)
      at org.mockito.internal.runners.StrictRunner.run(StrictRunner.java:39)
      at org.mockito.junit.MockitoJUnitRunner.run(MockitoJUnitRunner.java:163)
      at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
      at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
      at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
      at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
      at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
  </code></pre>
  
  <p>My Test Class:   </p>
  
  <pre><code>package com.efl.journal.controller;
  
  import com.efl.journal.model.JournalEntryModel;
  import com.efl.journal.service.JournalEntryService;
  import org.junit.Test;
  import org.junit.runner.RunWith;
  import org.mockito.InjectMocks;
  import org.mockito.junit.MockitoJUnitRunner;
  import org.springframework.boot.test.mock.mockito.MockBean;
  
  import javax.ws.rs.core.Response;
  
  
  import java.util.HashSet;
  
  import static org.junit.jupiter.api.Assertions.assertEquals;
  import static org.mockito.Mockito.when;
  
  @RunWith(MockitoJUnitRunner.class)
  public class JournalEntryControllerITests {
  
      @MockBean
      JournalEntryService service;
  
      @InjectMocks
      JournalEntryController controller;
  
      @Test
      public void testGetJournalEntryById(){
          JournalEntryModel model = new JournalEntryModel();
          model.setEntryId(1L);
          model.setSubject("test 1");
          model.setBody("This is a test");
          model.setUserId(1L);
          model.setTags(new HashSet&lt;&gt;());
  
          when(service.findEntry(1L)).thenReturn(model);
          Response result = controller.getJournalEntry(1L);
          JournalEntryModel resultModel = (JournalEntryModel) result.getEntity();
  
          assertEquals(model.getEntryId(), resultModel.getEntryId());
      }
  }
  </code></pre>
  
  <p>My <code>Controller</code>: </p>
  
  <pre><code>package com.efl.journal.controller;
  
  import com.efl.journal.model.JournalEntryModel;
  import com.efl.journal.service.JournalEntryService;
  import lombok.extern.slf4j.Slf4j;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.web.bind.annotation.*;
  
  import javax.ws.rs.core.Response;
  import java.util.List;
  
  @RestController
  @RequestMapping("/")
  @Slf4j
  public class JournalEntryController {
  
      JournalEntryService entryService;
  
      @Autowired
      public JournalEntryController(JournalEntryService entryService){
          this.entryService = entryService;
      }
  
  
      @GetMapping("entry/{entryId}")
      public Response getJournalEntry(@PathVariable Long entryId){
          JournalEntryModel journalEntry =  entryService.findEntry(entryId);
          if( journalEntry != null){
              return Response.status(Response.Status.OK)
                      .entity(journalEntry)
                      .build();
          }
  
          return Response.status(Response.Status.NO_CONTENT)
                      .build();
      }
  
      @GetMapping("entry")
      public Response getAllJournalEntires(){
          List&lt;JournalEntryModel&gt; entryList = entryService.findAllEntryObjects();
  
          if(entryList.isEmpty()){
              return Response.status(Response.Status.NO_CONTENT).build();
          }
          return Response.status(Response.Status.OK).entity(entryList).build();
      }
  
      @PostMapping("entry")
      public Response postJournalEntry(@RequestBody JournalEntryModel entry) {
          entry = entryService.saveJournalEntry(entry);
          return Response.status(Response.Status.OK)
                  .entity(entry)
                  .build();
      }
  
      @DeleteMapping("entry/{entryId}")
      public Response deleteJournalEntry(@PathVariable Long entryId, @RequestBody JournalEntryModel entry ){
          entryService.deleteJournalEntry(entry);
          return Response.status(Response.Status.NO_CONTENT)
                  .build();
      }
  }
  </code></pre>
  
  <p>My <code>Service</code> class: </p>
  
  <pre><code>package com.efl.journal.service;
  
  import com.efl.journal.entity.JournalEntry;
  import com.efl.journal.model.JournalEntryModel;
  import com.efl.journal.repository.JournalEntryRepository;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Service;
  
  import java.util.ArrayList;
  import java.util.Iterator;
  import java.util.List;
  import java.util.Optional;
  
  @Service
  public class JournalEntryService {
  
      private JournalEntryRepository journalEntryRepo;
  
      @Autowired
      public JournalEntryService(JournalEntryRepository journalEntryRepo){
          this.journalEntryRepo = journalEntryRepo;
      }
  
      public JournalEntryModel findEntry(Long id) {
          Optional&lt;JournalEntry&gt; optional = journalEntryRepo.findById(id);
          if(optional.isPresent()){
              return optional.get().toModel();
          }
  
          return null;
  
      }
  
      public List&lt;JournalEntryModel&gt; findAllEntryObjects(){
          Iterable&lt;JournalEntry&gt; iterable = journalEntryRepo.findAll();
          Iterator iterator = iterable.iterator();
  
          List&lt;JournalEntryModel&gt; list = new ArrayList&lt;&gt;();
  
          iterator.forEachRemaining(e -&gt; {
                 list.add(((JournalEntry) e).toModel());
          });
  
          return list;
      }
  
      public JournalEntryModel saveJournalEntry(JournalEntryModel entry){
          return journalEntryRepo.save(entry.toEntity()).toModel();
      }
  
      public void deleteJournalEntry(JournalEntryModel entry){
          journalEntryRepo.delete(entry.toEntity());
      }
  
      public List&lt;JournalEntryModel&gt; findEntriesbyUserId(Long userId){
          return null;
      }
  
  }
  </code></pre>
  
  <p>My JournalEntity class:  </p>
  
  <pre><code>package com.efl.journal.entity;
  
  import com.efl.journal.model.JournalEntryModel;
  import com.efl.journal.model.TagModel;
  import lombok.Getter;
  import lombok.Setter;
  import org.hibernate.annotations.ColumnDefault;
  import org.hibernate.annotations.CreationTimestamp;
  
  import javax.persistence.*;
  import java.io.Serializable;
  import java.time.LocalDateTime;
  import java.util.HashSet;
  import java.util.Set;
  
  @Entity
  @Table(name = "entry")
  @Getter
  @Setter
  public class JournalEntry implements Serializable {
  
      @GeneratedValue
      @Id
      private Long entryId;
      private Long userId;
      private String subject;
      private String body;
      @CreationTimestamp
      @ColumnDefault("CURRENT_TIMESTAMP")
      private LocalDateTime localDateTime;
      @ManyToMany(cascade = {CascadeType.ALL}, fetch = FetchType.LAZY)
      @JoinTable(
              name = "journal_entry_tags",
              joinColumns = {@JoinColumn(name="entry_id")},
              inverseJoinColumns = {@JoinColumn(name= "tag_id")}
      )
      private Set&lt;Tag&gt; tags = new HashSet&lt;&gt;();
  
      public JournalEntry(){
      }
  
      public JournalEntryModel toModel(){
          JournalEntryModel entry = new JournalEntryModel();
          entry.setEntryId(entryId);
          entry.setUserId(userId);
          entry.setSubject(subject);
          entry.setBody(body);
          entry.setLocalDateTime(localDateTime);
          Set&lt;TagModel&gt; tagModels = new HashSet&lt;&gt;();
          tags.forEach(t-&gt;{
              tagModels.add(t.toModel());
          });
          entry.setTags(tagModels);
  
          return entry;
      }
  }
  </code></pre>
  
  <p>I think the error occurs with the call of <code>JournalEntity.toModel()</code> method call. But I don't understand why this method is actually called when it's part of the code in the method I'm trying to mock.  </p>