You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot 3.1使用@DataJpaTest与@Testcontainers时DDL异常问题

Spring Boot 3.1 结合 @DataJpaTest 与 Testcontainers MySQL 容器的DDL异常问题

在使用Spring Boot 3.1进行Repository测试时,通过@Testcontainers连接MySQL容器替代嵌入式数据库,同时使用@DataJpaTest注解,会抛出两类DDL执行异常:

  • 尝试删除不存在的test.tab_house表的外键
  • 添加重复的FK_HOUSE_PRO外键约束

尽管测试可以成功运行,但这类异常的出现不符合预期。

异常信息

Hibernate: alter table tab_house drop foreign key FK_HOUSE_PRO
2023-05-26T13:22:51.438+02:00  WARN 249837 --- [           main] o.h.t.s.i.ExceptionHandlerLoggedImpl     : GenerationTarget encountered exception accepting command : Error executing DDL "alter table tab_house drop foreign key FK_HOUSE_PRO" via JDBC [Table 'test.tab_house' doesn't exist]

org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "alter table tab_house drop foreign key FK_HOUSE_PRO" via JDBC [Table 'test.tab_house' doesn't exist]
    ...
Caused by: java.sql.SQLSyntaxErrorException: Table 'test.tab_house' doesn't exist
    ...



Hibernate: drop table if exists tab_house
Hibernate: create table tab_house (pro_house bigint not null,...) engine=InnoDB
Hibernate: alter table tab_house add constraint FK_HOUSE_PRO foreign key (pro_house) references tab_profile (id)
2023-05-26T13:28:58.727+02:00  WARN 250759 --- [           main] o.h.t.s.i.ExceptionHandlerLoggedImpl     : GenerationTarget encountered exception accepting command : Error executing DDL "alter table tab_house add constraint FK_HOUSE_PRO foreign key (pro_house) references tab_profile (id)" via JDBC [Duplicate foreign key constraint name 'FK_HOUSE_PRO']

org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "alter table tab_house add constraint FK_HOUSE_PRO foreign key (pro_house) references tab_profile (id)" via JDBC [Duplicate foreign key constraint name 'FK_HOUSE_PRO']
...
Caused by: java.sql.SQLException: Duplicate foreign key constraint name 'FK_HOUSE_PRO'
    ...

相关配置与代码

配置文件

spring:
  profiles:
    active: test
  jpa:
    hibernate:
      ddl-auto: create-drop

测试类

@DataJpaTest
public class HouseRepositoryTest implements TestDbContainerInitializer {
    @Autowired
    private HouseRepository houseRepository;

    @Test
    public void tester() {
        assert 0 == projectRepository.findAll().size();
    }

}

容器初始化接口

@Testcontainers(disabledWithoutDocker = true)
public interface TestDbContainerInitializer {

    @Container
    public static MySQLContainer<TestDbContainer> mySQLContainer = TestDbContainer.getInstance();

    @DynamicPropertySource
    static void registerMySqlProperties(DynamicPropertyRegistry registry) {
        registry.add("spring.datasource.url", mySQLContainer::getJdbcUrl);
        registry.add("spring.datasource.username", mySQLContainer::getUsername);
        registry.add("spring.datasource.password", mySQLContainer::getPassword);
        registry.add("spring.datasource.driver-class-name",()->"com.mysql.cj.jdbc.Driver");
        registry.add("spring.test.database.replace",()->"none");
    }
}

容器类

public class TestDbContainer extends MySQLContainer<TestDbContainer> {
    public static final String IMAGE_VERSION = "mysql:8.0.33";
    public static TestDbContainer container;

    private TestDbContainer() {
        super(IMAGE_VERSION);
    }

    public static TestDbContainer getInstance() {
        if (container == null) {
            container = new TestDbContainer();
        }
        return container;
    }

    @Override
    public void stop() {
        // Don't stop between Tests. Keep Container running. Let JVM handle shut down after all Tests has been run
    }
}

内容的提问来源于stack exchange,提问作者bentinhoj

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 00:32:46