簡體   English   中英

StrutsSpringTestCase - 幾種上下文 - 如何按順序正確實例化它們

[英]StrutsSpringTestCase - Several contexts - How to instantiate them properly in order

我正在為使用 JUnit 的使用 Struts2、Spring、Hibernate 的項目編寫集成測試用例。

我的測試類擴展了StrutsSpringTestCase 應用程序需要登錄/會話來調用任何操作。 以下是代碼:

@Test
public void testGetActionProxy() throws Exception {
    
    ActionProxy proxy;
    String result;
    ActionContext.getContext().setSession(createUserSession()); // Not sure if this is needed here. But in order to get the session working, I need this.

    proxy = initAction("cInfo");
    assertNotNull(proxy);
        
    CustInfo action = (CustInfo) proxy.getAction();
    result = proxy.execute();
    assertEquals(Action.SUCCESS, result);
}

initAction()方法:

private ActionProxy initAction(String actionUri) {
    ActionProxy proxy = getActionProxy(actionUri);
    
    ActionContext.setContext(proxy.getInvocation().getInvocationContext());  // I tried this line of code to get the ServletActionContext.getMapping().getName() to work. But no use.
    
    ActionContext actionContext = proxy.getInvocation().getInvocationContext();
    actionContext.setSession(createUserSession()); // This is for setting a session
    return proxy;
 }

在它遇到這個方法之前,它會加載所有的配置文件。 struts.xml jpaContext.xmlbeans.xml等。

我的 Action 類CustInfo實現了ServletRequestAware並且它有一個方法getActionName作為行:

 return ServletActionContext.getActionMapping().getName();

當我調用result = proxy.execute();時會調用它result = proxy.execute(); . 所以請求失敗。

問題 1 :為什么它返回null 我認為ServletActionContext是自動啟動的,所以它應該返回一個值。 但它不是。 如果未初始化,初始化的正確位置在哪里以及如何初始化?

我在getActionProxy調用后嘗試了以下操作。 但它仍然沒有奏效。

ServletActionContext.setContext(proxy.getInvocation().getInvocationContext());

問題 2 :要設置會話,在getActionProxy()之前,我必須調用,

ActionContext.getContext().setSession(createUserSession());

再次,在getActionProxy之后

ActionContext actionContext = proxy.getInvocation().getInvocationContext();
actionContext.setSession(createUserSession());

設置會話。 我想,這里有問題。

問題 3 :看起來,這里有幾個上下文在起作用: applicationContextActionContext ServletContextServletActionContext

當我的測試類擴展StrutsSpringTestCase類時,我猜applicationContext已初始化。 但我不確定其他情況。 在哪里初始化它們?

編輯:

源代碼中的進一步調查揭示了一個問題。當我調用ServletActionContext.getActionMapping() ,它在內部調用ActionContextget()方法。

public Object get(String key) {
    return context.get(key);
}

context是一個對象的映射,它在其中尋找不存在的鍵struts.actionMapping值。 因此,返回null 但不確定它為什么在那里。 它不是空的。 它還有其他鍵/值。

你的問題的答案:

ServletActionContext.getActionMapping()從動作上下文返回映射,如果它沒有設置,那么你得到null

您不應該手動設置會話,執行操作時會創建會話。

不要ActionContext不同的類ActionContextServletContextServletActionContext 您不應該對初始化這些對象做任何事情,因為它是由超類StrutsSpringTestCase完成的。

public void testGetActionMapping() {
    ActionMapping mapping = getActionMapping("/cInfo.action");
    assertNotNull(mapping);
    assertEquals("/", mapping.getNamespace());
    assertEquals("cInfo", mapping.getName());
}

public void testGetActionProxy() throws Exception {        
    ActionProxy proxy = getActionProxy("/cInfo.action");
    assertNotNull(proxy);

    CustInfo action = (CustInfo) proxy.getAction();
    assertNotNull(action);

    String result = proxy.execute();
    assertEquals(Action.SUCCESS, result);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM