JUnit 测试 Spring Controller 应用上下文加载失败
最后更新:2024年5月11日
1. 概述
Spring Boot 应用中混合定义 bean 的方式包括同时使用基于注解和基于 XML 的配置。 在这种环境下,我们可能需要在测试类中使用基于 XML 的配置。 然而,有时在这种情况下,我们可能会遇到应用上下文加载错误“Failed to load ApplicationContext”。 此错误出现在测试类中,因为测试上下文未加载应用程序上下文。
在本教程中,我们将讨论如何在 Spring Boot 应用程序中将 XML 应用程序上下文集成到测试中。
更多阅读
Spring Boot 错误 ApplicationContextException
学习如何解决 Spring Boot 中的 ApplicationContextException。
阅读更多 →
2. “Failed to load ApplicationContext” 错误
让我们通过在 Spring Boot 应用程序中集成基于 XML 的应用程序上下文来重现该错误。
首先,假设我们有一个application-context.xml 文件,其中包含服务 bean 的定义
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="https://w3org.cn/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="employeeServiceImpl" class="com.baeldung.xmlapplicationcontext.service.EmployeeServiceImpl" />
</beans>
现在我们可以将application-context.xml 文件放在webapp/WEB-INF/ 位置
我们还将创建一个服务接口和类
public interface EmployeeService {
Employee getEmployee();
}
public class EmployeeServiceImpl implements EmployeeService {
@Override
public Employee getEmployee() {
return new Employee("Baeldung", "Admin");
}
}
最后,我们将为从应用程序上下文中获取EmployeeService bean 创建一个测试用例
@RunWith(SpringRunner.class)
@ContextConfiguration(locations={"classpath:WEB-INF/application-context.xml"})
public class EmployeeServiceAppContextIntegrationTest {
@Autowired
private EmployeeService service;
@Test
public void whenContextLoads_thenServiceISNotNull() {
assertThat(service).isNotNull();
}
}
现在如果我们尝试运行此测试,我们将观察到错误
java.lang.IllegalStateException: Failed to load ApplicationContext
此错误出现在测试类中,因为测试上下文未加载应用程序上下文。 此外,根本原因是WEB-INF 未包含在类路径中
@ContextConfiguration(locations={"classpath:WEB-INF/application-context.xml"})
3. 在测试中使用基于 XML 的ApplicationContext
让我们看看如何在测试类中使用基于 XML 的ApplicationContext。 我们有两种方法可以在测试中使用基于 XML 的ApplicationContext:@SpringBootTest 和@ContextConfiguration 注解。
3.1. 使用@SpringBootTest 和@ImportResource 进行测试
Spring Boot 提供了@SpringBootTest 注解,我们可以使用它来创建要在测试中使用的应用程序上下文。 此外,我们必须在 Spring Boot 主类中使用@ImportResource 来读取 XML bean。 此注解允许我们导入包含 bean 定义的一个或多个资源。
首先,让我们在主类中使用@ImportResource 注解
@SpringBootApplication
@ImportResource({"classpath*:application-context.xml"})
现在让我们为从应用程序上下文中获取EmployeeService bean 创建一个测试用例
@RunWith(SpringRunner.class)
@SpringBootTest(classes = XmlBeanApplication.class)
public class EmployeeServiceAppContextIntegrationTest {
@Autowired
private EmployeeService service;
@Test
public void whenContextLoads_thenServiceISNotNull() {
assertThat(service).isNotNull();
}
}
@ImportResource 注解加载位于resource 目录中的 XML bean。 此外,@SpringBootTest 注解将整个应用程序的 bean 加载到测试类中。 因此,我们能够在测试类中访问EmployeeService bean。
3.2. 使用带有resources 的@ContextConfiguration
我们可以通过将测试配置文件放置在src/test/resources 目录中,使用不同的 bean 配置来创建我们的测试上下文。
在这种情况下,我们使用@ContextConfiguration 注解从src/test/resources 目录加载测试上下文。
首先,让我们从EmployeeService 接口创建另一个 bean
public class EmployeeServiceTestImpl implements EmployeeService {
@Override
public Employee getEmployee() {
return new Employee("Baeldung-Test", "Admin");
}
}
然后我们将创建test-context.xml 文件放在src/test/resources 目录中
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="https://w3org.cn/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="employeeServiceTestImpl" class="process.service.EmployeeServiceTestImpl" />
</beans>
最后,我们将创建测试用例
@SpringBootTest
@ContextConfiguration(locations = "/test-context.xml")
public class EmployeeServiceTestContextIntegrationTest {
@Autowired
@Qualifier("employeeServiceTestImpl")
private EmployeeService serviceTest;
@Test
public void whenTestContextLoads_thenServiceTestISNotNull() {
assertThat(serviceTest).isNotNull();
}
}
这里我们使用@ContextConfiguration 注解从test-context.xml 加载了employeeServiceTestImpl。
3.3. 使用带有WEB-INF 的@ContextConfiguration
我们也可以从WEB-INF目录导入应用程序上下文到测试类中。为此,我们可以使用其file URL来引用应用程序上下文。
@RunWith(SpringRunner.class)
@ContextConfiguration(locations = "file:src/main/webapp/WEB-INF/application-context.xml")
4. 结论
在本文中,我们学习了如何在 Spring Boot 应用程序的测试类中使用基于 XML 的配置文件。
支持本文的代码可在 GitHub 上获取。 一旦你以 Baeldung Pro 会员 身份登录,就开始学习并在项目上进行编码。
















