使用 Micronaut 中的测试容器覆盖配置属性不起作用

Overriding Configuration properties with test container in Micronaut not working

测试容器配置

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@Testcontainers
public abstract class TestContainerFixture {
    @Container
    protected static final GenericContainer mongoDBContainer;
    protected static final Map<String, Object> properties;

    static {
        mongoDBContainer = new GenericContainer(DockerImageName.parse("mongo:4.0.10"))
                .withExposedPorts(27017)
                .withReuse(true);
        mongoDBContainer.start();
        properties = Map.of("mongodb.uri",
                String.format("mongodb://%s:%s/FeteBird-Product",
                        mongoDBContainer.getContainerIpAddress(),
                        mongoDBContainer.getMappedPort(27017)));
    }

    protected static ApplicationContext applicationContext;
}

Application.yml

mongodb:
  uri: "mongodb://${MONGO_HOST:localhost}:${MONGO_PORT:27017}/FeteBird-Product"
  database: "FeteBird-Product"

JUnit 测试

@MicronautTest(rebuildContext = true)
public class SubCategoryCreateTest extends TestContainerFixture {
    @BeforeAll
    public static void initUnitTest() {
        applicationContext = ApplicationContext.run(properties, ConstantValues.TEST_ENVIRONMENT);
    }

    @Test
    @DisplayName("Should create sub category with valid claim as Owner")
    void shouldCreateSubCategoryWithValidClaimAsOwner() {

        // Given
        HttpResponse<CategoryModel> categoryrsp = this.httpRequestToCreateCategory();
        assertEquals(categoryrsp.getStatus(), HttpStatus.CREATED);

        // When
        SubCategoryModel subCategoryModel = new SubCategoryModel(null, "Men swim sweat", "Men swim seat description");
        HttpRequest request = HttpRequest.POST(String.format("%s/%s/%s", "category", categoryrsp.body().id(), "/sub-category"), subCategoryModel)
                .bearerAuth(bearerAccessRefreshToken.getAccessToken());
        HttpResponse<SubCategoryModel> rsp = client.toBlocking().exchange(request, SubCategoryModel.class);
        var item = rsp.body();

        // Then
        assertEquals(rsp.getStatus(), HttpStatus.CREATED);
        assertTrue(item.id() != null);
    }
}

服务层没有覆盖配置,Controller对服务层有依赖

实施 TestPropertyProvider 后更新工作

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@Testcontainers
public class TestContainerFixture implements TestPropertyProvider {
    protected GenericContainer mongoDBContainer;

    @Override
    public Map<String, String> getProperties() {
        mongoDBContainer = new GenericContainer(DockerImageName.parse("mongo:4.0.10"))
                .withExposedPorts(27017)
                .withReuse(true);
        mongoDBContainer.start();
         return Map.of("mongodb.uri",
                        String.format("mongodb://%s:%s/FeteBird-Product",
                                mongoDBContainer.getContainerIpAddress(),
                                mongoDBContainer.getMappedPort(27017)));
    }
}

@MicronautTest
public class SubCategoryCreateTest extends TestPropertyProvider {

    @Test
    @DisplayName("Should create sub category with valid claim as Owner")
    void shouldCreateSubCategoryWithValidClaimAsOwner() {

        // Given
        HttpResponse<CategoryModel> categoryrsp = this.httpRequestToCreateCategory();
        assertEquals(categoryrsp.getStatus(), HttpStatus.CREATED);

        // When
        SubCategoryModel subCategoryModel = new SubCategoryModel(null, "Men swim sweat", "Men swim seat description");
        HttpRequest request = HttpRequest.POST(String.format("%s/%s/%s", "category", categoryrsp.body().id(), "/sub-category"), subCategoryModel)
                .bearerAuth(bearerAccessRefreshToken.getAccessToken());
        HttpResponse<SubCategoryModel> rsp = client.toBlocking().exchange(request, SubCategoryModel.class);
        var item = rsp.body();

        // Then
        assertEquals(rsp.getStatus(), HttpStatus.CREATED);
        assertTrue(item.id() != null);
    }
}

您需要实施 TestPropertyProperty 并在其需要的 getProperties 方法中设置您的数据库 URL

查看 guide here 示例