Hibernate: Testcontainers tests against the real database

H2 accepts SQL MySQL rejects, so a green suite on H2 proves less than it appears to.

Code
@SpringBootTest
@Testcontainers
class OrderRepositoryTest {

    @Container
    @ServiceConnection
    static MySQLContainer<?> mysql = new MySQLContainer<>("mysql:8.4");

    @Autowired OrderRepository repo;
}
Output
@ServiceConnection wires the datasource automatically -
no spring.datasource.url property needed in the test.
Advertisement

Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.

Published 2026-08-11