解决 spring-test 出现 Failed to load ApplicationContext 的异常

 

在使用spring-test的时候,在启动@Test的方法时,spring-test会去加载spring的配置文件,这个时候如果配置文件没有在 @ContextConfiguration 中写全,就会导致加载到一半失败,然后抛出 java.lang.IllegalStateException: Failed to load ApplicationContext 的异常,进而导致优先加载的各种bean加载失败

 

异常:

[main] WARN org.springframework.web.context.support.GenericWebApplicationContext - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'assetParser' defined in file [D:\project2\RM\target\classes\org\kinome\rm\utils\file\AssetParser.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.kinome.rm.utils.file.AssetParser]: Constructor threw exception; nested exception is java.lang.NullPointerException
[main] ERROR org.springframework.test.context.TestContextManager - Caught exception while allowing TestExecutionListener [org.springframework.test.context.web.ServletTestExecutionListener@f2f2cc1] to prepare test instance [MvcTest@6d469831]
java.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124)
at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:83)

 

 

解决方法:

只需要在 @ContextConfiguration 中把(idea)resources下的所有spring配置文件名写齐全,就不会因为加载不到没写的spring配置文件而加载失败了,写全就能成功加载完所有的配置文件进而全部加载成功了

例:

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations = {"classpath:applicationContext.xml", "classpath:spring-servlet.xml"}) // 这里因为没有把 applicationContext-quartz.xml 配置文件加载进去,所以导致了异常的发生
public class MvcTest {
}

 

只需要把 applicationContext.xml 改成 applicationContext*.xml 就可以把 applicationContext-quartz.xml 加载进来了,就能正常运行了。

以后如果再增加了新的spring文件,也得注意在spring-test的 @ContextConfiguration 中引入进来。

以后我会把spring的配置文件全部统一格式为:applicationContext(追加其他命名用来区别spring配置文件).xml ,这样一来,一个 applicationContext*.xml 就全部引入进来了,都不需要改web.xml 和 spring-test 或者其他依赖spring配置文件的需要引入的引入项 的引入了。

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations = {"classpath:applicationContext*.xml", "classpath:spring-servlet.xml"}) 
public class MvcTest {
}

 

转载于:https://www.cnblogs.com/kinome/p/9628830.html

### 回答1: 在使用Spring Boot进行测试时,有时可能会遇到"Failed to load ApplicationContext"的问题。这个错误通常是由于Spring Boot应用程序上下文无法正确加载所致。以下是一些可能的解决方案: 1. 检查测试类的注解。确保测试类上面有@SpringBootTes或者@SpringBootTest注解。 2. 检查应用程序的配置文件。确保application.properties或application.yml文件中的配置正确。 3. 检查依赖项。检查pom.xml文件中的依赖项是否正确。 4. 检查测试资源目录。检查测试资源目录是否正确配置。 5. 检查启动类。检查启动类是否正确配置。 如果上述方法都没有解决问题,可以尝试清除Maven本地仓库或者重新安装依赖项。 ### 回答2: 解决Spring Boot测试方法Failed to load ApplicationContext问题的方法如下: 1. 检查依赖:首先,确保你的项目中的依赖项正确配置。检查你的pom.xml文件,确认你的项目中包含了正确的Spring Boot相关依赖项,比如spring-boot-starter-test。 2. 检查注解配置:确保你的测试类上标注了正确的注解。通常使用@RunWith(SpringRunner.class)和@SpringBootTest注解来配置Spring Boot测试类。 3. 检查包扫描路径:确保你的项目中的包扫描路径配置正确。检查你的@SpringBootApplication注解,查看@ComponentScan注解是否包含了你的测试类所在的包。 4. 检查测试资源配置:如果你的测试类依赖于某些资源文件,例如配置文件或者数据库初始化脚本,确保这些资源文件正确放置在测试资源目录下。检查你的src/test/resources目录下是否包含了相关资源文件。 5. 检查数据库连接配置:如果你的测试类依赖于数据库连接,在application.properties或者application.yml文件中配置的数据库连接属性需要与你的测试环境一致。确保你的数据库连接配置正确,用户名、密码等属性与你的测试环境一致。 6. 检查测试类路径:确保你的测试类路径正确。比如,如果你的测试类在src/test/java目录下的某个包下,确保该包路径正确。 如果以上方法仍然无法解决问题,你可以尝试以下方法: 1. 清除和重新构建项目:有时候项目的构建可能存在问题,可以尝试清除项目并在重新构建一次。 2. 更新依赖版本:如果使用的Spring Boot版本较旧,可能存在某些已知的问题。尝试更新Spring Boot相关依赖的版本,可通过在pom.xml文件中修改对应的依赖版本号来实现。 3. 查看日志:如果以上方法仍然无法解决问题,可以查看测试过程中的日志信息,确定具体的错误原因,并针对性地解决。可以将日志级别设为DEBUG,查看更详细的日志信息。 总之,通过检查依赖配置、注解配置、路径配置,以及检查资源文件、数据库连接等相关配置,可以解决Spring Boot测试方法Failed to load ApplicationContext问题。若上述方法无效,可以尝试清除和重新构建项目,或者更新依赖版本,并查看日志信息以定位错误原因。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值