Mat*_*att 18 java spring-data-jpa spring-boot
我已经创建了一个新的spring boot 1.4应用程序,希望尝试使用@DataJpaTest进行一些测试,但不断收到以下错误消息
引起:org.springframework.beans.factory.BeanCreationException:创建名为'dataSource'的bean时出错:init方法的调用失败; 嵌套异常是java.lang.IllegalStateException:无法确定测试的嵌入式数据库.如果你想要一个嵌入式数据库,请在类路径上放置一个受支持的数据库.
SRC /主/资源/ 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
Run Code Online (Sandbox Code Playgroud)
MyRepositoryTest
@RunWith(SpringRunner.class)
@DataJpaTest
final public class MyRepositoryTest {
}
Run Code Online (Sandbox Code Playgroud)
的build.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')
}
Run Code Online (Sandbox Code Playgroud)
我有什么想法吗?
Ste*_*oll 52
默认情况下,我们不提供嵌入式数据库.默认情况下,DataJpaTest
您将替换DataSource
为嵌入式数据库,但没有嵌入式数据库.
因此,如果您想使用MySQL进行测试,请按以下步骤替换您的测试:
@RunWith(SpringRunner.class)
@DataJpaTest
@AutoConfigureTestDatabase(replace = NONE)
final public class MyRepositoryTest {
}
Run Code Online (Sandbox Code Playgroud)
如果要对这些测试使用内存数据库,则需要在测试类路径中添加一个.将其添加到您的gradle文件中
testCompile('com.h2database:h2')
Run Code Online (Sandbox Code Playgroud)