1. Overview
1.概述
The hybrid definition of beans in a Spring Boot application is one that includes both an Annotation-Based and XML-Based configuration. In this environment, we may want to use the XML-Based configuration in the test classes. However, sometimes in this situation, we may encounter the application context loading error “Failed to load ApplicationContext.” This error appears in the test classes because the application context isn’t loaded in the test context.
在Spring Boot应用程序中对Bean的混合定义是指同时包括基于注释的和基于XML的配置。在这种环境下,我们可能希望在测试类中使用基于XML的配置。然而,有时在这种情况下,我们可能会遇到应用上下文加载错误”Failed to load ApplicationContext。”这个错误出现在测试类中,因为应用上下文没有在测试上下文中加载。
In this tutorial, we’ll discuss how to integrate the XML application context into testing in a Spring Boot application.
在本教程中,我们将讨论如何在Spring Boot应用程序中把XML应用程序上下文集成到测试中。
2. “Failed to load ApplicationContext” Error
2. “加载ApplicationContext失败 “错误
Let’s reproduce the error by integrating the XML-Based application context in a Spring Boot application.
让我们通过在Spring Boot应用程序中整合基于XML的应用程序上下文来重现这个错误。
First, let’s suppose we have an application-context.xml file with the definition of a service bean:
首先,让我们假设我们有一个application-context.xml文件,其中有一个服务Bean的定义。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/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>
Now we can add the application-context.xml file in the webapp/WEB-INF/ location:
现在我们可以在webapp/WEB-INF/位置添加application-context.xml文件。
We’ll also create a service interface and class:
我们还将创建一个服务接口和类。
public interface EmployeeService {
Employee getEmployee();
}
public class EmployeeServiceImpl implements EmployeeService {
@Override
public Employee getEmployee() {
return new Employee("Baeldung", "Admin");
}
}
Finally, we’ll create a test case for getting the EmployeeService bean from the application context:
最后,我们将创建一个测试用例,从应用程序上下文中获取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();
}
}
Now if we try to run this test, we’ll observe the error:
现在如果我们尝试运行这个测试,我们会观察到错误。
java.lang.IllegalStateException: Failed to load ApplicationContext
This error appears in the test classes because the application context isn’t loaded in the test context. Moreover, the root cause is that the WEB-INF isn’t included in the classpath:
这个错误出现在测试类中,因为应用程序上下文没有在测试上下文中加载。此外,根本原因是WEB-INF没有包含在classpath中。
@ContextConfiguration(locations={"classpath:WEB-INF/application-context.xml"})
3. Using an XML-Based ApplicationContext in Test
3.在测试中使用基于XML的ApplicationContext
Let’s see how we can use an XML-Based ApplicationContext in test classes. We have two options to use the XML-Based ApplicationContext in the test: @SpringBootTest and @ContextConfiguration annotations.
让我们看看我们如何在测试类中使用基于XML的ApplicationContext。我们有两个选项可以在测试中使用基于XML的ApplicationContext。@SpringBootTest和@ContextConfiguration注释。
3.1. Test Using @SpringBootTest and @ImportResource
3.1.使用@SpringBootTest和@ImportResource测试
Spring Boot provides the @SpringBootTest annotation, which we can use to create an application context to be used in a test. In addition, we must use @ImportResource in the Spring Boot main class for reading XML beans. This annotation allows us to import one or more resources containing bean definitions.
Spring Boot提供了@SpringBootTest注解,我们可以用它来创建测试中使用的应用环境。此外,我们必须在Spring Boot主类中使用@ImportResource来读取XML beans。这个注解允许我们导入一个或多个包含Bean定义的资源。
First, let’s use the @ImportResource annotation in the main class:
首先,让我们在主类中使用@ImportResource注释。
@SpringBootApplication
@ImportResource({"classpath*:application-context.xml"})
Now let’s create a test case for getting EmployeeService bean from the application context:
现在让我们创建一个测试案例,从应用上下文中获取EmployeeService Bean。
@RunWith(SpringRunner.class)
@SpringBootTest(classes = XmlBeanApplication.class)
public class EmployeeServiceAppContextIntegrationTest {
@Autowired
private EmployeeService service;
@Test
public void whenContextLoads_thenServiceISNotNull() {
assertThat(service).isNotNull();
}
}
The @ImportResource annotation loads XML beans located in the resource directory. In addition, the @SpringBootTest annotation loads the whole application’s beans in the test class. Therefore, we’re able to access the EmployeeService bean in the test class.
@ImportResource 注解加载位于resource目录中的XML Bean。此外,@SpringBootTest注解在测试类中加载整个应用程序的Bean。因此,我们能够在测试类中访问EmployeeService Bean。
3.2. Test Using @ContextConfiguration With resources
3.2.使用@ContextConfiguration与resources进行测试
We can create our test context with different configurations of beans by placing our test configuration file in the src/test/resources directory.
我们可以通过在src/test/resources目录下放置我们的测试配置文件来创建具有不同配置的bean的测试环境。
In this case, we use the @ContextConfiguration annotation for loading the test context from the src/test/resources directory.
在这种情况下,我们使用@ContextConfiguration注解从src/test/resources目录加载测试上下文。
First, let’s create another bean from the EmployeeService interface:
首先,让我们从EmployeeService接口创建另一个bean。
public class EmployeeServiceTestImpl implements EmployeeService {
@Override
public Employee getEmployee() {
return new Employee("Baeldung-Test", "Admin");
}
}
Then we’ll create the test-context.xml file in the src/test/resources directory:
然后我们将在src/test/resources目录下创建test-context.xml文件。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/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>
Finally, we’ll create the test case:
最后,我们将创建测试案例。
@SpringBootTest
@ContextConfiguration(locations = "/test-context.xml")
public class EmployeeServiceTestContextIntegrationTest {
@Autowired
@Qualifier("employeeServiceTestImpl")
private EmployeeService serviceTest;
@Test
public void whenTestContextLoads_thenServiceTestISNotNull() {
assertThat(serviceTest).isNotNull();
}
}
Here we loaded employeeServiceTestImpl from the test-context.xml using the @ContextConfiguration annotation.
这里我们使用@ContextConfiguration注解从test-context.xml加载employeeServiceTestImpl。
3.3. Test Using @ContextConfiguration With WEB-INF
3.3.使用@ContextConfiguration与WEB-INF测试
We can also import an application context in the test classes from the WEB-INF directory. To do this, we can address the application context using its file URL:
我们也可以在测试类中从WEB-INF目录中导入一个应用程序上下文。要做到这一点,我们可以使用应用程序上下文的file URL来解决这个问题。
@RunWith(SpringRunner.class)
@ContextConfiguration(locations = "file:src/main/webapp/WEB-INF/application-context.xml")
4. Conclusion
4.总结
In this article, we learned how to use XML-Based configuration files in test classes in a Spring Boot application. As always, the source code used in this article is available over on GitHub.
在这篇文章中,我们学习了如何在Spring Boot应用程序的测试类中使用基于XML的配置文件。一如既往,本文中使用的源代码可在GitHub上找到。