Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SpringBoot: Configuring Spring DataSource for Tests

I have a SpringBoot app.

I have created this test:

@ContextConfiguration(classes={TestConfig.class})
@RunWith(SpringRunner.class)
@SpringBootTest
public class SuncionServiceITTest {
    @Test
    public void should_Find_2() {
        // TODO
    }
}

where

@Configuration
@EnableJpaRepositories(basePackages = "com.plats.bruts.repository")
@PropertySource("local-configuration.properties")
@EnableTransactionManagement
@SpringBootApplication(exclude = {SecurityAutoConfiguration.class})
public class TestConfig {
}

and local configuration.properties:

spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1
spring.datasource.username=sa
spring.datasource.password=sa

but when I run the test. I got this error:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' available

I also tried with:

@EnableJpaRepositories(basePackages = "com.plats.bruts.repository", entityManagerFactoryRef="emf")

but then I have the error:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'emf' available

like image 1000
en Lopes Avatar asked Nov 04 '20 08:11

en Lopes


1 Answers

Looks like you are missing below starter dependency. This starter dependency has all the necessary dependencies needed to configure the jpa repositories.

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
like image 200
s7vr Avatar answered Oct 17 '22 23:10

s7vr