gpt4 book ai didi

testcontainers - 测试容器中的 GenericContainer 应该如何参数化?

转载 作者:行者123 更新时间:2023-12-04 16:01:47 26 4
gpt4 key购买 nike

我在 IDE 中遇到以下错误:

Raw use of parameterized class 'GenericContainer' Inspection info: Reports any uses of parameterized classes where the type parameters are omitted. Such raw uses of parameterized types are valid in Java, but defeat the purpose of using type parameters, and may mask bugs.



我检查了文档,创建者也到处使用原始类型:
https://www.testcontainers.org/quickstart/junit_4_quickstart/
例如:
@Rule
public GenericContainer redis = new GenericContainer<>("redis:5.0.3-alpine")
.withExposedPorts(6379);

我不明白这种方法。任何人都可以解释我应该如何参数化 GenericContainer<>?

最佳答案

测试容器使用自类型机制:

class GenericContainer<SELF extends GenericContainer<SELF>> implements Container<SELF> {
...
}

这是一个让 fluent 方法即使在扩展类的情况下也能工作的决定:

class GenericContainer<SELF extends GenericContainer<SELF>> implements Container<SELF> {

public SELF withExposedPorts(Integer... ports) {
this.setExposedPorts(newArrayList(ports));
return self();
}
}

现在,即使有子类,它也会返回最终类型,而不仅仅是 GenericContainer :

class MyContainer extends GenericContainer< MyContainer> {
}

MyContainer container = new MyContainer()
.withExposedPorts(12345); // <- without SELF, it would return "GenericContainer"

仅供引用,Testcontainers 2.0 计划更改方法,您将在以下演示文稿中找到更多信息:
https://speakerdeck.com/bsideup/geecon-2019-testcontainers-a-year-in-review?slide=74

关于testcontainers - 测试容器中的 GenericContainer 应该如何参数化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57059260/

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