在我的应用程序中,我有一个EntryServlet,它加载默认数据并根据登录的用户id设置安全性。这个servlet通过依赖注入使用EJB。当我将其作为普通web应用程序执行时,它工作得很好,但是当我尝试使用JSFUnit测试该应用程序时,servlet中的ejb引用没有被注入,并且为空。
在设置JSF会话之前,我需要调用EntryServlet,因为它加载了jsf页面中所需的数据。我将其命名为
JSFUnit测试代码
公共类VisitingScholarNewRequestTest扩展了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代码
公共类JSFUnitEntryServlet扩展了HttpServlet { @EJB私有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.正如我前面提到的,当我在浏览器中执行应用程序时,它工作得很好,并且EJB被注入到EntryServlet中,但是当我运行JSFUnit测试套件时,它不会被注入。由于某些原因,servlet不能在容器中执行。请在此regard.Any帮助中提供帮助将非常感谢
向Kiran致敬
发布于 2010-11-08 19:57:45
您正在使用new创建servlet:
//Calling EntryServlet
JSFUnitEntryServlet entryServlet = new JSFUnitEntryServlet();我不明白注入是如何发生的,因为servlet在这里不是托管的(即由容器创建的)。
https://stackoverflow.com/questions/4089819
复制相似问题