title: NullPointer Exception everytime I try to run my Junit Test Case
body: <p>Every time i try to run the junit test case , i get null pointer exception and also when i tried to hadle the exception everytime the function returns NULL value. Here is the code.</p>
  
  <p><strong>RestaurantRepository.java</strong></p>
  
  <pre><code>package org.springsource.restbucks;
  
  import org.springframework.data.jpa.repository.Query;
  import org.springframework.data.repository.PagingAndSortingRepository;
  import org.springframework.data.repository.query.Param;
  import org.springframework.stereotype.Component;
  import org.springframework.transaction.annotation.Transactional;
  
  
  @Component
  @Transactional
  public interface RestaurantRepository extends PagingAndSortingRepository&lt;Restaurant, Long&gt; {
      @Query("SELECT p FROM Restaurant p Where (p.id)=(:Id) and p.deleted=false")
      Restaurant findById(@Param("Id") long Id);
  
      @Query("SELECT p FROM Restaurant p Where LOWER(p.name)=LOWER(:name) and p.deleted = false")
      Restaurant findByName(@Param("name") String name);
  }
  </code></pre>
  
  <p><strong>RestaurantController.java</strong></p>
  
  <pre><code>@RestController
  public class RestaurantController {
  @Autowired
      RestaurantRepository repository;
  @RequestMapping(value = "/restaurants/{id}", method = RequestMethod.GET, produces = {MediaType.APPLICATION_JSON_VALUE})
      @ResponseStatus(HttpStatus.OK)
      @ResponseBody
      public ResponseEntity&lt;Restaurant&gt; getRestaurant(@PathVariable("id") long restaurantId) {
          Restaurant responseRestaurant = repository.findOne(restaurantId);
          if(responseRestaurant==null){
              logger.error("No Restaurant found with id:"+restaurantId);
              return new ResponseEntity&lt;Restaurant&gt;(HttpStatus.NOT_FOUND);
          }else if(responseRestaurant.isDeleted()==true){
              logger.error("Restaurant Object is deleted");
              return new ResponseEntity&lt;Restaurant&gt;(HttpStatus.NOT_FOUND);
          }else{
              return new ResponseEntity&lt;Restaurant&gt;(responseRestaurant,HttpStatus.OK);
          }
      }
  
  }
  </code></pre>
  
  <p><strong>RestaurantRepositoryTest.java</strong></p>
  
  <pre><code>import static org.junit.Assert.assertEquals;
  import org.junit.Test;
  import org.springframework.beans.factory.annotation.Autowired;
  
  public class RestaurantRepositoryTest{
  
      @Autowired
      private RestaurantRepository repository;    
  
      @Test
      public void testfindById() throws Exception{ //testing findbyid method in restaurant repository
  
          Restaurant responseRestaurant = repository.findByName("xyz");   //getting error over here   
          assertEquals("xyz",responseRestaurant.getName());           
      }   
  }
  </code></pre>
  
  <p>I am getting null pointer exception whenever i run <strong>mvn test</strong> . the stack trace is below what i get displayed in terminal.</p>
  
  <ul>
  <li><p>testfindById(org.springsource.restbucks.RestaurantRepositoryTest) 
  Time elapsed: 0.018 sec  &lt;&lt;&lt; ERROR! java.lang.NullPointerException:
  null     at</p></li>
  <li><p>org.springsource.restbucks.RestaurantRepositoryTest.testfindById(RestaurantRepositoryTest.java:19)</p></li>
  </ul>
  
  <p>what shall i do to run my test successfully??</p>