1

There is a situation that I want to use a database image to integration test my service layer. Here is the code I developed to setup my Postgres container image:

@SpringBootTest(classes = EventhandlerApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureMockMvc
public abstract class BaseIT {

public static final PostgreSQLContainer<?> postgresDB = new PostgreSQLContainer<>
        ("postgres:12-alpine")
        .withDatabaseName("test-db")
        .withUsername("postgres")
        .withPassword("password");

static {
    postgresDB.start();
}

@DynamicPropertySource
public static void properties(DynamicPropertyRegistry registry) {
    registry.add("spring.datasource.url", postgresDB::getJdbcUrl);
    registry.add("spring.datasource.username", postgresDB::getUsername);
    registry.add("spring.datasource.password", postgresDB::getPassword);
}
}

Now I want to use the test database to call my service layer methods and inspect the results, here is my integration test example:

public class SampleServiceTesting extends BaseIT {

@Autowired
private SampleService sampleService;


@Test
@Transactional
void testIntegrationFlow() {
    SampleService.exampleMethod();
}
}

But when I run the test, it returns the following error:

org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set

How I can fix this issue on runing my test please?

2
  • I provided an answer, if it resolved the situation please consider to upvote and accept it as answer, otherwise feel free to leave comment. If you using Junit runner have you already tried the @ClassRule before postgresDB declaration ? Commented Dec 15, 2022 at 7:09
  • It may be related to different profile used in your test. Are you executing the IntegrationTests with default profile? If not ensure to provide an ´application-<profile>.yaml´ containing the missing properties. Commented Dec 15, 2022 at 19:54

1 Answer 1

1

You asked

Experiencing HibernateException in testcontainers with using PostgreSQLContainer

The problem lies on not having active connection into the database through a connection pool which can also cause from miss-configuration and incorrect injection of PostgreSQLContainer, cause Hibernate can determine the correct dialect to use automatically, and for doing this it needs a live connection to the database.

One practice could be using of ApplicationContextInitializer interface instead of @DynamicPropertySource for programmatic initialization of the application context. For example, registering datasources or activating profiles against the context's environment. One practice will look like below.

static class PropertyInitializer
   implements ApplicationContextInitializer<ConfigurableApplicationContext> {
    public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
        TestPropertyValues.of(
          "spring.datasource.url=" + postgresDB.getJdbcUrl(),
          "spring.datasource.username=" + postgresDB.getUsername(),
          "spring.datasource.password=" + postgresDB.getPassword()
        ).applyTo(configurableApplicationContext.getEnvironment());
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you @Lunatic, Actually I still can not run the test and I get the following error: dataSource or dataSourceClassName or jdbcUrl is required.

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.