Tôi muốn thực hiện các kiểm tra cho ứng dụng web của mình, nhưng cấu hình ngữ cảnh bị treo trên autowiring servletContext
. Lỗi dưới đây. Autowiring servletContext
hoạt động tốt khi tôi chạy ứng dụng web trên tomcat/jetty.Thử nghiệm đơn vị Ứng dụng web Spring MVC: Không thể tự động điền trường: private javax.servlet.ServletContext
java.lang.IllegalStateException: Không thể tải ApplicationContext ... Nguyên nhân: org.springframework.beans.factory.BeanCreationException: Lỗi tạo đậu với tên 'testController': Tiêm autowired phụ thuộc thất bại; ngoại lệ lồng nhau là org.springframework.beans.factory.BeanCreationException: Không thể trường autowire: private javax.servlet.ServletContext com.test.controllers.TestController.servletContext; lồng nhau ngoại lệ là org.springframework.beans.factory.NoSuchBeanDefinitionException: Không đậu kết hợp của loại [javax.servlet.ServletContext] tìm thấy cho phụ thuộc: dự kiến ít nhất 1 đậu mà đủ điều kiện như autowire ứng cử viên cho sự phụ thuộc này. Phụ thuộc chú thích: {@ org.springframework.beans.factory.annotation.Autowired (bắt buộc = 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;
...
}
Xem câu trả lời liên quan này http://stackoverflow.com/questions/2674697/how-to-inject-servletcontext-for-junit -tests-with-spring – ptomli
Cảm ơn. Làm thế nào để sử dụng 'MockServletContext' với' @ ContextConfiguration'? – marioosh
Chỉ cần bao gồm định nghĩa cho 'MockServletContext' trong tệp' applicationContext.xml' –
ptomli