vendredi 27 mars 2015

How to write unit/integration test class for struts (1.2) Action class with Mockito

I have started to study how to write unit tests with Mockito version 1.9.5 .


And I need a help.


This is the code of my Action class which takes a request from a jsp page. In our project we use Struts 1.2 .



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 = super.findForward(mapping, form, request, response);

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

HttpSession session = request.getSession();
ServletContext servletContext = (ServletContext) session.getServletContext();
WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
FantasyService fantasyService = (FantasyService) wac.getBean("fantasyService");
Statistics statistics = fantasyService.getSessionFactory().getStatistics();
Collection<OpenStatement> openStatements = SQLProfiler.getOpenStatements();

Date created = FantasyService.created;
Date cacheEmptied = FantasyService.cacheEmptied;
long cacheEmptiedSeconds = (new java.util.Date().getTime() - FantasyService.cacheEmptied.getTime()) / 1000;
Date currentDate = new Date();
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);

request.setAttribute("statistics", statistics);
request.setAttribute("openStatements", openStatements);
request.setAttribute("created", created);
request.setAttribute("cacheEmptied", cacheEmptied);
request.setAttribute("cacheEmptiedSeconds", cacheEmptiedSeconds);
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 Permission getPermission() {
return null;
}

}


I have started to write the unit/test class following the examples from internet and for the moment I have such code in my class.



@RunWith(MockitoJUnitRunner.class)
public class AdminViewSqlStatisticsActionTest {
@InjectMocks
private AdminViewSqlStatisticsAction adminViewSqlStatisticsAction = new AdminViewSqlStatisticsAction();

@Mock
private ActionMapping mapping;

@Mock
private AdminForm form;

@Mock
private HttpServletRequest request;

@Mock
private HttpServletResponse response;

@Test
public void testFindForwardSuccess() throws JspException {

String result = adminViewSqlStatisticsAction.findForward(mapping, form, request, response);
assertEquals(SessionLookup.FORWARD_SUCCESS_NO_INDEX_JSP, result);
}
}


Is this code correct?


What code can I add to my test class?


Thank you for your help!


Aucun commentaire:

Enregistrer un commentaire