Loading...
Spring Framework Reference Documentation 7.0.2의 Loading a WebApplicationContext의 한국어 번역본입니다.
아래의 경우에 피드백에서 신고해주신다면 반영하겠습니다.
감사합니다 :)
WebApplicationContext표준 ApplicationContext 대신 WebApplicationContext를 로드하도록 TestContext 프레임워크에 지시하려면 해당 테스트 클래스에 @WebAppConfiguration을 어노테이션으로 지정하면 됩니다.
테스트 클래스에 @WebAppConfiguration이 존재하면 TestContext 프레임워크(TCF)는 통합 테스트를 위해 WebApplicationContext(WAC)를 로드해야 함을 인지합니다. 내부적으로 TCF는 MockServletContext가 생성되어 테스트의 WAC에 제공되도록 보장합니다. 기본적으로 MockServletContext의 기본 리소스 경로는 src/main/webapp으로 설정됩니다. 이는 JVM의 루트(일반적으로 프로젝트의 경로)를 기준으로 한 상대 경로로 해석됩니다.
Maven 프로젝트의 웹 애플리케이션 디렉터리 구조에 익숙하다면 src/main/webapp이 WAR의 루트에 대한 기본 위치라는 것을 알고 있을 것입니다. 이 기본값을 오버라이드해야 하는 경우 @WebAppConfiguration 어노테이션에 대체 경로를 제공할 수 있습니다(예: @WebAppConfiguration("src/test/webapp")). 파일 시스템 대신 클래스패스에서 기본 리소스 경로를 참조하고 싶다면 Spring의 classpath: 프리픽스를 사용할 수 있습니다.
Spring의 WebApplicationContext 구현에 대한 테스트 지원이 표준 ApplicationContext 구현에 대한 지원과 동등하다는 점에 유의하십시오. WebApplicationContext로 테스트할 때는 @ContextConfiguration을 사용하여 XML 설정 파일, Groovy 스크립트 또는 @Configuration 클래스를 자유롭게 선언할 수 있습니다.
또한 @ActiveProfiles, @TestExecutionListeners, @Sql, @Rollback 등과 같은 다른 테스트 어노테이션도 자유롭게 사용할 수 있습니다.
이 섹션의 나머지 예제들은 WebApplicationContext를 로드하기 위한 다양한 설정 옵션을 보여줍니다. 다음 예제는 설정보다 관례를 우선하는 TestContext 프레임워크의 지원을 보여줍니다:
1@ExtendWith(SpringExtension.class) 2 3// defaults to "file:src/main/webapp" 4@WebAppConfiguration 5 6// detects "WacTests-context.xml" in the same package 7// or static nested @Configuration classes 8@ContextConfiguration 9class WacTests { 10 //... 11}
1@ExtendWith(SpringExtension::class) 2 3// defaults to "file:src/main/webapp" 4@WebAppConfiguration 5 6// detects "WacTests-context.xml" in the same package 7// or static nested @Configuration classes 8@ContextConfiguration 9class WacTests { 10 //... 11}
리소스 기본 경로를 지정하지 않고 테스트 클래스에 @WebAppConfiguration을 어노테이션으로 지정하면 리소스 경로는 사실상 file:src/main/webapp이 기본값이 됩니다. 마찬가지로 리소스 locations, 컴포넌트 classes 또는 컨텍스트 initializers를 지정하지 않고 @ContextConfiguration을 선언하면 Spring은 관례를 사용하여 설정의 존재를 감지하려고 시도합니다(즉, WacTests 클래스와 동일한 패키지에 있는 WacTests-context.xml 또는 static nested @Configuration 클래스).
다음 예제는 @WebAppConfiguration으로 리소스 기본 경로를 명시적으로 선언하고 @ContextConfiguration으로 XML 리소스 위치를 명시적으로 선언하는 방법을 보여줍니다:
1@ExtendWith(SpringExtension.class) 2 3// file system resource 4@WebAppConfiguration("webapp") 5 6// classpath resource 7@ContextConfiguration("/spring/test-servlet-config.xml") 8class WacTests { 9 //... 10}
1@ExtendWith(SpringExtension::class) 2 3// file system resource 4@WebAppConfiguration("webapp") 5 6// classpath resource 7@ContextConfiguration("/spring/test-servlet-config.xml") 8class WacTests { 9 //... 10}
여기서 주목해야 할 중요한 점은 이 두 어노테이션에 대한 경로의 의미가 서로 다르다는 것입니다. 기본적으로 @WebAppConfiguration 리소스 경로는 파일 시스템 기반인 반면, @ContextConfiguration 리소스 위치는 클래스패스 기반입니다.
다음 예제는 Spring 리소스 프리픽스를 지정하여 두 어노테이션 모두에 대해 기본 리소스 의미를 오버라이드할 수 있음을 보여줍니다:
1@ExtendWith(SpringExtension.class) 2 3// classpath resource 4@WebAppConfiguration("classpath:test-web-resources") 5 6// file system resource 7@ContextConfiguration("file:src/main/webapp/WEB-INF/servlet-config.xml") 8class WacTests { 9 //... 10}
1@ExtendWith(SpringExtension::class) 2 3// classpath resource 4@WebAppConfiguration("classpath:test-web-resources") 5 6// file system resource 7@ContextConfiguration("file:src/main/webapp/WEB-INF/servlet-config.xml") 8class WacTests { 9 //... 10}
이 예제의 comment를 이전 예제의 comment와 비교해 보십시오.
Context Configuration with Dynamic Property Sources
Working with Web Mocks