Spring Boot Test Injection Failure Troubleshooting
Unit Test Template
@RunWith(SpringRunner.class)
//@SpringBootTest(classes = PersonConfig.class)
@ContextConfiguration(classes = PersonConfig.class)
class Test {
@Resource
private PersonEventService personEventService;
@Test
public void test() {
// ApplicationContext context = new AnnotationConfigApplicationContext(PersonEventService.class);
// PersonEventService personEventService = context.getBean(PersonEventService.class);
System.out.println(personEventService);
personEventService.registerUser("userName222");
}
}
@RunWith Annotation Explanation
In standard testing scenarios, the @RunWith annotation is required to specify the test execution environment. It instructs Java on how to run the test class, typically by initializing a Spring application context. Without it, developers would need to manuallly configure the entire runtime environment. In IntelliJ IDEA, tests may still execute without this annotation because the IDE automatically treats them as JUnit tests. However, this behavior is not consistent across all IDEs, so including @RunWith is recommended for portability.
@ContextConfiguration Annotation
There are two primary usage patterns:
-
@ContextConfiguration(locations={"classpath*:/spring1.xml","classpath*:/spring2.xml"})- Specifies XML configuration files.
-
@ContextConfiguration(classes = PersonConfig.class)- Specifies Java-based configuration classes.
XML Configuration Example:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<!-- Component scanning -->
<context:component-scan base-package="com" />
</beans>
This XML file uses <context:component-scan base-package="com" /> to automatically register beans from the specified package. When using @ContextConfiguration, locations = {"classpath*:/*.xml"} includes all XML files found in the classpath, making the scanned beans available for injection in the test class via @Autowired.
Classpath vs Classpath* Difference:
classpath: Only searches within the classpath.classpath*: Searches both the classpath and jar files.
Java Configuration Class:
/**
* Java Configuration Class
*/
@Configuration
@ComponentScan("com.xxx.xxx.controller.eventtest")
class PersonEventConfig {
}
Using Java configuration simplifies setup compared to XML. A configuration class annotated with @Configuration and @ComponentScan enables automatic component scanning. If no package is specified in @ComponentScan, it defaults to scanning the same package as the configuration class. This approach is preferred by Spring and offers cleaner syntax than XML-based configurations.