Dependency injection JSFUnit问题-EJB未被注入正在测试的servlet
在我的应用程序中,我有一个EntryServlet,它加载默认数据并根据登录的用户id设置安全性。这个servlet通过依赖项注入使用EJB。当我将它作为一个普通的web应用程序执行时,它工作得很好,但是当我尝试使用JSFUnit测试应用程序时,servlet中的ejb引用没有被注入,并且是空的 我需要在设置JSF会话之前调用EntryServlet,因为它加载JSF页面中所需的数据。 我这样称呼它 JSF单元测试代码 公共类访问ScholarNewRequestTest扩展org.apache.cactus.ServletTestCase{Dependency injection JSFUnit问题-EJB未被注入正在测试的servlet,dependency-injection,ejb-3.0,jsfunit,Dependency Injection,Ejb 3.0,Jsfunit,在我的应用程序中,我有一个EntryServlet,它加载默认数据并根据登录的用户id设置安全性。这个servlet通过依赖项注入使用EJB。当我将它作为一个普通的web应用程序执行时,它工作得很好,但是当我尝试使用JSFUnit测试应用程序时,servlet中的ejb引用没有被注入,并且是空的 我需要在设置JSF会话之前调用EntryServlet,因为它加载JSF页面中所需的数据。 我这样称呼它 JSF单元测试代码 公共类访问ScholarNewRequestTest扩展org.apache
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没有在容器中执行。请在这方面提供帮助。任何帮助都将不胜感激
问候
Kiran您正在使用一个新的创建servlet:
//Calling EntryServlet
JSFUnitEntryServlet entryServlet = new JSFUnitEntryServlet();
我看不出注入是如何发生的,因为servlet在这里没有被管理(即由容器创建)。您正在使用
新的来创建servlet:
//Calling EntryServlet
JSFUnitEntryServlet entryServlet = new JSFUnitEntryServlet();
我不知道注入是如何发生的,因为servlet没有被管理(即,由容器创建)这里。谢谢Pascal,我现在启动了对servlet的浏览器请求,它正在由容器本身实例化。依赖项注入现在工作正常。谢谢Pascal,我现在启动了对servlet的浏览器请求,它正在由容器本身实例化。依赖项注入现在工作正常。