title: NullPointerException while running unit tests using Mockito
body: <p>While running Mockito unit test for my controller I am getting the following Exception. I realize that my <code>dataSource</code> is null here and hence the exception but not sure how to fix this problem. I am using Tomcat container for running this web application and JNDI context for the dataSource is defined in META-INF <code>context.xml</code>. Please guide.</p>
  
  <p><strong>Exception</strong></p>
  
  <pre><code>-------------------------------------------------------------------------------
  Test set: com.study.mockito.controllers.ProductControllerTest
  -------------------------------------------------------------------------------
  Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.28 sec &lt;&lt;&lt; FAILURE!
  testProcessRequest(com.study.mockito.controllers.ProductControllerTest)  Time elapsed: 0.25 sec  &lt;&lt;&lt; ERROR!
  java.lang.NullPointerException
      at com.study.mockito.dao.MasterDao.getProduct(MasterDao.java:21)
      at com.study.mockito.service.ProductService.getProduct(ProductService.java:21)
      at com.study.mockito.controllers.ProductController.processRequest(ProductController.java:37)
      at com.study.mockito.controllers.ProductController.doGet(ProductController.java:25)
      at com.study.mockito.controllers.ProductControllerTest.testProcessRequest(ProductControllerTest.java:50)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:606)
      at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
      at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
      at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
      at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
      at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
      at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
      at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
      at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
      at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
      at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
      at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
      at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
      at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
      at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
      at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:252)
      at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141)
      at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:606)
      at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
      at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
      at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
      at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
      at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)
  </code></pre>
  
  <p><strong>ProductControllerTest.java</strong></p>
  
  <pre><code>public class ProductControllerTest {
  
          private HttpServletRequest request;
          private HttpServletResponse response;
          private ProductController controller;
          private RequestDispatcher rd;
          private ServletContext appContext;
          private ProductService productService;
          private Product product;
  
          @Before
          public void setUp() {
              controller = new ProductController();
              request = mock(HttpServletRequest.class);
              response = mock(HttpServletResponse.class);
              rd = mock(RequestDispatcher.class);
              appContext = mock(ServletContext.class);
              productService = mock(ProductService.class);
              product = mock(Product.class);
          }
  
          @Test
          public void testProcessRequest() throws ServletException, IOException {
              when(productService.getProduct(anyInt())).thenReturn(product);
  
              when(request.getServletContext()).thenReturn(appContext);
              when(appContext.getRequestDispatcher(anyString())).thenReturn(rd);
  
  
              //make an actual call
              controller.doGet(request, response);
  
              //verify that the method is getting called
              verify(rd).forward(request, response);
          }
      }
  </code></pre>
  
  <p><strong>ProductController.java</strong></p>
  
  <pre><code>@WebServlet(name = "ProductController", urlPatterns = {"/product.htm"})
  public class ProductController extends HttpServlet {
  
      private static final int PRODUCT_ID = 301;
  
      @Resource(name = "jdbc/istore-db")
      protected DataSource dataSource;
  
      @Override
      protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          processRequest(request, response);
      }
  
      @Override
      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          processRequest(request, response);
      }
  
      public void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          System.out.println("Into the ProductController...");
  
          //fetch the product from the db based on productId parameter
          Product product = new ProductService(dataSource).getProduct(PRODUCT_ID);
          request.setAttribute("product", product);
  
          RequestDispatcher rd = request.getServletContext().getRequestDispatcher("/jsp/product.jsp");
          rd.forward(request, response);
      }
  
  }
  </code></pre>