1

When running tests in a spring boot based application, spring is unable to create the datasource bean:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceAutoConfiguration$NonEmbeddedConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.sql.DataSource]: Factory method 'dataSource' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Cannot determine embedded database driver class for database type NONE. If you want an embedded database please put a supported one on the classpath.

Reading this error it is clear to me, I think, that spring did not read my application.properties file located at:

src/test/resources/application.properties

For clairity, I do not want to make use of an in memory database when running my integration tests, for application specific reasons.

This file contains:

spring.datasource.url=jdbc:mysql://127.0.2.1:3306/project-test
spring.datasource.username=foo
spring.datasource.password=bar
spring.datasource.driverClassName=org.mariadb.jdbc.Driver

While when starting the application using bootRun at does read out

src/main/resources/application.properties 

and does create the datasource correctly.

My tests are based on cucumber and started using the following class:

@RunWith(Cucumber.class)
public class AcceptanceTests {
}

The test context is started using the following annotations on a BaseSteps class which each class defining cucumber tests inherits from

@WebAppConfiguration
@ContextConfiguration(classes = App.class)

The spring context is started successfuly but it was unable to find my application.properties file and/or use it.

4
  • How is your application.properties file hookedup in your application? Commented Aug 31, 2015 at 7:16
  • Have you tried with jdbc.driverClassName=com.mysql.jdbc.Driver Commented Aug 31, 2015 at 7:18
  • @Craig that's what I'm trying to figure out, how does spring boot hook up the src/test/resources/application.properties file to my application. Commented Aug 31, 2015 at 7:25
  • @SaviNuclear I don't see how adding a property to my configuration file would help since clearly spring does not even find the file in the first place. Commented Aug 31, 2015 at 7:26

2 Answers 2

3

I found the solution.

I added changed the ContextConfiguration like so:

@ContextConfiguration(classes = App.class, loader = SpringApplicationContextLoader.class)
Sign up to request clarification or add additional context in comments.

Comments

0

Use @SpringApplicationConfiguration instead of ContextConfiguration. This is needed for Spring-boot application tests.

3 Comments

When trying this I get: java.lang.IllegalArgumentException: WebApplicationContext is required
@JonasGeiregat add (classes = SampleDataJpaApplication.class) after @SpringApplicationConfiguration
I've also elaborated on how the tests are configured and started, might be important.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.