我正在使用Spring框架配置一个简单的JPA应用程序。 我的目标是在JUnit Test运行期间使用数据填充数据库。我明白这不太理想。但我希望它用于不同的目的。
这是我的persistence.xml
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
version="1.0">
<persistence-unit name="tothought-tutorial-test" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<!-- <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" />
<property name="hibernate.hbm2ddl.auto" value="create-drop" /> -->
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.show_sql" value="true"/>
</properties>
</persistence-unit>
</persistence>
测试context.xml中
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.2.xsd">
<!-- Database -->
<!-- <jdbc:embedded-database id="datasource" type="H2"></jdbc:embedded-database> -->
<bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/to_thought_tutorial" />
<property name="username" value="demo" />
<property name="password" value="demo" />
</bean>
<!-- Entity Manager -->
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="datasource" />
<property name="persistenceUnitName" value="tothought-tutorial-test" />
</bean>
<!-- Transaction Manager -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<!-- Jpa Repositories -->
<jpa:repositories base-package="com.cloudfoundry.tothought.repositories"></jpa:repositories>
</beans>
这是junitTest
类
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:META-INF/test-context.xml")
@Transactional
public class PostPartRepositoryTest {
@Autowired
PostPartRepository repository;
@Test
public void test() {
PostPart postPart = new PostPart();
String body = "Hello";
postPart.setBody(body);
repository.save(postPart);
PostPart dbPostPart = repository.findOne(postPart.getPostPartId());
assertNotNull(dbPostPart);
assertEquals(body, dbPostPart.getBody());
}
@Test
public void insertTest(){
Post post = new Post();
post.setPostDate(new Date());
post.setTitle("First Post");
PostPart postPart = new PostPart();
String body = "Hello";
postPart.setBody(body);
postPart.setPost(post);
repository.save(postPart);
PostPart dbPostPart = repository.findOne(postPart.getPostPartId());
assertNotNull(dbPostPart);
assertNotNull(dbPostPart.getPost());
assertEquals(body, dbPostPart.getBody());
}
}
两个测试通过。但是我没有在表格中看到任何条目,尽管表格是第一次创建的。
答案 0 :(得分:1)
您将看不到任何条目,因为您使用@Transactional
使用SpringJUnit4ClassRunner
运行测试。使用@Transactional
运行测试的默认设置是,一旦测试完成,任何数据库更改都将自动回滚。有关详细信息,请参阅此问题here
这些事情的发生顺序如下:
您可以更改此默认行为,您可以在课程顶部使用@TransactionConfiguration(defaultRollback=false)
。然而,这是不好的做法,因为单元测试不应该相互依赖,也不应该永久修改应用程序状态。此外,如果您依赖于一个测试来在环境中设置某些内容并在下一个测试中使用它,则无法保证测试运行器执行测试的顺序。