. Advertisement .
..3..
. Advertisement .
..4..
Junit is a Java unit testing framework, which is vital for test-driven development of a Spring application. Spring Boot is a must-have in this case.
However, working with this app will lead to an unwanted error message: Failed To Load Applicationcontext. So what causes this error? How to solve it? This post will give you the answers!
What Causes Failed To Load Applicationcontext Error?
The error comes from the use of the XML Based configuration. It appears in the test classes when the test context fails to load the application one.
Assume that you have an application-context.xml file:
<?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.ittutoria.xmlapplicationcontext.service.EmployeeServiceImpl” />
</beans>
Create a class and service interface:
public interface EmployeeService {
Employee getEmployee();
}
public class EmployeeServiceImpl implements EmployeeService {
@Override
public Employee getEmployee() {
return new Employee(“ITtutoria”, “Admin”);
}
}
Create a test:
@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();
}
}
When running this test, you will get the following message:
java.lang.IllegalStateException: Failed to load ApplicationContext
How To Fix The Error?
Method 1: Use SpringBootTest Annotation
The first method to solve the error is to use SpringBootTest annotation. This one describes an existing project or creates a new Spring Boot one.
Use the ImportResource annotation for the main class first:
@SpringBootApplication
@ImportResource({"classpath*:application-context.xml"})
Here is the test case for 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();
}
}
Method 2: Use ContextConfiguration
You can create the context by the different bean configurations. Let’s create a bean from the interface:
public class EmployeeServiceTestImpl implements EmployeeService {
@Override
public Employee getEmployee() {
return new Employee("ITtutoria-Test", "Admin");
}
}
Then, it is time to get a test-context.xml file:
<?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, here is 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();
}
}
Conclusion
The Failed to load ApplicationContext error often appears in the test classes. This post discusses the best ways to test and solve the problem.
Leave a comment