Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Spring Boot Test Injection Failure Troubleshooting

Tech 2

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:

  1. @ContextConfiguration(locations={"classpath*:/spring1.xml","classpath*:/spring2.xml"})

    • Specifies XML configuration files.
  2. @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.

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.