【问题标题】:Spring Boot TestContainers Mapped port can only be obtained after the container is startedSpring Boot TestContainers 映射的端口只能在容器启动后获取
【发布时间】:2020-08-20 19:56:17
【问题描述】:

我正在尝试将使用 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

据我所见,容器应该在开始测试时启动,有人知道我缺少什么吗?

【问题讨论】:

标签: testing spring-data-jpa automated-tests integration-testing testcontainers


【解决方案1】:

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

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

【讨论】:

猜你喜欢
  • 2021-02-08
  • 2020-08-05
  • 2019-09-22
  • 2019-10-19
  • 2017-08-02
  • 1970-01-01
  • 2021-08-10
  • 2021-11-18
  • 2021-03-12
相关资源
最近更新 更多