我正在尝试创建一个使用嵌入式 H2 数据库的测试。但是我必须更改 spring.datasource.url,我不能使用 spring boot 创建的默认值。 (这是因为我要把H2数据库的模式改成MYSQL)
这是我的测试类
:
@JdbcTest
@RunWith(SpringRunner.class)
@ActiveProfiles("test")
public class DemoApplicationTests {
@Autowired
JdbcTemplate jdbcTemplate;
@Autowired
DataSource dataSource;
@Test
public void contextLoads() {
System.out.println(dataSource);
}
}
这是我的application-test.properties
:
spring.datasource.url=jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE;MODE=MYSQL
spring.datasource.username=dbuser
spring.datasource.password=dbpass
我的依赖项:
compile('org.springframework.boot:spring-boot-starter-batch')
runtime('com.h2database:h2')
compile group: 'org.springframework.boot', name: 'spring-boot-starter-jdbc', version: '1.5.3.RELEASE'
compile group: 'org.springframework.boot', name: 'spring-boot-starter-test', version: '1.5.3.RELEASE'
控制台输出:
Starting embedded database: url='jdbc:h2:mem:bfad6b71-3e2d-4a47-a32d-c76988b3c5f6;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=false', username='sa'
我希望 url 是这样的:jdbc:h2:mem:testdb
,我还希望它采用 MODE=MYSQL
设置。
我试着关注 this post , 但它没有用。
最佳答案
您需要告诉 Spring 不要用以下内容替换随机的嵌入式数据库名称:
@AutoConfigureTestDatabase(replace= AutoConfigureTestDatabase.Replace.NONE)
然后它应该获取您在 application.properties 中声明的属性。
是否适合您,请随时表扬。
关于java - H2 嵌入式数据库在 spring boot 测试期间未获取属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44134297/