title: Mockito Spring Boot gives Null Pointer Exception
body: <p>I am unable to run a unit test for my spring boot application at the Service Layer as it gives me null pointer exception at line <code>List&lt;Student&gt; expectedList = studentService.findAl();</code>. But if I invoked the mock directly it works <code>List&lt;Student&gt; expectedList = studentRepository.findAll();</code>. The studentRepository is inject in the studentService. Can anyone illustrate what the problem is?<br>
  TestClass:</p>
  
  <pre><code>import com.demo.student.demo.entity.Student;
  import com.demo.student.demo.repository.StudentRepository;
  import org.junit.jupiter.api.Test;
  import org.mockito.InjectMocks;
  import org.mockito.Mock;
  import static org.mockito.Mockito.*;
  
  import java.util.ArrayList;
  import java.util.List;
  
  import static org.junit.jupiter.api.Assertions.*;
  
  public class StudentServiceImplTest {
  
      @Mock
      private StudentRepository studentRepository;
  
      @InjectMocks
      private StudentServiceImpl studentService;
  
      @Test
      public void findAll(){
          List&lt;Student&gt; studentList = new ArrayList&lt;&gt;();
          studentList.add(new Student(1, "person1", "person1@mail.com"));
          studentList.add(new Student(2, "person2", "person2@mail.com"));
  
          when(studentRepository.findAll()).thenReturn(studentList);
  
          List&lt;Student&gt; expectedList = studentService.findAl();
  
          assertEquals(0, expectedList.size());
      }
  
  
  }
  </code></pre>
  
  <p>and the Repository:</p>
  
  <pre><code>@Repository
  public interface StudentRepository extends JpaRepository&lt;Student, Long&gt; {
  }
  </code></pre>
  
  <p>the service interface:</p>
  
  <pre><code>public interface StudentService {
  
      public List&lt;Student&gt; findAl();
      public Student findById(long id);
      public Student saveOrUpdate(Student student);
      public void deleteById(long id);
  
  }
  </code></pre>
  
  <p>and the service implementation:</p>
  
  <pre><code>@Service
  public class StudentServiceImpl implements StudentService {
  
      @Autowired
      private StudentRepository studentRepository;
  
      @Override
      public List&lt;Student&gt; findAl() {
          return studentRepository.findAll();
      }
  
      @Override
      public Student findById(long id) {
          Optional&lt;Student&gt; student = studentRepository.findById(id);
          boolean exist = student.isPresent();
          return exist? student.get() : null;
      }
  
      @Override
      public Student saveOrUpdate(Student student) {
          studentRepository.save(student);
          return student;
      }
  
      @Override
      public void deleteById(long id) {
          studentRepository.deleteById(id);
      }
  
  }
  </code></pre>