Spring
[Spring-MVC JUnit Test Error] No qualifying bean of type [javax.servlet.ServletContext] found for dependency..
uchacha
2020. 6. 17. 10:43
- @참고 : https://sup2is.tistory.com/70
- @참고2 : https://gdtbgl93.tistory.com/164
에러 내역
No qualifying bean of type [javax.servlet.ServletContext] found for dependency
원인
junit이 돌아갈때는 javax.servlet 패키지에 대한 의존관계를 아무도 해결해주지 않기때문에 스프링 컨테이너가 올라가는 시점에 에러가 난다.
해결법
@WebAppConfiguration 어노테이션 사용
spring doc
The presence of @WebAppConfiguration on a test class indicates that a WebApplicationContext should be loaded for the test using a default for the path to the root of the web application.테스트 클래스에 @WebAppConfiguration이 있으면 웹 응용 프로그램의 루트에 대한 경로에 대한 기본값을 사용하여 테스트를 위해 WebApplicationContext를로드해야 함을 나타냅니다.
에러 내역
java.lang.NoClassDefFoundError: javax/servlet/SessionCookieConfig
원인
서블릿 버전 3.1 이하에서는 SessionCookieConfig 클래스를 찾지 못하는 오류가 발생한다.
해결법
서블릿 jar 파일을 3.1 버전으로 업데이트
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>