You run the JUnit test with the Spring Controller and receive an error message: “Failed to Load ApplicationContext for JUnit Test of Spring Controller”. Let’s figure out the solution for this kind of error.
Why do we get this error?
When you work with Spring Boot, you must write Unit Test for your code to ensure it works correctly. The idea is that we need the Unit Test to check when the application runs, the beans of the classes must be initialized in the Spring Container, and their method returns the exact values we want. But you’ll be in trouble if you don’t know how to use it correctly.
Let’s see an example of when the error occurs:
First, make sure in your project, in ‘src/main’, create a subfolder ‘webapp/WEB-INF’, then create ‘app_context.xml’ in WEB-INF
In src/main/webapp/WEB-INF/app_context.xml define 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="FractionResultImpl" class="com.hoanbm.applicationcontext.FractionResultImpl"/>
</beans>
Then we create an interface and classes:
In Fraction.java:
class Fraction {
double result = 0.0;
public Fraction(int numerator, int denominator) {
this.numerator = numerator,
this.denominator = denominator,
}
public int getNumerator(){
return this.numerator;
}
public int getDenominator(){
return this.denominator;
}
}
In FractionResult.java:
public interface FractionResult {
Fraction getResult();
}
In FractionResultImp.java:
public class FractionResultImpl implements FractionResult {
@Override
public Fraction getResult() {
fraction= new Fraction(8,10);
a = fraction.getNumerator();
b = fraction.getDenominator();
res = a/ b;
return res;
}
}
Now, let’s create a test case to get FractionResult bean from the application context:
@RunWith(SpringRunner.class)
@ContextConfiguration(locations={"classpath:WEB-INF/app_context.xml"})
public class FractionResultAppContextTest {
@Autowired
private FractionResult result;
@Test
public void contextLoad() throw Exception{
assertThat(result).isNotNull();
}
}
Lastly, you run this test and get the error:
Output:
java.lang.IllegalStateException: Failed to load ApplicationContext
The main reason that you get the error message: “Failed to Load ApplicationContext” is that “WEB-INF ” is not included in the classpath. Besides, the test context can not load the application context, so we get the error in the test classes.
Solution for the “Failed to load ApplicationContext” error
Solution 1 – Checking your injection of Spring Boot Starter Test dependency in pom.xml
The first thing you need to do is inject Spring Boot Starter Test dependency.
You also need to pay attention to the version of JUnit you are using. If you use the latest version of JUnit, version 5, you need to exclude the “junit-vintage-engine” and the “junit-jupiter-engine” for JUnit 5.
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Solution 2 – Using @SpringBootTest annotation
With @SpringBootTest annotation, users are allowed to create an application context to use when running a test.
@RunWith(SpringRunner.class)
@SpringBootTest(classes = BeansApplication.class)
public class FractionResultAppContextTest {
@Autowired
private FractionResult result;
@Test
public void contextLoad() throw Exception{
assertThat(result).isNotNull();
}
}
In the example code above, we can access FractionResult because @SpringBootTest helps us load all the application beans in the test class.
Solution 3 – Using @ContextConfiguration annotation with WEB-INF
You can scroll up to see the example of the error I mentioned above. I told you that the main reason the error occurs is that “WEB-INF ” is not included in the classpath. Here is the solution
Check this line in your code:
@ContextConfiguration(locations={"classpath:WEB-INF/app_context.xml"})
Edit it as below:
@ContextConfiguration(locations={"file:src/main/webapp/WEB-INF/app_context.xml"})
*Note: Remember this is in my example. In your project, it might be different. But that’s the general idea.
Summary
In this article, I discussed with you the “Failed to Load ApplicationContext” error. Along with that are some approaches to solving the problem. If there is a better way to solve this problem, the information about it will be updated. Thank you for your interest.
Maybe you are interested:
- Could not open a connection to your authentication agent
- “yarn.ps1 cannot be loaded because running scripts is disabled on this system”
- Download an Entire S3 Bucket

My name’s Christopher Gonzalez. I graduated from HUST two years ago, and my major is IT. So I’m here to assist you in learning programming languages. If you have any questions about Python, JavaScript, TypeScript, Node.js, React.js, let’s contact me. I will back you up.
Name of the university: HUST
Major: IT
Programming Languages: Python, JavaScript, TypeScript, Node.js, React.js