【问题标题】:JSFUnit issue - EJB not getting injected into servlet being testedJSFUnit 问题 - EJB 未注入正在测试的 servlet
【发布时间】:2010-11-03 17:29:39
【问题描述】:

在我的应用程序中,我有一个 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 没有在容器中执行。请在这方面提供帮助。任何帮助将不胜感激

问候 基兰

【问题讨论】:

    标签: dependency-injection ejb-3.0 jsfunit


    【解决方案1】:

    您正在使用 new 创建您的 servlet:

    //Calling EntryServlet
    JSFUnitEntryServlet entryServlet = new JSFUnitEntryServlet();
    

    我看不出注入是如何发生的,因为这里没有管理 servlet(即由容器创建)。

    【讨论】:

    • 感谢 Pascal,我现在正在向 servlet 发起一个浏览器请求,并且它正在被容器本身实例化。依赖注入现在工作正常。
    猜你喜欢
    • 1970-01-01
    • 2015-01-17
    • 2014-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多