title: Null Pointer Exception JUnit Mockito
body: <p>I m new to JUnit and Mockitio. When I run the below code, I get,
  <em>java.lang.NullPointerException: Cannot invoke &quot;org.springframework.http.ResponseEntity.getStatusCodeValue()&quot; because the return value of &quot;com.learnit.testing.MyController.getUser(java.lang.Long)&quot; is null</em></p>
  <pre><code>@Controller
  @RequestMapping(&quot;/api&quot;)
  public class MyController {
  
      @Autowired
      private MyService service;
  
      @GetMapping(&quot;/&quot;)
      @ResponseBody
      public ResponseEntity&lt;Object&gt; getUser(Long id) throws Exception {
          return service.myResponse(id);
      }
  }
  
  @Service
  public class MyService {
      
      @Autowired
      private MyRepository repository;
  
      public ResponseEntity&lt;Object&gt; myResponse(Long id) throws Exception{
  
          MyData data=repository.findById(id).orElse(null);
          if(data!=null)
              return new ResponseEntity&lt;&gt;(HttpStatus.OK);
          return new ResponseEntity&lt;&gt;(HttpStatus.INTERNAL_SERVER_ERROR);
  
      }
  }
  </code></pre>
  <p><strong>Testing</strong></p>
  <pre><code>@ExtendWith(MockitoExtension.class)
  public class MyTest {
  
      @InjectMocks
      private MyController controller;
      
      @Mock
      private MyRepository repo;
      
      @Mock
      private MyService service;
      
      @Test
      public void checkService() throws Exception {
          when(repo.findById((long)1)).thenReturn(null);
          assertEquals(controller.getUser((long)1).getStatusCodeValue(), 500);
          
      }
  }
  </code></pre>