SpringBoot集成JUnit测试时TomcatResources空指针异常求助
你这是踩中了SpringBoot内置Tomcat版本判断的一个坑!核心问题出在TomcatResources.get(Context)的版本检测逻辑上:它用org.apache.catalina.deploy.ErrorPage类的存在性来区分Tomcat7和Tomcat8,但Tomcat8.5.x里这个类依然存在,导致错误实例化了Tomcat7Resources。而Tomcat7Resources会反射调用Tomcat Context的addResourceJarUrl方法——这个方法是Tomcat8才新增的,自然找不到,最终触发NPE。
下面给你几个可行的解决思路,按推荐程度排序:
1. 自定义Tomcat容器工厂,强制使用Tomcat8Resources
最直接的方式是在测试配置中替换掉默认的TomcatResources实例,直接指定用Tomcat8的实现:
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory; import org.springframework.boot.context.embedded.tomcat.TomcatResources; import org.springframework.boot.context.embedded.tomcat.Tomcat8Resources; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.apache.catalina.Context; @Configuration public class TestTomcatConfiguration { @Bean public TomcatEmbeddedServletContainerFactory tomcatContainerFactory() { return new TomcatEmbeddedServletContainerFactory() { @Override protected void postProcessContext(Context context) { // 直接创建Tomcat8Resources实例,覆盖默认的资源处理类 TomcatResources tomcat8Resources = new Tomcat8Resources(context); context.getAttributes().put(TomcatResources.class.getName(), tomcat8Resources); // 别忘了调用父类的后置处理逻辑 super.postProcessContext(context); } }; } }
然后在你的JUnit测试类上加上@Import(TestTomcatConfiguration.class),让测试上下文加载这个自定义配置,就能强制使用正确的Tomcat8Resources了。
2. 用BeanPostProcessor动态替换错误的Resources实例
如果你不想修改容器工厂,也可以通过Spring的Bean后置处理器,在Context初始化后替换掉错误的Tomcat7Resources:
import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanPostProcessor; import org.springframework.boot.context.embedded.tomcat.TomcatResources; import org.springframework.boot.context.embedded.tomcat.Tomcat8Resources; import org.springframework.stereotype.Component; import org.apache.catalina.Context; @Component public class TomcatResourcesFixProcessor implements BeanPostProcessor { @Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { // 只处理Tomcat的Context实例 if (bean instanceof Context) { Context context = (Context) bean; TomcatResources currentResources = (TomcatResources) context.getAttributes().get(TomcatResources.class.getName()); // 如果当前是Tomcat7Resources,就替换成Tomcat8Resources if (currentResources != null && currentResources.getClass().getSimpleName().equals("Tomcat7Resources")) { Tomcat8Resources fixedResources = new Tomcat8Resources(context); context.getAttributes().put(TomcatResources.class.getName(), fixedResources); } } return bean; } }
把这个处理器放到测试类能扫描到的包下,或者直接在测试类里用@Import导入即可。
3. 升级SpringBoot版本(治本之策)
如果你使用的SpringBoot版本比较旧,这个版本判断的bug可能已经被官方修复了。建议查看SpringBoot的发行日志,找到修复Tomcat版本检测逻辑的版本,升级到对应版本后,这个问题应该会自动消失。
内容的提问来源于stack exchange,提问作者Goldfish

