我正在嘗試爲我的Spring MVC應用程序設置H2內存數據庫的測試。使用Spring MVC進行Hibernate配置測試
現在,我所有用於Hibernate的配置都在一個Java文件PersistenceConfig.java
中,它擁有所有Hibernate配置的SessionFactory
。
但是,我的測試環境是XML,因爲它更容易 - 有什麼方法可以使這項工作?現在,當我運行我的測試時,我所得到的只是錯誤,因爲它無法連接到MySQL,它甚至不應該嘗試這樣做。
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd">
<!-- annotation support -->
<context:annotation-config/>
<!-- support for transaction -->
<tx:annotation-driven transaction-manager="transactionManager"/>
<context:component-scan base-package="com.package.configuration" />
<context:component-scan base-package="com.package.models" />
<!-- H2 datasource -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.h2.Driver"/>
<property name="url" value="jdbc:h2:mem:test;MODE=MySQL;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=TRUE"/>
<property name="username" value="sa"/>
<property name="password" value=""/>
</bean>
<jdbc:initialize-database data-source="dataSource" ignore-failures="DROPS">
<jdbc:script location="classpath:reset_database.sql"/>
<jdbc:script location="classpath:create_testdata.sql"/>
</jdbc:initialize-database>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="persistenceConfig" class="com.package.configuration.PersistenceConfig"/>
<bean id="userDao" class="com.package.models.user.UserDao"/>
</beans>
我的測試類是什麼樣子
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"/test-context.xml",
"classpath:/spring/spring-security.xml"})
@Transactional
public class UserTest {
private UserDao userDao = new UserDao();
private static User user;
@Autowired
@Qualifier("sessionFactory")
private static SessionFactory sessionFactory;
@BeforeClass
public static void initUser() {
user = new User(...);
}
@Test
public void testSave() throws Exception {
userDao.save(user);
User foundUser = userDao.findByUuid(user.getUuid());
assertEquals(foundUser.getUuid(), user.getUuid());
}
}
的sessionFactory
沒有被自動裝配Autowired和userDao
從來沒有得到它。
我的UserDao
在其構造函數中調用PersistenceConfig.sessionFactory()
,而PersistenceConfig
爲它提供對應於MySQL的sessionFactory
。我需要以某種方式將其切換爲H2 sessionFactory
。
我對這個完全錯誤嗎?
嗯,我的'sessionFactory'是沒有得到自動裝配在所有 - 它爲空。 – AVP
您可以嘗試將JNDI名稱設置爲H2 dataSource,然後在測試類中放置一個斷點,並嘗試從上下文中獲取此數據源以查看它是否已創建。只是一個想法... – lauksas