84669 person learning
152542 person learning
20005 person learning
5487 person learning
7821 person learning
359900 person learning
3350 person learning
180660 person learning
48569 person learning
18603 person learning
40936 person learning
1549 person learning
1183 person learning
32909 person learning
如果把获取WEB路径的注释掉,单元测试就正常
所以求助:如何配置单元测试避免servletContext空指针异常?
ringa_lee
webApplicationContext为null。你不能在static的静态块中这样做。你应该使用spring的方式来初始化上下文,或者你自己初始化ApplicationContext.Refer to this example.
webApplicationContext
null
static
spring
ApplicationContext
Unit tests don’t haveWEB环境,所有的WEB对象都是Mock而来。你想要获取啥参数,应该都是提前知道的。而不需要通过ServletContext来获取。单元测试环境也没有ServletContextobjects.
WEB
Mock
ServletContext
webApplicationContext
为null
。你不能在
static
的静态块中这样做。你应该使用spring
的方式来初始化上下文,或者你自己初始化ApplicationContext
.Refer to this example.
Unit tests don’t have
WEB
环境,所有的WEB
对象都是Mock
而来。你想要获取啥参数,应该都是提前知道的。而不需要通过ServletContext
来获取。单元测试环境也没有ServletContext
objects.