gpt4 book ai didi

spring-data-jpa - Spring Boot TestContainers映射的端口只能在容器启动后获得

转载 作者:行者123 更新时间:2023-12-03 17:04:32 25 4
gpt4 key购买 nike

我正在尝试使用TestContainers库将自动化测试添加到我的Spring Boot项目中

这是测试我的jpa存储库的测试类:

package com.ubm.mfi.repo;

import com.ubm.mfi.domain.MasterFileIndexRow;
import org.junit.ClassRule;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.junit.jupiter.Testcontainers;

import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;

@ExtendWith(SpringExtension.class)
@DataJpaTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
@Testcontainers
@ContextConfiguration(initializers = { MasterFileIndexRowRepoTest.Initializer.class })
public class MasterFileIndexRowRepoTest {

@ClassRule
public static PostgreSQLContainer<?> postgreSQLContainer = new PostgreSQLContainer<>("postgres:latest");

@Autowired
private MasterFileIndexRowRepo masterFileIndexRowRepo;

// write test cases here
@Test
public void whenFindAllRows_thenSizeIsGreaterThanZero() {
// when
List<MasterFileIndexRow> rows = masterFileIndexRowRepo.findAll();

// then
assertThat(rows.size())
.isGreaterThan(0);
}

static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {

@Override
public void initialize(ConfigurableApplicationContext configurableApplicationContext) {

TestPropertyValues
.of("spring.datasource.url=" + postgreSQLContainer.getJdbcUrl(),
"spring.datasource.username=" + postgreSQLContainer.getUsername(),
"spring.datasource.password=" + postgreSQLContainer.getPassword())
.applyTo(configurableApplicationContext.getEnvironment());

}

}

}

这是我的build.gradle中的依赖项
testCompile "org.testcontainers:testcontainers:1.14.1"
testCompile "org.testcontainers:postgresql:1.14.1"

运行测试时出现以下错误: Caused by: java.lang.IllegalStateException: Mapped port can only be obtained after the container is started
从我所看到的,容器应该在开始测试时启动,有人知道我所缺少的吗?

最佳答案

您正在尝试将PostgresSQLContainer用作JUnit ClassRule,但是您对@ExtendWith的使用似乎表明您正在使用不支持JUnit 4规则的JUnit 5/Jupiter。

改用Testcontainers的JUnit 5集成:https://www.testcontainers.org/test_framework_integration/junit_5/

关于spring-data-jpa - Spring Boot TestContainers映射的端口只能在容器启动后获得,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61625288/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com