0

I am new to Mockito and trying to unit test my servlets using mockito and junit but hitting following exception. Can someone please guide.

pom.xml

  <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-all</artifactId>
            <version>1.10.19</version>
            <scope>test</scope>
        </dependency>

Exception

-------------------------------------------------------------------------------
Test set: com.istore.web.controllers.ProductPageControllerTest
-------------------------------------------------------------------------------
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.082 sec <<< FAILURE!
testProcessRequest(com.istore.web.controllers.ProductPageControllerTest)  Time elapsed: 0.02 sec  <<< ERROR!
java.lang.ExceptionInInitializerError
    at com.istore.web.controllers.ProductPageControllerTest.setUp(ProductPageControllerTest.java:26)
    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.RunBefores.evaluate(RunBefores.java:27)
    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)
Caused by: java.util.MissingResourceException: Can't find bundle for base name javax.servlet.LocalStrings, locale en_US
    at java.util.ResourceBundle.throwMissingResourceException(ResourceBundle.java:1499)
    at java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1322)
    at java.util.ResourceBundle.getBundle(ResourceBundle.java:721)
    at javax.servlet.GenericServlet.<clinit>(GenericServlet.java:95)
    ... 30 more

ProductPageControllerTest.java

public class ProductPageControllerTest {

    private ProductPageController controller;
    private HttpServletRequest request;
    private HttpServletResponse response;
    private ProductService productService;
    private Product product;
    private static final String PRODUCT_ID = "301";

    @Before
    public void setUp() {
        controller = new ProductPageController();
        request = mock(HttpServletRequest.class);
        response = mock(HttpServletResponse.class);
        productService = mock(ProductService.class);
        product = mock(Product.class);
    }

    @Test
    public void testProcessRequest() throws ServletException, IOException {
        when(request.getParameter("productId")).thenReturn(PRODUCT_ID);
        when(productService.getProduct(new Integer(PRODUCT_ID))).thenReturn(product);

        controller.processRequest(request, response);

        assertEquals("text/html", response.getContentType());
    }
}

BasePageController.java

public abstract class BasePageController extends HttpServlet {

    private static final Logger LOG = Logger.getLogger(BasePageController.class);

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

    //to be implemented by all child classes
    public abstract void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException;

    protected void showPage(HttpServletRequest request, HttpServletResponse response, String viewName) throws ServletException, IOException {
        LOG.debug("Displaying " + viewName + " page now...");
        request.getServletContext().getRequestDispatcher(viewName).forward(request, response);
    }

ProductPageController.java

@WebServlet(name = "ProductPageController", urlPatterns = {AppConstants.PRODUCT_PAGE_CONTROLLER})
public class ProductPageController extends BasePageController {

    private static final Logger LOG = Logger.getLogger(ProductPageController.class);
    private static final String PRODUCT_ID = "productId";

    @Override
    public void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        LOG.debug("Into the ProductPageController...");

        //fetch the product from the db based on productId parameter
        String productId = StringUtils.trimToEmpty(request.getParameter(PRODUCT_ID));
        request.setAttribute("product", new ProductService(dataSource).getProduct(new Integer(productId)));

        //show the page now
        showPage(request, response, PRODUCT_PAGE_URL);
    }
}
2
  • you can try this stackoverflow.com/questions/23386234/… Commented Apr 16, 2015 at 20:16
  • Thanks for the pointer :) Adding javax.servlet-api pom dependency helped me resolve this issue. Will answer my own question shortly. Commented Apr 16, 2015 at 20:31

1 Answer 1

3

Add the following javax.servlet-api dependency resolved the issue.

pom.xml updated

<dependency>
    <groupId>javax</groupId>
    <artifactId>javaee-web-api</artifactId>
    <version>7.0</version>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.1.0</version>
</dependency>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.