Jonas Geiregat
Jonas Geiregat

Reputation: 5442

Spring boot cannot create datasource bean from cucumber test context

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.

Upvotes: 1

Views: 2478

Answers (2)

Jonas Geiregat
Jonas Geiregat

Reputation: 5442

I found the solution.

I added changed the ContextConfiguration like so:

@ContextConfiguration(classes = App.class, loader = SpringApplicationContextLoader.class)

Upvotes: 3

Jens
Jens

Reputation: 69460

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

Upvotes: 0

Related Questions