在为我的控制器运行Mockito单元测试时,我收到以下异常。我意识到我的dataSource
在这里为空,因此异常但不确定如何解决这个问题。我正在使用Tomcat容器来运行此Web应用程序,并且在META-INF context.xml
中定义了dataSource的JNDI上下文。请指导。
异常
-------------------------------------------------------------------------------
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);
}
}
答案 0 :(得分:1)
您正在尝试模拟ProductService,但是当您的控制器调用ProductService.getProduct(int)时,它正在创建自己的新实例。您需要使ProductService成为一个类变量,并将模拟的ProductService传递给控制器方法进行测试。
您可以在控制器类中为数据源添加setter方法,并以这种方式传递模拟数据源。