java.lang.IllegalStateException: Failed to load ApplicationContext

The issue

When we were writing unit test for a java spring boot application we ran into the following issue:

java.lang.IllegalStateException: Failed to load ApplicationContext at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:125) at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:108)

The reason

There are many reasons of this issue but in our case it was not finding a bean that we used in the unit test.

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'transformedAuthorConsumer': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'no.mentormedier.loader.utility.ETLHttpUtils' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}

The solution

We were trying to get the application context using @SpringBootTest annotation. Inside the annotation we were specifying the classes to avoid loading all the classes. We missed one of the class that we used in the unit test case.

@RunWith(SpringRunner.class)
@SpringBootTest(classes = {TransformedAuthorConsumer.class})
// This causes the issue

As soon as we added the missing class the issue was solved.

@RunWith(SpringRunner.class)
@SpringBootTest(classes = {TransformedAuthorConsumer.class, ETLHttpUtils.class})