JSFUnit 问题 - EJB 未注入到正在测试的 servlet 中
在我的应用程序中,我有一个 EntryServlet,它加载默认数据并根据登录的用户 ID 设置安全性。该 servlet 通过依赖注入使用 EJB。当我将其作为普通 Web 应用程序执行时,它工作正常,但是当我尝试使用 JSFUnit 测试应用程序时,servlet 中的 ejb 引用未注入并且为 null。
我需要在设置 JSF 会话之前调用 EntryServlet,因为它会加载 jsf 页面中所需的数据。 我将其称为如下
JSFUnit 测试代码
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 代码
public class JSFUnitEntryServlet extends 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 没有在容器中执行。请在这方面提供帮助。任何帮助将不胜
感激 基兰
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在使用
new
来创建您的 servlet:我不明白注入是如何发生的,因为这里的 servlet 不是托管的(即由容器创建的)。
You're using a
new
to create your servlet:I don't see how injection could occur since the servlet is not managed (i.e. created by a container) here.