Java 在 Spring Boot 测试之前初始化数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38262430/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Initialising a database before Spring Boot test
提问by px06
I'm using JUnit to test my application and everything works fine as long as the database has been initialised before the testing (using gradle bootRun
to run as a web-app). However, if the database is empty, the application does not seem to initialise any models or entities before testing. Is there a way I'm supposed to do this? I made an assumption that the ApplicationRunner
class will be ran before the test and initalise the entities. Is there a way to do this or am I using the wrong approach?
我正在使用 JUnit 来测试我的应用程序,只要数据库在测试之前已经初始化(gradle bootRun
用作 Web 应用程序运行),一切都可以正常工作。但是,如果数据库为空,则应用程序在测试之前似乎不会初始化任何模型或实体。有没有办法我应该这样做?我假设ApplicationRunner
该类将在测试之前运行并初始化实体。有没有办法做到这一点,还是我使用了错误的方法?
This is how my application.properties
file is looking like:
这是我的application.properties
文件的样子:
server.port=8090
server.ssl.key-store=classpath:keystore.jks
server.ssl.key-store-password=123456
server.ssl.key-password 123456
spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect
spring.jpa.hibernate.ddl-auto=create
spring.jpa.hibernate.naming-strategy:org.hibernate.cfg.ImprovedNamingStrategy
application.logger.org.springframework=INFO
My database is stored in /src/main/java/application/persistence/DbConfig.java
using a DriverManagerDataSource
connection. And I have setup ApplicationRunner
to run add a few rows to the db upon starting.
我的数据库是/src/main/java/application/persistence/DbConfig.java
使用DriverManagerDataSource
连接存储的。我已经设置ApplicationRunner
为在启动时向数据库添加几行。
edit:
编辑:
I should also add that these are the annotations I'm using on the JUnit test file:
我还应该补充一点,这些是我在 JUnit 测试文件中使用的注释:
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(classes={
AdeyTrackApplication.class,
SecurityConfig.class,
WebConfig.class,
AuthorizationController.class
})
采纳答案by px06
The above answers all use the .sql schema loading technique where I'd have to have a .sql schema for tests. I didn't want to do it that way as my schema would be expanding and I'd rather not go through the hassle of adding entries to the schema as my tests expand.
上面的答案都使用 .sql 模式加载技术,我必须有一个 .sql 模式进行测试。我不想那样做,因为我的模式会扩展,而且我不想在我的测试扩展时经历向模式添加条目的麻烦。
As I'm using Spring Boot, I came across this annotation which seems to solve the issue by first running bootRun
and then running the tests.
当我使用 Spring Boot 时,我遇到了这个注释,它似乎通过首先运行bootRun
然后运行测试来解决问题。
In my test annotations I replaced the @ContextConfigurations
with @SpringApplicationConfiguration
and left all the classes to be the same. This seemed to solve the issue. So now the test
task invokes bootRun to load the classes and thenruns the tests.
在我的测试注释中,我替换了@ContextConfigurations
with@SpringApplicationConfiguration
并使所有类保持相同。这似乎解决了这个问题。所以现在test
任务调用 bootRun 来加载类,然后运行测试。
See @SpringApplicationConfiguration
见@SpringApplicationConfiguration
Hop this helps anyone facing the same issue.
跳这可以帮助任何面临同样问题的人。
回答by luboskrnac
There are various options if you do not want to execute that explicitly from @Before
JUnit hook.
如果您不想从@Before
JUnit 钩子中显式执行,则有多种选择。
- Use Spring Boot's JDBC initialization feature, where you would place
schema.sql
ordata.sql
intosrc/test/resources
folder, so that it would be picked up only during testing. - Use Spring's @Sql annotation
- 使用Spring Boot 的 JDBC 初始化功能,您可以在其中放置
schema.sql
或data.sql
放入src/test/resources
文件夹,以便仅在测试期间将其取出。 - 使用Spring 的 @Sql 注解
回答by MxWild
You can use @Sql annotaion for populate your DB, for example^
您可以使用@Sql annotaion 来填充您的数据库,例如^
@Sql(scripts = "classpath:db/populateDB.sql")