I have created cactus tests and want to test servlet doGet() method. I have the next code:
if (action.startsWith("/projects")) {
try {
int categoryid = Integer.valueOf(req.getParameter("category"));
List<Project> projects_with_category = projects.getContainer(categories.getById(categoryid));
session.setAttribute("projects", projects_with_category);
req.getRequestDispatcher("views/projects.jsp").forward(req, resp);
} catch (NumberFormatException | IndexOutOfBoundsException e) {
session.setAttribute("error", "error");
resp.sendRedirect("/categories");
}
} else if (action.startsWith("/project")) {
List<Project> projects = (List<Project>) session.getAttribute("projects");
System.out.println(projects);
try {
int projectid = Integer.valueOf(req.getParameter("projectid"));
for( Project project: projects) {
if(project.getId() == projectid) {
session.setAttribute("project", project);
req.getRequestDispatcher("views/project.jsp").forward(req, resp);
return;
}
}
The cactus tests:
public void begin2ProjectsTest(WebRequest request) {
request.setURL(null, null, "/projects", null, null);
request.addParameter("category", "1");
}
public void test2ProjectsTest() throws ServletException, IOException {
mainservlet.doGet(request, response);
assertNotNull(this.session.getAttribute("categoryid"));
assertNotNull(this.session.getAttribute("projects"));
projs = new ArrayList<Project>((List<Project>)this.session.getAttribute("projects"));
}
public void begin4ProjectTest(WebRequest request) {
request.setURL(null, null, "/project", null, null);
request.addParameter("projectid", "1");
session.setAttribute("projects", projs);
}
public void test4ProjectTest() throws ServletException, IOException {
mainservlet.doGet(request, response);
assertNotNull(this.session.getAttribute("project"));
}
The problem is that attribute "projects" available in first test become null in second what ever I did((( I even created List to copy "projects" but it still null in second project((( I can not test it(((
Aucun commentaire:
Enregistrer un commentaire