Home > Back-end >  Spring boot application with Spring-boot-starter-data-jpa and spring-boot-starter-test makes spring
Spring boot application with Spring-boot-starter-data-jpa and spring-boot-starter-test makes spring

Time:08-24

I have created a small application with spring boot that access to a database (mySql) using spring data:

  • spring-boot-starter-parent: 2.4.2
  • spring-boot-starter-web: 2.4.2
  • spring-boot-starter-data-jpa: 2.4.2
  • mysql-connector-java: 8.0.22
  • spring-boot-starter-test: 2.4.2
  • org.junit.jupiter: 5.7.0
  • junit-jupiter-engine: 5.7.0

I have configured my application.properties, inside src/main/resources, and I have configured the properties for url, username, password, driver, ..., like this:

spring.datasource.url=jdbc:mysql://localhost:3306/BBDD
spring.datasource.username=XXX
spring.datasource.password=XXXX
spring.datasource.driver=com.mysql.jdbc.Driver
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect:org.hibernate.dialect.MySQL5Dialect

spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

I have an interface for my repository for access to my database:

@Repository
public interface ArticleRepository extends PagingAndSortingRepository<ArticuloEntity, Integer> {

}

also, I have defined my entity.

The issue consists on when I start my webapp, I get an error because it does not read the parameters for configuring the datasource from the application.properties:

Description:

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

Reason: Failed to determine a suitable driver class

Action:

Consider the following: If you want an embedded database (H2, HSQL or Derby), please put it on the classpath. If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).

If I remove the dependencies for spring-boot-starter-test and junit, the application loads fine, and it access to database. But if I add the dependency for adding my junits, it does not read my application.properties file and I get the previous error.

How can I have my application configured with junits and reading the configuration for my application with the application.properties file?

Thank you in advance!

CodePudding user response:

Your test/application.properties needs to be configured as well. Try adding this to your test application properties file

spring.datasource.url=jdbc:h2:mem:BBDD;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE

and this to your pom if you don't already support h2.

    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <scope>test</scope>
    </dependency>

CodePudding user response:

check your application.properties

changing

spring.datasource.driver=com.mysql.jdbc.Driver

to

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
  • Related