代码如下:
package com.web;
import java.util.List;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import com.bean.Girls;
import com.biz.GirlsBiz;
public class Controller {
private GirlsBiz girlsBiz;
//设值注入
public void setGirlsBiz(GirlsBiz girlsBiz) {
this.girlsBiz = girlsBiz;
}
public String sayhello(){
List<Girls> list=girlsBiz.findAllGirls();
for (Girls girls : list) {
System.out.println("name: "+girls.getName());
}
HttpServletRequest request=ServletActionContext.getRequest();
request.setAttribute("glist", list);
return "hello";
}
}
用junit测试时可以遍历出集合list的,可同时报空指针在HttpServletRequest request=ServletActionContext.getRequest();这句,求大神指点~
使用Junit的时候,不是从容器启动的,ServletActionContext是容器的上下文,肯定会报错的。
单元测试启动时都没加载部署文件web.xml