首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JSFUnit问题-未将EJB注入到正在测试的servlet中

JSFUnit问题-未将EJB注入到正在测试的servlet中
EN

Stack Overflow用户
提问于 2010-11-04 01:29:40
回答 1查看 359关注 0票数 1

在我的应用程序中,我有一个EntryServlet,它加载默认数据并根据登录的用户id设置安全性。这个servlet通过依赖注入使用EJB。当我将其作为普通web应用程序执行时,它工作得很好,但是当我尝试使用JSFUnit测试该应用程序时,servlet中的ejb引用没有被注入,并且为空。

在设置JSF会话之前,我需要调用EntryServlet,因为它加载了jsf页面中所需的数据。我将其命名为

JSFUnit测试代码

公共类VisitingScholarNewRequestTest扩展了org.apache.cactus.ServletTestCase {

代码语言:javascript
复制
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;

代码语言:javascript
复制
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致敬

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-11-08 19:57:45

您正在使用new创建servlet:

代码语言:javascript
复制
//Calling EntryServlet
JSFUnitEntryServlet entryServlet = new JSFUnitEntryServlet();

我不明白注入是如何发生的,因为servlet在这里不是托管的(即由容器创建的)。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4089819

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档