samedi 28 mars 2015

Unit testing for struts Action with Mockito

Thanks for any help!


In our project are used: Java 6, Struts 1.2, Spring and JUnit4 (Mockito 1.9.5)


I want to make my first test class with Mockito. What code from this class should I test in my test class?



public class AdminViewSqlStatisticsAction extends AdminUserAction {

protected String findForward(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
throws JspException, IllegalStateException {

// First check everything was ok up the hierarchy
String forward = getForward(mapping, form, request, response);

if (null != forward) {
return forward;
}

HttpSession session = request.getSession();
ServletContext servletContext = (ServletContext) session.getServletContext();
//WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
WebApplicationContext wac = getWebApplicationContext(servletContext);
FantasyService fantasyService = (FantasyService) wac.getBean("fantasyService");
SessionFactory sessionFactory = fantasyService.getSessionFactory();
Statistics statistics = sessionFactory.getStatistics();

Date created = FantasyService.created;
Date cacheEmptied = FantasyService.cacheEmptied;
long cacheEmptiedSeconds = (new java.util.Date().getTime() - FantasyService.cacheEmptied.getTime()) / 1000;
Date currentDate = new Date();
Collection<OpenStatement> openStatements = SQLProfiler.getOpenStatements();
Collection<StoredObject> pageStatistics = ObjectStore.getStore("Page Profile").values();
long pageStatisticsSeconds = (new Date().getTime() - ((LRUCache) ObjectStore.getStore("Page Profile")).getClearDate()
.getTime()) / 1000;
boolean isScoringRunning = fantasyService.isScoringRunning();
String[] statisticsEntityName = statistics.getEntityNames();

long max_memory = Runtime.getRuntime().maxMemory() / (1024 * 1024);
long total_memory = Runtime.getRuntime().totalMemory() / (1024 * 1024);
long free_memory = Runtime.getRuntime().freeMemory() / (1024 * 1024);
long used_memory = total_memory - free_memory;
long perc_used = (int) ((used_memory * 100) / max_memory);

// Passing values to jsp page
request.setAttribute("statistics", statistics);
request.setAttribute("created", created);
request.setAttribute("cacheEmptied", cacheEmptied);
request.setAttribute("cacheEmptiedSeconds", cacheEmptiedSeconds);
request.setAttribute("openStatements", openStatements);
request.setAttribute("currentDate", currentDate);
request.setAttribute("pageStatistics", pageStatistics);
request.setAttribute("pageStatisticsSeconds", pageStatisticsSeconds);
request.setAttribute("isScoringRunning", isScoringRunning);
request.setAttribute("statisticsEntityName", statisticsEntityName);

request.setAttribute("max_memory", max_memory);
request.setAttribute("total_mamory", total_memory);
request.setAttribute("free_memory", free_memory);
request.setAttribute("used_memory", used_memory);
request.setAttribute("perc_used", perc_used);

String clearProfile = request.getParameter("clearProfile");
if (clearProfile != null) {
if (clearProfile.equals("all")) {
ObjectStore.clear(null);
} else {
ObjectStore.clear(clearProfile + " Profile");
}
}
return SessionLookup.FORWARD_SUCCESS_NO_INDEX_JSP;
}

protected String getForward(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
throws JspException {
String forward = super.findForward(mapping, form, request, response);
return forward;
}

protected Permission getPermission() {
return null;
}

protected WebApplicationContext getWebApplicationContext(ServletContext servletContext){
WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
return wac;
}
}


For the moment I have such code in my test class:



@RunWith(MockitoJUnitRunner.class)
public class AdminViewSqlStatisticsActionTest {

@InjectMocks
private AdminViewSqlStatisticsAction adminViewSqlStatisticsAction = spy(new AdminViewSqlStatisticsAction());

@Mock
private ActionMapping mapping;
@Mock
private AdminForm form;
@Mock
private HttpServletRequest request;
@Mock
private HttpServletResponse response;
@Mock
private HttpSession session;
@Mock
private ServletContext servletContext;
@Mock
private WebApplicationContextUtils wacu;
@Mock
private WebApplicationContext wac;
@Mock
private FantasyService fantasyService;
@Mock
private Statistics statistics;
@Mock
private SessionFactory sessionFactory;

@Test
public void testFindForwardSuccess() throws JspException {

when(request.getSession()).thenReturn(session);
when((ServletContext)session.getServletContext()).thenReturn(servletContext);
when(servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE)).thenReturn(wac);
// when(WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext)).thenReturn(wac);
when(adminViewSqlStatisticsAction.getWebApplicationContext(servletContext)).thenReturn(wac);
when(wac.getBean("fantasyService")).thenReturn(fantasyService);
when(fantasyService.getSessionFactory()).thenReturn(sessionFactory);
when(sessionFactory.getStatistics()).thenReturn(statistics);
}
}

Aucun commentaire:

Enregistrer un commentaire