[英]Spring Boot 1.4 @DataJpaTest - Error creating bean with name 'dataSource'
I've created a new spring boot 1.4 application, want to try some testing using @DataJpaTest but keep getting the following error message我创建了一个新的 spring boot 1.4 应用程序,想尝试使用 @DataJpaTest 进行一些测试,但不断收到以下错误消息
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource': Invocation of init method failed;引起:org.springframework.beans.factory.BeanCreationException: Error created bean with name 'dataSource': Invocation of init method failed; nested exception is java.lang.IllegalStateException: Cannot determine embedded database for tests.
嵌套异常是 java.lang.IllegalStateException:无法确定用于测试的嵌入式数据库。 If you want an embedded database please put a supported one on the classpath.
如果您想要一个嵌入式数据库,请在类路径上放置一个受支持的数据库。
src/main/resources/application.properties src/main/resources/application.properties
spring.datasource.url=jdbc:mysql://localhost/my_db
spring.datasource.username=user
spring.datasource.password=password
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
MyRepositoryTest我的存储库测试
@RunWith(SpringRunner.class)
@DataJpaTest
final public class MyRepositoryTest {
}
build.gradle构建.gradle
dependencies {
compile 'org.springframework.boot:spring-boot-starter-web',
'org.springframework.boot:spring-boot-starter-data-jpa',
'mysql:mysql-connector-java',
'org.projectlombok:lombok:1.16.10'
testCompile('org.springframework.boot:spring-boot-starter-test')
}
Any ideas what i am doing wrong?任何想法我做错了什么?
We don't provide an embedded database by default. 默认情况下,我们不提供嵌入式数据库。 By default
DataJpaTest
replaces your DataSource
with an embedded database but you don't have one. 默认情况下,
DataJpaTest
您的DataSource
替换为嵌入式数据库,但您没有。
So, if you want to test with MySQL, replace your test as follows: 因此,如果您想使用MySQL进行测试,请按以下步骤替换您的测试:
@RunWith(SpringRunner.class)
@DataJpaTest
@AutoConfigureTestDatabase(replace = NONE)
final public class MyRepositoryTest {
}
If you want to use an in-memory database for those tests, you need to add one to the test classpath. 如果要对这些测试使用内存数据库,则需要在测试类路径中添加一个。 Add this to your gradle file
将其添加到您的gradle文件中
testCompile('com.h2database:h2')
Please add this before your class.请在您的课程之前添加此内容。
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
it will run for sure.它肯定会运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.