44

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?

3
  • @ Matt, You have to add MySQL configuration class to establish connection through your mysql-connector-java client. And check whether if it is MySQL driver jar is added in your class path. Commented Dec 24, 2016 at 17:33
  • I was hoping to use the embedded database for JPA testing... @PraveenKumar Commented Dec 24, 2016 at 17:37
  • you need to explicitly set the embedded database in your class path. Commented Dec 25, 2016 at 2:49

3 Answers 3

129

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') 
Sign up to request clarification or add additional context in comments.

5 Comments

If instead of wanting to provide the AutoConfigureTestDatabase, can i specify this in a properties file? i.e spring.test.database.connection: H2
Well you need to tell boot to not override your datasource so that annotation is required. Once you've done that you're free to configure the datasource any way you want. If you want more details, I'd create a seperate issue
Also, I've create an issue in our tracker as I am wondering if we couldn't do something smarter here. Feel free to subscribe for updates or share your toughts on what Spring Boot should do in such situation.
Thanks yes i'll subscribe, it'll be interesting to see the discussion
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
4

You could also configure this via application properties

spring: test: database: replace: none 

Comments

0

Please add this before your class.

@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE) 

it will run for sure.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.