9

I am getting errors in my IDE about:

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.

I've checked documentation and creators use everywhere raw type also: https://www.testcontainers.org/quickstart/junit_4_quickstart/ f.e.:

@Rule
public GenericContainer redis = new GenericContainer<>("redis:5.0.3-alpine")
                                        .withExposedPorts(6379);

I dont understand this approach. Can Anyone explain how should I parametrize GenericContainer<>?

masterdany88
  • 5,041
  • 11
  • 58
  • 132

2 Answers2

13

Testcontainers uses the self-typing mechanism:

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

This was a decision to make fluent methods work even if the class is being extended:

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

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

Now, even if there is a child class, it will return the final type, not just GenericContainer:

class MyContainer extends GenericContainer< MyContainer> {
}

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

FYI it is scheduled for Testcontainers 2.0 to change the approach, you will find more info in the following presentation:
https://speakerdeck.com/bsideup/geecon-2019-testcontainers-a-year-in-review?slide=74

bsideup
  • 2,845
  • 17
  • 21
  • 1
    Oh. Now I got it. Thanks. 2.0 version is much nicer. Lets wait. – masterdany88 Jul 18 '19 at 10:50
  • In fact, you can start doing v2-like already. Code snippets from the slides should work with 1.x – bsideup Jul 18 '19 at 12:30
  • 1
    Can You help me using v2 like api, f.e.: ` @ClassRule public static final PostgreSQLContainer userDbService = new PostgreSQLContainer<>().withExposedPorts( POSTGRESQL_PORT).withNetwork(network).withNetworkAliases("userDbService");` How To type it to by generic type save? I am getting error `Raw use of parameterized class 'PostgreSQLContainer' ` – masterdany88 Oct 01 '19 at 07:40
  • 1
    this is not an error, but warning (that can be suppressed). v2 will remove the generic type and the warning will disappear. – bsideup Oct 02 '19 at 11:42
5

If you declare it like

PostgreSQLContainer<?> container = new PostgreSQLContainer<>(
        "postgres:9.6.12")
        .withDatabaseName("mydb");

the warning also goes away.

Stefan Reisner
  • 607
  • 5
  • 12