问题描述
使用junit4测试spring服务层的下一个问题是:
如何在所有@Test方法之前调用仅填充一次数据库的脚本:
我想在所有@Tests之前执行一次:
My next problem testing spring service layer with junit4 is:How to call script that populates database only once before all @Test methods:I want to execute this once before all @Tests:
JdbcTestUtils.executeSqlScript(jdbcTemplate(), new FileSystemResource(
"src/main/resources/sql/mysql/javahelp-insert.sql"), false);
我试图在我的GenericServiceTest类上使用@PostConstruct(由测试类扩展)。
事实证明每次@Test方法之前都会调用@PostConstruct。有趣的是,即使是注释了@Autowired of GenericServiceTest的方法也会在每个@Test方法之前调用。
I tried to use @PostConstruct on my GenericServiceTest class(extended by test classes).It turned out that @PostConstruct is called every time before every @Test method. Interesting is that even methods annotated @Autowired of GenericServiceTest are called before every @Test method.
我不希望在每个测试类之前填充数据库但在春天只需要填充一次-test startup。
I don't want to populate database before every test class but only once at spring-test startup.
如何在使用spring test framework和junit4的所有@Test方法之前只执行一次上面的方法?
谢谢!
推荐答案
使用Springs嵌入式数据库支持
Use Springs Embedded Database Support
<jdbc:embedded-database id="dataSource">
<jdbc:script location="classpath:myScript.sql"/>
<jdbc:script location="classpath:otherScript.sql"/>
</jdbc:embedded-database>
或Springs Initialize Database Support
or Springs Initialize Database Support
<jdbc:initialize-database data-source="dataSource">
<jdbc:script location="classpath:myScript.sql"/>
<jdbc:script location="classpath:otherScript.sql"/>
</jdbc:initialize-database>
@See
这篇关于如何在spring test中的@Test方法之前只填充一次数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!