<?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 class="com.ssg.springex.sample.SampleDAO"></bean>
    <bean class="com.ssg.springex.sample.SampleService"></bean>
</beans>

🧾 설명


@Log4j2
@ExtendWith(SpringExtension.class)
@ContextConfiguration(locations = "file:src/main/webapp/WEB-INF/root-context.xml")
public class SampleTests {

    @Autowired
    private SampleService sampleService;

    @Test
    public void testService() {
        log.info(sampleService);
        Assertions.assertNotNull(sampleService);
    }
}

🔍 사용된 어노테이션 설명

어노테이션 설명
@Log4j2 Lombok에서 제공하는 어노테이션으로 log.info() 등을 사용할 수 있게 로그 객체를 자동 생성해줌
@ExtendWith(SpringExtension.class) JUnit 5에서 스프링 테스트 기능을 사용하도록 해줌 (스프링 컨테이너 연동)
@ContextConfiguration(...) 어떤 스프링 설정 파일(XML)을 읽어야 하는지 지정 (root-context.xml)
@Autowired Spring 컨테이너에서 SampleService 빈을 주입

📌 전체 흐름 정리

  1. root-context.xml 파일에서 SampleDAO를 스프링 빈으로 등록
  2. 테스트 실행 시 SpringExtension이 컨테이너를 띄움
  3. @AutowiredSampleService 가 주입됨
  4. 테스트에서 객체가 잘 주입 되었는지 로그 출력하고 null 아님을 검증