CarlosAmds
CarlosAmds

Reputation: 31

What is the minimal required configuration to run @DataJpaTest against real database?

I wish to perform the testing of the models against a real database.

I am able to run the test cases against the default in-memory database.

Problem

I have not been able to configure the project correctly to run the test cases against the real database.

I keep getting this error:

java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: java.lang.IllegalStateException: Unable to retrieve @EnableAutoConfiguration base packages

I have tried the following steps based on the docs.spring.io documentation:


I am creating a Java Spring package that maps a database into ORM @Entity class models.

The package consists in the following:

As you can infer, this project is not meant to be an application, rather is a package containing ORM models that represent a database meant to be used by different projects.


This is an example of the @Entity model I am using: /src/main/java/com/myapp/models/Country.java

@Entity
@Table(name = "country", schema = "public")
public class Country implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "country_country_id_seq")
    @SequenceGenerator(name = "country_country_id_seq", sequenceName = "country_country_id_seq", allocationSize = 1)
    @Column(name = "country_id", nullable = false)
    private Integer countryId;

    @Column(name = "country", nullable = false, length = 50)
    private String country;

    @Column(name = "last_update", nullable = false, columnDefinition = "TIMESTAMP DEFAULT now()")
    private Timestamp lastUpdate;

    // Getters and Setters
}

This is an example of the @DataJpaTest test i am using: /src/test/java/com/myapp/models/CountryTest.java

@DataJpaTest
@Transactional
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
@ContextConfiguration(classes = {TestConfig.class})
public class CountryTest {

    @Autowired
    private EntityManager entityManager;

    private Country country;

    @BeforeEach
    public void setUp() {
        country = new Country();
        country.setCountry("Sample Country");
    }

    @Test
    public void testPersistCountry() {
        entityManager.persist(country);
        entityManager.flush();
        Country foundCountry = entityManager.find(Country.class, country.getCountryId());
        assertNotNull(foundCountry);
        assertEquals(country.getCountry(), foundCountry.getCountry());
    }

    // More tests
}

This is the @Configuration class I am using: /src/test/java/com/java/models/TestConfig.java

@Configuration
@PropertySource("classpath:application-test.properties")
public class TestConfig {

    @Bean
    @ConfigurationProperties(prefix = "test.datasource")
    public DataSource dataSource() {

        return DataSourceBuilder.create().build();
    }
}

This is the .properties file I am using: /src/test/java/resources/application-test.properties

test.datasource.driver-class-name=org.postgresql.Driver
test.datasource.url=jdbc:postgresql://localhost:5432/db_name
test.datasource.username=db_user
test.datasource.password=db_password
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect

This is my current pom.xml

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>3.3.2</version>
    <relativePath/>
</parent>
<dependencies>
    <!-- Spring Boot Starter Data JPA for JPA and Hibernate -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>

    <!-- Spring Boot Starter Test for testing -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

    <!-- Jakarta Persistence API -->
    <dependency>
        <groupId>jakarta.persistence</groupId>
        <artifactId>jakarta.persistence-api</artifactId>
        <version>3.1.0</version>
    </dependency>
    <!-- H2 Database for in-memory testing -->
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <scope>test</scope>
    </dependency>

    <!-- PostgreSQL -->
    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>42.7.3</version>
        <scope>test</scope>
    </dependency>

</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

Upvotes: 1

Views: 56

Answers (0)

Related Questions