Question

In my application I have an EntryServlet which loads default data and sets up the security based on the logged in user id. This servlet is using EJB's through dependency injection. It works fine when I execute it as a normal web application but when I try to test the application using JSFUnit the ejb reference in the servlet is not injected and is null.

I need to invoke the EntryServlet before setting up the JSF session since it loads the data required in the jsf pages. I am calling it as follows

JSFUnit test code

public class VisitingScholarNewRequestTest extends org.apache.cactus.ServletTestCase {

public static Test suite() {
   return new TestSuite( VisitingScholarNewRequestTest.class );
}



public void testSaveAsDraft() throws ServletException,IOException {
    //Calling EntryServlet
    JSFUnitEntryServlet entryServlet = new JSFUnitEntryServlet();
    request.setAttribute("netid","KS2316");
    entryServlet.doPost(request,response); -- this is giving an error.
    // Send an HTTP request for the initial page
    JSFSession jsfSession = new JSFSession("/faces/cardrequestcreate.xhtml");

    // A JSFClientSession emulates the browser and lets you test HTML
    JSFClientSession client = jsfSession.getJSFClientSession();

    // A JSFServerSession gives you access to JSF state      
    JSFServerSession server = jsfSession.getJSFServerSession();

    System.out.println("current view id :"+server.getCurrentViewID());

    // Test navigation to initial viewID
    assertEquals("/cardrequestcreate.xhtml", server.getCurrentViewID());

}

EntryServlet code

public class JSFUnitEntryServlet extends HttpServlet { @EJB private MasterDataLocal masterDataFacade;

public void doGet(HttpServletRequest request,
                  HttpServletResponse response) throws ServletException,
                                                       IOException {
    doPost(request,response);
}

public void doPost(HttpServletRequest request,
                   HttpServletResponse response) throws ServletException,
                                                        IOException {
    String netId = "";
    try {
        netId = (String)request.getAttribute("netid");
        //Establish the portal connection for this user
        masterDataFacade.reinstateUser(netId); -- The test is failing with a null pointer exception here.

As I mentioned earlier When I execute the application in the browser it works fine and the EJB is injected in the EntryServlet, but it is not getting injected when I run the JSFUnit test suite. For some reason the servlet is not getting executed in the container. Please help in this regard.Any help will be greatly appreciated

Regards Kiran

Was it helpful?

Solution

You're using a new to create your servlet:

//Calling EntryServlet
JSFUnitEntryServlet entryServlet = new JSFUnitEntryServlet();

I don't see how injection could occur since the servlet is not managed (i.e. created by a container) here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top