Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

1. Overview

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.

In this tutorial, we’ll discuss how to integrate the XML application context into testing in a Spring Boot application.

Further reading:

Testing in Spring Boot

Learn about how the Spring Boot supports testing, to write unit tests efficiently.

Integration Testing in Spring

A quick guide to writing integration tests for a Spring Web application.

Spring Boot Error ApplicationContextException

Learn how to solve the ApplicationContextException in Spring Boot.

2. “Failed to load ApplicationContext” Error

Let’s reproduce the error by integrating the XML-Based application context in a Spring Boot application.

First, let’s suppose we have an application-context.xml file with the definition of a service 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:

ApplicationContextDirecory

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:

@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:

@ContextConfiguration(locations={"classpath:WEB-INF/application-context.xml"})

3. Using an XML-Based ApplicationContext in Test

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.

3.1. Test Using @SpringBootTest and @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.

First, let’s use the @ImportResource annotation in the main class:

@SpringBootApplication
@ImportResource({"classpath*:application-context.xml"})

Now let’s create a test case for getting EmployeeService bean from the application context:

@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.

3.2. Test Using @ContextConfiguration With resources

We can create our test context with different configurations of beans by placing our test configuration file in the src/test/resources directory.

In this case, we use the @ContextConfiguration annotation for loading the test context from the src/test/resources directory.

First, let’s create another bean from the EmployeeService interface:

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:

<?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.

3.3. Test Using @ContextConfiguration With 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:

@RunWith(SpringRunner.class)
@ContextConfiguration(locations = "file:src/main/webapp/WEB-INF/application-context.xml")

4. Conclusion

In this article, we learned how to use XML-Based configuration files in test classes in a Spring Boot application.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW