Spring Boot 1.4 @DataJpaTest - 创建名为'dataSource'的bean时出错 [英] Spring Boot 1.4 @DataJpaTest - Error creating bean with name 'dataSource'

查看:742
本文介绍了Spring Boot 1.4 @DataJpaTest - 创建名为'dataSource'的bean时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've created a new spring boot 1.4 application, want to try some testing using @DataJpaTest but keep getting the following error message

引起: org.springframework.beans.factory.BeanCreationException:创建名为'dataSource'的bean时出错:init方法的调用失败;嵌套异常是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

MyRepositoryTest

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

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')
}

任何想法我做错了什么?

Any ideas what i am doing wrong?

推荐答案

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

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.

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

So, if you want to test with MySQL, replace your test as follows:

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

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

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

testCompile('com.h2database:h2')

这篇关于Spring Boot 1.4 @DataJpaTest - 创建名为'dataSource'的bean时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆