【发布时间】:2014-02-14 08:37:07
【问题描述】:
我现在正在用 Hibernate 学习 Spring。我有一个 POJO 模型类,用 Hibernate 注释进行注释,称为 Person、PersonDao 接口,它是休眠实现和 PersonService 类。我正在使用注释,因此它们没有在我的 spring-config.xml 中定义为 bean。现在我想为我的 PersonService 类编写一些 JUnit4 测试,但我想在测试时使用不同的数据库。这是我的 spring-config.xml
<context:component-scan base-package="org.example" />
<tx:annotation-driven />
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost/example" />
<property name="username" value="root" />
<property name="password" value="pwd" />
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<property name="annotatedClasses">
<list>
...
</list>
</property>
</bean>
我希望在我的测试中使用 jdbc:mysql://localhost/example_test 数据库。我该如何实现?
【问题讨论】:
-
你的单元测试不应该使用数据库,因为他们不应该测试数据库,而是你的类,而是尝试类似 mockito code.google.com/p/mockito
标签: java spring hibernate junit