내 웹 응용 프로그램에 대한 테스트를하고 싶습니다만, autowiring시 컨텍스트 구성이 충돌합니다. servletContext
. 아래 오류. Autowiring servletContext
는 tomcat/jetty에서 web-app를 실행할 때 잘 작동합니다.유닛 테스트 Spring MVC web-app : autowire 필드를 사용할 수 없습니다 : private javax.servlet.ServletContext
이 java.lang.IllegalStateException : org.springframework.beans.factory.BeanCreationException :에 의해 발생의 ApplicationContext ... 를로드하지 못했습니다 오류 이름 'testController'와 콩 생성 : autowire가 종속성 주입 실패; 중첩 예외는 입니다. org.springframework.beans.factory.BeanCreationException : 할 수 없습니다 autowire 필드 : private javax.servlet.ServletContext com.test.controllers.TestController.servletContext; 이 org.springframework.beans.factory.NoSuchBeanDefinitionException 인 경우 : 에 대해 에 대해 일치하는 bean이 발견되지 않음 : 종속성 : autowire로 간주되는 적어도 하나의 bean 이 종속성에 대한 후보. 종속성 주석 :
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class FirstTest {
@Test
public void doTest() throws Exception {
// ...
}
}
TestController
@Controller
public class TestController {
@Autowired
private ServletContext servletContext;
...
}
이 관련 답변을 참조 사라 http://stackoverflow.com/questions/2674697/how-to-inject-servletcontext-for-junit 테스트 - 봄 - – ptomli
감사. '@ ContextConfiguration'과 함께'MockServletContext'를 어떻게 사용합니까? – marioosh
'applicationContext.xml' 파일에'MockServletContext'에 대한 정의가 으로 포함되어 있습니다. –
ptomli