繁体   English   中英

Spring Boot 1.4 @DataJpaTest - 创建名为“dataSource”的 bean 时出错

[英]Spring Boot 1.4 @DataJpaTest - Error creating bean with name 'dataSource'

我创建了一个新的 spring boot 1.4 应用程序,想尝试使用 @DataJpaTest 进行一些测试,但不断收到以下错误消息

引起:org.springframework.beans.factory.BeanCreationException: Error created bean with name 'dataSource': Invocation of init method failed; 嵌套异常是 java.lang.IllegalStateException:无法确定用于测试的嵌入式数据库。 如果您想要一个嵌入式数据库,请在类路径上放置一个受支持的数据库。

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

我的存储库测试

@RunWith(SpringRunner.class)
@DataJpaTest
final public class MyRepositoryTest {
}

构建.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')
}

任何想法我做错了什么?

默认情况下,我们不提供嵌入式数据库。 默认情况下, DataJpaTest您的DataSource替换为嵌入式数据库,但您没有。

因此,如果您想使用MySQL进行测试,请按以下步骤替换您的测试:

@RunWith(SpringRunner.class)
@DataJpaTest
@AutoConfigureTestDatabase(replace = NONE)
final public class MyRepositoryTest {
}

如果要对这些测试使用内存数据库,则需要在测试类路径中添加一个。 将其添加到您的gradle文件中

testCompile('com.h2database:h2')

请在您的课程之前添加此内容。

@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)

它肯定会运行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM