【发布时间】:2015-05-01 05:56:29
【问题描述】:
我正在尝试为 Spring Boot (Spring 4) 应用程序编写测试。
我的 Junit 测试类被配置为允许自动装配。
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = SpringApp.class)
public class MyServiceTest {
...
我的src/main/resources/application.properties是这样的
spring.jpa.database=POSTGRESQL
spring.jpa.show-sql=false
spring.jpa.hibernate.ddl-auto=update
spring.datasource.driverClassName=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost/mydb
spring.datasource.username=BNF0016779
spring.datasource.password=
在测试上下文中,src/test/resources/application.properties 只是空的。
可以像往常一样查询数据库,创建对象...
但我想创建一个数据初始化 sql。
从一个奇怪的行为开始,似乎 Spring 在类路径中加载了任何“schema.sql”。 不需要像下面这样的东西?
//This is not required to execute schema.sql
@Configuration
public class DatabaseTestConfig {
@Bean
public DataSource dataSource() {
return new EmbeddedDatabaseBuilder()
.setType(EmbeddedDatabaseType.H2)
.addScript("classpath:schema.sql")
.build();
}
}
然后,我无法从此 SQL 创建任何表。
总是收到org.h2.jdbc.JdbcSQLException: Table "MY_TABLE" already exists; SQL statement:
H2 应该是一个内存数据库,两次启动之间不保存数据! 为什么我会收到这些错误?
有什么想法吗? 谢谢
【问题讨论】:
标签: java spring junit spring-test