title: Unit testing for spring boot RESTFUL Controller
body: <p>I am trying to do unit testing for spring boot RESTFUL controller but I am receiving a null pointer exception. The application has a <code>StudentController</code> which depends on a <code>StudentService</code>. 
  Here is the controller code: </p>
  
  <pre><code>package com.demo.student.demo.controller;
  
  import com.demo.student.demo.annotation.ApiDescription;
  import com.demo.student.demo.entity.Student;
  import com.demo.student.demo.entity.StudentDto;
  import com.demo.student.demo.service.StudentService;
  import io.swagger.annotations.Api;
  import io.swagger.annotations.ApiOperation;
  import org.modelmapper.ModelMapper;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.web.bind.annotation.*;
  import java.util.List;
  import java.util.stream.Collectors;
  
  @RestController
  @RequestMapping(value = "/v1/students")
  @Api(description = "This is a general description for StudentController RESTFUL Controller")
  public class StudentController {
  
      private StudentService studentService;
      private ModelMapper modelMapper;
  
      public StudentController(ModelMapper modelMapper, StudentService studentService){
          this.studentService = studentService;
          this.modelMapper = modelMapper;
      }
  
      private StudentDto convertToDto(Student student) {
          StudentDto studentDto = modelMapper.map(student, StudentDto.class);
          return studentDto;
      }
  
      private Student convertToEntity(StudentDto studentDto) {
          Student student = modelMapper.map(studentDto, Student.class);
          return student;
      }
  
      @GetMapping
      @ApiOperation("Returns a list of all students in the system.")
      @ApiDescription("FIND_ALL_STUDENTS.md")
      public List&lt;StudentDto&gt; findAll(){
          List&lt;Student&gt; students = studentService.findAl();
          return students.stream()
                  .map(student -&gt; convertToDto(student))
                  .collect(Collectors.toList());
      }
  
      @GetMapping("/{id}")
      @ApiOperation("Returns a specific Student by his/her identifier. 404 if does not exist.")
      @ApiDescription("FIND_STUDENT_BY_ID.md")
      public StudentDto findById(@PathVariable("id") long id){
          return convertToDto(studentService.findById(id));
      }
  
      @PostMapping
      @ApiOperation("Creates/Updates a new Student.")
      @ApiDescription("SAVE_OR_UPDATE_STUDENT.md")
      public StudentDto saveOrUpdate(@RequestBody StudentDto studentDto) {
          Student student = convertToEntity(studentDto);
          student.setUsername(student.getEmail() + "----" + student.getId());
          Student studentCreated = studentService.saveOrUpdate(student);
          return convertToDto(studentCreated);
      }
  
      @DeleteMapping("/{id}")
      @ApiOperation("Deletes a Student from the system. 404 if the Student's identifier is not found.")
      @ApiDescription("DELETE_STUDENT_BY_ID.md")
      public void deleteById(@PathVariable("id") long id){
          studentService.deleteById(id);
      }
  
  }
  </code></pre>
  
  <p>And here is the service implementation code: </p>
  
  <pre><code>package com.demo.student.demo.service;
  
  import com.demo.student.demo.entity.Student;
  import com.demo.student.demo.exception.StudentNotFoundException;
  import com.demo.student.demo.repository.StudentRepository;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Service;
  
  import java.util.List;
  
  
  @Service
  public class StudentServiceImpl implements StudentService {
  
      private StudentRepository studentRepository;
  
      @Autowired
      public StudentServiceImpl(StudentRepository studentRepository){
          this.studentRepository = studentRepository;
      }
  
      @Override
      public List&lt;Student&gt; findAl() {
          return studentRepository.findAll();
      }
  
      @Override
      public Student findById(long id) {
          return  studentRepository.findById(id).orElseThrow(() -&gt; new StudentNotFoundException(id));
      }
  
      @Override
      public Student saveOrUpdate(Student student) {
          studentRepository.save(student);
          return student;
      }
  
      @Override
      public void deleteById(long id) {
          if(this.findById(id) != null)
              studentRepository.deleteById(id);
      }
  
  }
  </code></pre>
  
  <p>And this is the test class for the controller: </p>
  
  <pre><code>package com.demo.student.demo.controller;
  
  import com.demo.student.demo.entity.Student;
  import com.demo.student.demo.service.StudentService;
  import com.demo.student.demo.service.StudentServiceImpl;
  import org.junit.jupiter.api.BeforeEach;
  import org.junit.jupiter.api.Test;
  import org.junit.runner.RunWith;
  import org.mockito.InjectMocks;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
  import org.springframework.boot.test.mock.mockito.MockBean;
  import org.springframework.test.web.servlet.MockMvc;
  
  import java.util.Arrays;
  import java.util.List;
  
  import static org.mockito.BDDMockito.given;
  import static org.mockito.Mockito.mock;
  import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
  import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
  
  @WebMvcTest(StudentController.class)
  class StudentControllerTest {
  
      private final static String URI = "/v1/students";
  
      @Autowired
      private MockMvc mockMvc;
  
      @MockBean
      private StudentService studentService;
  
      @Test
      void findAll() throws Exception {    
          // given
          Student student = new Student(1, "test", "test@test.test");
          List&lt;Student&gt; students = Arrays.asList(student);
          given(studentService.findAl()).willReturn(students);
  
          // when + then
          mockMvc.perform(get(URI))
                  .andExpect(status().isOk())
                  .andExpect(content().json("[{'id':1,'email':'test@test.test'}]"));
      }
  
  }
  </code></pre>
  
  <p>Here is also the pom.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
           xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"&gt;
      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
      &lt;parent&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
          &lt;version&gt;2.0.5.RELEASE&lt;/version&gt;
          &lt;relativePath/&gt; &lt;!-- lookup parent from repository --&gt;
      &lt;/parent&gt;
      &lt;groupId&gt;com.demo.student&lt;/groupId&gt;
      &lt;artifactId&gt;demo&lt;/artifactId&gt;
      &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
      &lt;name&gt;demo&lt;/name&gt;
      &lt;description&gt;Demo project for Spring Boot&lt;/description&gt;
  
      &lt;properties&gt;
          &lt;java.version&gt;1.8&lt;/java.version&gt;
      &lt;/properties&gt;
  
      &lt;dependencies&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-actuator&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-data-jpa&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;com.h2database&lt;/groupId&gt;
              &lt;artifactId&gt;h2&lt;/artifactId&gt;
              &lt;scope&gt;runtime&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.postgresql&lt;/groupId&gt;
              &lt;artifactId&gt;postgresql&lt;/artifactId&gt;
              &lt;scope&gt;runtime&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.projectlombok&lt;/groupId&gt;
              &lt;artifactId&gt;lombok&lt;/artifactId&gt;
              &lt;optional&gt;true&lt;/optional&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
              &lt;scope&gt;test&lt;/scope&gt;
              &lt;exclusions&gt;
                  &lt;exclusion&gt;
                      &lt;groupId&gt;org.junit.vintage&lt;/groupId&gt;
                      &lt;artifactId&gt;junit-vintage-engine&lt;/artifactId&gt;
                  &lt;/exclusion&gt;
              &lt;/exclusions&gt;
          &lt;/dependency&gt;
          &lt;!-- https://mvnrepository.com/artifact/org.modelmapper/modelmapper --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.modelmapper&lt;/groupId&gt;
              &lt;artifactId&gt;modelmapper&lt;/artifactId&gt;
              &lt;version&gt;0.7.5&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.plugin&lt;/groupId&gt;
              &lt;artifactId&gt;spring-plugin-core&lt;/artifactId&gt;
              &lt;version&gt;1.2.0.RELEASE&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;io.springfox&lt;/groupId&gt;
              &lt;artifactId&gt;springfox-swagger2&lt;/artifactId&gt;
              &lt;version&gt;2.8.0&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;io.springfox&lt;/groupId&gt;
              &lt;artifactId&gt;springfox-swagger-ui&lt;/artifactId&gt;
              &lt;version&gt;2.8.0&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;io.springfox&lt;/groupId&gt;
              &lt;artifactId&gt;springfox-bean-validators&lt;/artifactId&gt;
              &lt;version&gt;2.8.0&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;javax.xml&lt;/groupId&gt;
              &lt;artifactId&gt;jaxb-api&lt;/artifactId&gt;
              &lt;version&gt;2.1&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-engine --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.junit.jupiter&lt;/groupId&gt;
              &lt;artifactId&gt;junit-jupiter-engine&lt;/artifactId&gt;
              &lt;version&gt;5.2.0&lt;/version&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.javassist&lt;/groupId&gt;
              &lt;artifactId&gt;javassist&lt;/artifactId&gt;
              &lt;version&gt;3.25.0-GA&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.mockito&lt;/groupId&gt;
              &lt;artifactId&gt;mockito-core&lt;/artifactId&gt;
              &lt;version&gt;2.22.0&lt;/version&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.skyscreamer&lt;/groupId&gt;
              &lt;artifactId&gt;jsonassert&lt;/artifactId&gt;
              &lt;version&gt;1.5.0&lt;/version&gt;
          &lt;/dependency&gt;
      &lt;/dependencies&gt;
  
      &lt;build&gt;
          &lt;plugins&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
                  &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
              &lt;/plugin&gt;
          &lt;/plugins&gt;
      &lt;/build&gt;
  
  &lt;/project&gt;
  </code></pre>
  
  <p>When I run the test I receive a NULL POINTER EXCEPTION pointing to a code line in the <code>StudentController</code> class where the student service retrieves all the student data: <code>List&lt;Student&gt; students = studentService.findAl();</code></p>
  
  <pre><code>java.lang.NullPointerException: null
      at com.demo.student.demo.controller.StudentController.findAll
  </code></pre>
  
  <p>Can anyone tell me what is the problem?</p>