Spring Framework 5 standardized its logging infrastructure by removing support for Log4j 1.x configurations via LOG4jConfigListener. Applications should migrate to Log4j2 for consistent logging behavior across the framework.
Configure Log4j2 by adding the following dependencies to your project:
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.17.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.17.1</version>
</dependency>
Create a log4j2.xml configuration file in the resources directory:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="ConsoleOutput" target="SYSTEM_OUT">
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="ConsoleOutput"/>
</Root>
</Loggers>
</Configuration>
The spring-test module provides comprehensive integration for both JUnit 4 and JUnit 5 environments.
JUnit 4 Integration
Add the test-scoped dependencies:
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.3.9</version>
<scope>test</scope>
</dependency>
Implement the test class using the Spring JUnit 4 runner:
package com.example.spring;
import com.example.service.OrderService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@ContextConfiguration(locations = "classpath:spring-context.xml")
public class OrderServiceTest {
@Autowired
private OrderService orderService;
@Test
public void testOrderProcessing() {
boolean result = orderService.processOrder(12345L, 2);
System.out.println("Order processed: " + result);
}
}
JUnit 5 Integration
Include the JUnit Jupiter API:
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.8.1</version>
<scope>test</scope>
</dependency>
JUnit 5 supports both explicit extension configuration and composite annotations:
package com.example.spring;
import com.example.service.InventoryService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
@SpringJUnitConfig(locations = "classpath:spring-context.xml")
public class InventoryServiceTest {
@Autowired
private InventoryService inventoryService;
@Test
public void verifyStockCalculation() {
int available = inventoryService.queryStock(987);
System.out.println("Stock available: " + available);
}
}
Alternatively, use explicit extension registration:
@ExtendWith(SpringExtension.class)
@ContextConfiguration("classpath:spring-context.xml")
public class InventoryServiceExplicitTest {
@Autowired
private InventoryService inventoryService;
@Test
public void checkInventory() {
int count = inventoryService.queryStock(1001);
System.out.println("Current stock: " + count);
}
}