JUnit + Spring配置文件:无法加载JDBC驱动程序类

时间:2016-03-12 22:49:19

标签: java spring jdbc spring-profiles

如何解决下一个异常?

在配置中看不到任何问题。

效果很好,但经过一些重构失败后。

异常

java.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124)
...Property 'driverClassName' threw exception; nested exception is java.lang.IllegalStateException: Could not load JDBC driver class [${jdbc.driverClassName}]
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:121)
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:75)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1514)
... 52 more

配置

JUnit的

@ActiveProfiles("test")
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = SpringHsqlTest.class)
@TestExecutionListeners({DependencyInjectionTestExecutionListener.class,
    DirtiesContextTestExecutionListener.class,
    TransactionalTestExecutionListener.class,
    DbUnitTestExecutionListener.class,
    ForeignKeyBreakTestExecutionListener.class})
@ContextConfiguration({"classpath:contexts/bean-locations.xml"})
public class SpringHsqlTest {//...

来自pom.xml

   <dependency>
        <groupId>org.hsqldb</groupId>
        <artifactId>hsqldb</artifactId>
        <version>2.3.3</version>
        <scope>test</scope>
    </dependency>

的profiles.xml

<beans profile="test">
    <context:property-placeholder ignore-resource-not-found="false"
                                  location="classpath*:properties/test-database.properties"/>
</beans>

来自data-source.xml

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${jdbc.driverClassName}"/>
    <property name="username" value="${jdbc.userName}"/>
    <property name="password" value="${jdbc.password}"/>
    <property name="url" value="${jdbc.url}"/>
</bean>

属性/ test-database.properties

jdbc.dialect=H2Dialect
jdbc.driverClassName=org.hsqldb.jdbcDriver
jdbc.url=jdbc:hsqldb:mem:dbtest-local
jdbc.userName=sa
jdbc.password=

1 个答案:

答案 0 :(得分:0)

<强>修正

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns="http://www.springframework.org/schema/beans"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
     ...
     <import resource="profiles.xml"/>
</beans>