<?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>
SampleDAO
, SampleService
클래스를 스프링 빈으로 등록@ContextConfiguration
으로 참조되고 있음@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 빈을 주입 |
root-context.xml
파일에서 SampleDAO
를 스프링 빈으로 등록SpringExtension
이 컨테이너를 띄움@Autowired
로 SampleService
가 주입됨