简体   繁体   English

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

[英]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.

相关问题 创建名为'dataSource'+ Spring Boot + Hibernate的bean时出错 - Error creating bean with name 'dataSource' + Spring Boot + Hibernate Spring 引导 - 创建名称为“dataSource”的 bean 在 class 路径资源中定义时出错 - Spring Boot - Error creating bean with name 'dataSource' defined in class path resource Spring 引导错误创建名称为错误的 bean - Spring boot Error creating bean with name Error Spring Boot“使用名称创建bean时出错” - Spring Boot "Error creating bean with name" 创建名为“requestMappingHandlerAdapter”的 bean 时出错 Spring Boot - Error creating bean with name 'requestMappingHandlerAdapter' Spring Boot Spring Boot:创建名称为'methodValidationPostProcessor'的bean时出错 - Spring boot : Error creating bean with name 'methodValidationPostProcessor' Spring Boot 错误创建名为“optionalLiveReloadServer”的bean - Spring Boot Error creating bean with name 'optionalLiveReloadServer' Spring Boot:创建名为“springSecurityFilterChain”的 bean 时出错 - Spring Boot: Error creating bean with name 'springSecurityFilterChain' Spring MVC错误在类中定义了名称为“ dataSource”的bean - Spring MVC Error creating bean with name 'dataSource' defined in class 为什么我尝试在 Spring Boot 应用程序上配置数据库连接时会获得此异常? 创建名为“dataSource”的 bean 时出错 - Why am I obtaining this exception trying to configure DB connection on a Spring Boot application? Error creating bean with name 'dataSource'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM