Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource': Invocation of init method failed; nested exception is java.lang.IllegalStateException: Cannot determine embedded database for tests. If you want an embedded database please put a supported one on the classpath.

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?

like image 804
Matt Avatar asked Dec 24 '16 17:12

Matt


People also ask

What is Error creating bean with name?

BeanCreationException: Error creating bean with name happens when a problem occurs when the BeanFactory creates a bean. If the BeanFactory encounters an error when creating a bean from either bean definition or auto-configuration, the BeanCreationException will be thrown.

What is DataJpaTest?

@DataJpaTest is used to test JPA repositories. It is used in combination with @RunWith(SpringRunner. class) . The annotation disables full auto-configuration and applies only configuration relevant to JPA tests. By default, tests annotated with @DataJpaTest use an embedded in-memory database.

What is @AutoConfigureTestDatabase?

Annotation Type AutoConfigureTestDatabaseAnnotation that can be applied to a test class to configure a test database to use instead of the application-defined or auto-configured DataSource . In the case of multiple DataSource beans, only the @Primary DataSource is considered.

What is spring DataSource URL?

A DataSource is a factory for connections to the physical databases. It is an alternative to the DriverManager facility. A datasource uses a URL along with username/password credentials to establish the database connection. In Java, a datasource implements the javax. sql.


1 Answers

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.

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

@RunWith(SpringRunner.class) @DataJpaTest @AutoConfigureTestDatabase(replace = 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

testCompile('com.h2database:h2') 
like image 148
Stephane Nicoll Avatar answered Sep 21 '22 12:09

Stephane Nicoll