在我的应用程序中,我有一个 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 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.
正如我之前提到的,当我在浏览器中执行应用程序时,它工作正常,并且 EJB 被注入到 EntryServlet 中,但是当我运行 JSFUnit 测试套件时它没有被注入。由于某种原因,servlet 没有在容器中执行。请在这方面提供帮助。任何帮助将不胜感激
问候基兰