Unit testing Spring MVC web-app: Could not autowire field: private javax.servlet.ServletContext

Я хотел бы сделать тесты для моего web-приложения, но конфигурация контекста падает при автоподключении servletContext. Ошибка ниже. Autowiring servletContext работает хорошо, когда я запускаю web-приложение на tomcat/jetty.

java.lang.IllegalStateException: Failed to load ApplicationContext ... Вызвано: org.springframework.beans.factory.BeanCreationException: Ошибка при создании боба с именем 'testController': Injection of autowired зависимостей не удалось; вложенное исключение org.springframework.beans.factory.BeanCreationException: Could not автопроводное поле: private javax.servlet.ServletContext com.test.controllers.TestController.servletContext; вложенное исключение является org.springframework.beans.factory.NoSuchBeanDefinitionException: No подходящий боб типа [javax.servlet.ServletContext] не найден для зависимости: ожидалось, что будет найден хотя бы 1 боб, который квалифицируется как autowire кандидат для этой зависимости. Аннотации зависимостей: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

@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;

    ... 
}
11
задан marioosh 19 September 2011 в 13:16
поделиться