我创建了一个 STS Spring MVC 模板应用程序,并希望使用 JUnit 连接并测试我的 Dao。该项目在 Tomcat 中加载良好这是我的 servlet 上下文:
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
<!-- JDBC Datasource Configuration Bean -->
<beans:bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<beans:property name="driverClassName" value="${jdbc.driverClassName}" />
<beans:property name="url" value="${jdbc.url}" />
<beans:property name="username" value="${jdbc.username}" />
<beans:property name="password" value="${jdbc.password}" />
</beans:bean>
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<context:property-placeholder location="/WEB-INF/jdbc.properties"/>
<context:component-scan base-package="com.example" />
context:property jdbc.properties 加载正常,直到它被 @ContextConfiguration 读入
这是我的 JUnit 测试:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"file:src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml"})
public class ControllerTest{
@Autowired
ApplicationContext applicationContext;
private MockHttpServletRequest request;
private MockHttpServletResponse response;
private HandlerAdapter handlerAdapter;
@Autowired
private HomeController homeController;
@Before
public void setUp() {
request = new MockHttpServletRequest();
response = new MockHttpServletResponse();
handlerAdapter = new AnnotationMethodHandlerAdapter();
}
@Test
public void testHallo() throws Exception {
request.setRequestURI("/");
request.setMethod("GET");
final ModelAndView mav = handlerAdapter.handle(request, response,
homeController);
System.out.println("TEST " + mav.getModelMap());
}
}
如果我注释掉 jdbc.properties 文件,它工作正常
我得到的错误是:
java.lang.IllegalStateException:无法加载 ApplicationContext
引起:org.springframework.beans.factory.BeanInitializationException:无法加载属性;嵌套异常是 java.io.FileNotFoundException: 类路径资源 [WEB-INF/jdbc.properties] 无法打开,因为它不存在
项目运行良好,但 JUnit 找不到数据库属性。
这是我尝试过的:
<context:property-placeholder location="file:/WEB-INF/jdbc.properties"/>
还
但是,这会导致 Tomcat 抛出异常。