0

While running Mockito unit test for my controller I am getting the following Exception. I realize that my dataSource 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 context.xml. Please guide.

Exception

-------------------------------------------------------------------------------
Test set: com.study.mockito.controllers.ProductControllerTest
-------------------------------------------------------------------------------
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.28 sec <<< FAILURE!
testProcessRequest(com.study.mockito.controllers.ProductControllerTest)  Time elapsed: 0.25 sec  <<< 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)

ProductControllerTest.java

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);
        }
    }

ProductController.java

@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);
    }

}
durron597
  • 31,968
  • 17
  • 99
  • 158
Nital
  • 5,784
  • 26
  • 103
  • 195

1 Answers1

1

You are trying to mock ProductService, but when your controller calls ProductService.getProduct(int), it is creating its own new instance. You would need to make the ProductService a class variable and pass the mocked ProductService into the controller method to test.

You could add a setter method for the datasource in your controller class and pass a mocked data source in that way.

BamaPookie
  • 2,560
  • 1
  • 16
  • 21