When it comes to the Java unit testing framework, programmers know the importance of Junit as it can test spring applications. You need the spring boot when testing the interaction between code and spring. You may encounter ‘java.lang.illegalstateexception: Failed To Load Applicationcontext’ error, which makes you think you messed up with the code and figure out how this error occurs.
Here, we are going to tell you how this error occurs and what you need to do to fix it.
How the error ‘java.lang.illegalstateexception: Failed To Load Applicationcontext’ Occurs
You may get an error warning when you integrate application based on XML context in the spring boot application. You can assume, you have an application-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="addressServiceImpl" class="com.tutopal.springproject.service.AddressServiceImpl" />
</beans>
You created class and interface service like this:
public interface AddressService {
Address getAddress();
}
public class AddressServiceImpl implements AddressService {
@Override
public Address getAddress() {
return new Address("tutopal", "Admin");
}
}
Test creation
@RunWith(SpringRunner.class)
@ContextConfiguration(locations={"classpath:WEB-INF/application-context.xml"})
public class AddressServiceAppContextIntegrationTest {
@Autowired
private AddressService service;
@Test
public void whenContextLoads_serviceISNotNull() {
assertThat(service).isNotNull();
}
}
When you run this, you will get the error warning
java.lang.IllegalStateException: Failed to load ApplicationContext
How to Fix the Error ‘java.lang.illegalstateexception: Failed To Load Applicationcontext’
Have a look at the methods to solve it
Method 1 – ContextConfiguration
This method is used to create context’s test with various beans configuration. For this, you need to create a bean. Follow the code below
public class AddressServiceTestImpl implements AddressService {
@Override
public Address getAddress() {
return new Address("Baeldung-Test", "Admin");
}
}
For creating the test-context.xml file in the directory, follow this
<?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="addressServiceTestImpl" class="process.service.AddressServiceTestImpl" /> </beans>
In the end, you will create the test case
@SpringBootTest @ContextConfiguration(locations = "/test-context.xml") public class AddressServiceTestContextIntegrationTest {
@Autowired
@Qualifier("addressServiceTestImpl")
private AddressService serviceTest;
@Test public void whenTestContextLoads_serviceTestISNotNull() { assertThat(serviceTest).isNotNull();
}
}
Method 2 – SpringBootTest
This is another method that can be used to get rid of the error. In this method, you can use the SpringBootTest annotation to create the test for the context of the application. You are allowed to import or other resources. Check out the code
@SpringBootApplication
@ImportResource({"classpath*:application-context.xml"})
You need to use the import annotation in the main class to get the code correctly.
Have a look at the test case from the context application
@RunWith(SpringRunner.class)
@SpringBootTest(classes = XmlBeanApplication.class)
public class AddressServiceAppContextIntegrationTest {
@Autowired
private AddressService service;
@Test
public void whenContextLoads_serviceISNotNull() {
assertThat(service).isNotNull();
}
}
Here, the SpringBootTest annotation loads the beans application entirely in the test class. Hence, you can access bean in the test class.
Conclusion
To fix ‘java.lang.illegalstateexception: Failed To Load Applicationcontex’, we shed light on two methods. Both are perfect to solve the error warning that pops up every time you code. It is up to you to select the method that is more suitable for your application.
With that, I hope it helps! All the best for your program! Feel free to drop a message below if you need assistance.