@ContextConfiguration配置默认servlet处理需要servletcontext spring org.springframework.util.Assert.notNull错误

博客指出使用Assert.assertnotnull方法断言失败,可能是jar包版本不兼容,需检查spring - test等jar包版本。还分析了产生问题的根本原因是执行了@EnableWebMvc,最后介绍了两种解决方案,一是@contextConfiguration中不含有@EnableWebMvc,二是在测试类加@WebApplicationContext注解,也提及了@ComponentScan的过滤方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

错误提示
如果使用了Assert.assertnotnull 的方法的话,有可能是断言失败

这个错误出现,在找不到别的原因的时候,基本可以断定是jar包版本不兼容的问题

  1. 首先检查spring-test jar包的版本号,因为这个jar包有可能是系统引用的,版本不兼容
  2. 然后检查spring 的其余包的版本号是否一致
  3. 然后检查引用的集成的特殊的包

@ContextConfiguration
ContextConfiguration 的使用配置默认servlet处理需要servletcontext
产生这种状况的根本原因

  • 执行了@EnableWebMvc,这个就是最直接的原因
  • 根本原因:启动WebMvc 需要servletcontext(底层的东西以后会在想下面进行补充)

很多人感觉自己引用的类里面没有@EnableWebMvc啊!
下面说一下我的经验:
一般来说我们需要引用的是配置的 config 文件,config文件一般来说都会扫描一些别的包,引入各种东西,甚至有一些小伙伴在扫描的时候根据很多教材的版本,直接扫描 classpath 根目录,这种情况也是存在的,这种情况就说明你的 mvc 的配置文件也通过扫描,这就说明 mvc 中的@enableMebMvc 得到了有效的引入执行。
下面就说一说解决方案吧!
一般网上有两种解决方案:

  • 第一种方案:@contextConfiguration中不要含有@EnableWebMvc
  • 第二种方案:在测试类中加入@WebApplicationContext注解来配置Servlet环境。
    当我还是小萌新的时候,大佬解释到这一步我还是一脸懵逼。
    怎么不含有@EnableWebMvc 啊,删了?
    @ComponentScan 的过滤方法
    我们通过属性的设置,过滤掉我们不需要扫描的包
@ComponentScan(basePackages = {"****"},
excludeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION,value = EnableWebMvc.class)})

excludeFilers属性是设置过滤文件的。

未完待续

[ERROR] Errors: [ERROR] ApiServiceTest.testCallConsentServiceVerifyConsent_thenReturnBadRequest » IllegalState Failed to load ApplicationContext for [WebMergedContextConfiguration@2dfeb141 testClass = unit.ApiSer viceTest, locations = [], classes = [com.dahsing.api.Application], contextInitializerClasses = [], activeProfiles = [], propertySourceDescriptors = [], propertySourceProperties = ["org.springframewo rk.boot.test.autoconfigure.web.servlet.WebMvcTestContextBootstrapper=true"], contextCustomizers = [[ImportsContextCustomizer@629e8212 key = [org.springframework.boot.autoconfigure.task.TaskExecution AutoConfiguration, org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration, org.springframework.boot.autoconfigure.web.servlet.HttpEncodingAutoConfiguration, org.springframework.boot.aut oconfigure.validation.ValidationAutoConfiguration, org.springframework.boot.autoconfigure.context.MessageSourceAutoConfiguration, org.springframework.boot.autoconfigure.hateoas.HypermediaAutoConfigu ration, org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration, org.springframework.boot.autoconfigure.data.web.SpringDataWebAutoConfiguration, org.springframework.boot.test.autocon figure.web.reactive.WebTestClientAutoConfiguration, org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration, org.springframework.boot.test.autoconfigure.web.servlet.MockMv cAutoConfiguration, org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration, org.springframework.boot.autoconfigure.security.servlet.SecurityFilterAutoConfiguration, org.sp ringframework.boot.autoconfigure.http.HttpMessageConvertersAutoConfiguration, org.springframework.boot.autoconfigure.security.oauth2.client.servlet.OAuth2ClientAutoConfiguration, org.springframework .boot.test.autoconfigure.web.servlet.MockMvcSecurityConfiguration, org.springframework.boot.test.autoconfigure.web.servlet.MockMvcWebClientAutoConfiguration, org.springframework.boot.test.autoconfig ure.web.servlet.MockMvcWebDriverAutoConfiguration, org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration, org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration, org .springframework.boot.autoconfigure.jsonb.JsonbAutoConfiguration, org.springframework.boot.autoconfigure.freemarker.FreeMarkerAutoConfiguration, org.springframework.boot.autoconfigure.web.servlet.We bMvcAutoConfiguration, org.springframework.boot.autoconfigure.security.servlet.UserDetailsServiceAutoConfiguration, org.springframework.boot.autoconfigure.security.oauth2.resource.servlet.OAuth2Reso urceServerAutoConfiguration, org.springframework.boot.autoconfigure.groovy.template.GroovyTemplateAutoConfiguration, org.springframework.boot.autoconfigure.mustache.MustacheAutoConfiguration]], org. springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@28b46423, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@ 48c76607, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.reactor.netty.DisableReactorResourceFactoryGlobalResourcesContextCustomizerFactory$ DisableReactorResourceFactoryGlobalResourcesContextCustomizerCustomizer@3fc79729, org.springframework.boot.test.autoconfigure.OverrideAutoConfigurationContextCustomizerFactory$DisableAutoConfigurati onContextCustomizer@1187c9e8, org.springframework.boot.test.autoconfigure.actuate.observability.ObservabilityContextCustomizerFactory$DisableObservabilityContextCustomizer@1f, org.springframework.bo ot.test.autoconfigure.filter.TypeExcludeFiltersContextCustomizer@35ae581d, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@14deeb89, org.springframework.boot. test.autoconfigure.web.servlet.WebDriverContextCustomizer@9573584, org.springframework.test.context.web.socket.MockServerContainerContextCustomizer@53251a66, org.springframework.boot.test.context.SpringBootTestAnnotation@bcb91adb], resourceBasePath = "src/main/webapp", contextLoader = org.springframework.boot.test.context.SpringBootContextLoader, parent = null] [INFO] [ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0
最新发布
06-26
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值