[英]Test method to add attribute to session fails
我測試了一種僅使用以下代碼向portletsession添加屬性的方法:
@Test
@PrepareForTest({ActionRequest.class, ActionResponse.class, LocalizedLoggerFactory.class})
public void test_addConfigCookiesFromSession() {
PowerMockito.mockStatic(LocalizedLoggerFactory.class);
ActionRequest request = PowerMockito.mock(ActionRequest.class);
PortletSession session = PowerMockito.mock(PortletSession.class);
when(request.getPortletSession()).thenReturn(session);
List<String> list = new ArrayList<String>();
list.add("prueba");
ConfigPortlet configPortlet = new ConfigPortlet();
configPortlet.addCookiesToSession(request, list);
assertNotNull(session.getAttribute("exemptCookiesListSession"));
}
在addcookiestosession內部,我只添加一個像這樣的列表:
PortletSession portletSession = request.getPortletSession();
portletSession.setAttribute("exemptCookiesListSession", listExemptCookies);
但是當我做斷言時,我在session.getattribute
有null ..我在做什么錯?
您的“會話”變量是一個模擬對象。 當您在模擬對象上調用“ setAttribute()”時,它將不會執行任何操作。 之后調用“ getAttribute()”將不會獲得您在調用中發送給“ setAttribute()”的值。
聽起來您需要的是這樣的(對mockito具有靜態導入):
verify(session).setAttribute(eq("exemptCookiesListSession"), anyList());
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.