简体   繁体   English

创建自定义测试容器模块?

[英]Create custom testcontainer modules?

I am using JUnit5 + Testcontainers.我正在使用 JUnit5 + 测试容器。

I see there are Testcontainer modules such as these: https://github.com/testcontainers/testcontainers-java/tree/main/modules我看到有这样的 Testcontainer 模块: https://github.com/testcontainers/testcontainers-java/tree/main/modules

I would like to build my own Testcontainers custom module.我想构建自己的 Testcontainers 自定义模块。

Is there a way for developers to build their own?有没有办法让开发人员构建自己的?

Yes, you can extend the GenericContainer class. By overriding methods like containerIsStarted or adding your own methods, you can customize the container's behavior.是的,您可以扩展GenericContainer class。通过重写containerIsStarted等方法或添加您自己的方法,您可以自定义容器的行为。

Have a look at how these modules are implemented, eg, the MongoDB one .看看这些模块是如何实现的,例如 MongoDB 一个 It overrides containerIsStarted to initialize a replica set:它覆盖containerIsStarted以初始化副本集:

@Override
protected void containerIsStarted(InspectContainerResponse containerInfo, boolean reused) {
    if (reused && isReplicationSetAlreadyInitialized()) {
        log.debug("Replica set already initialized.");
    } else {
        initReplicaSet();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM