lundi 22 mars 2021

Spring, is there a way to get around not being able to extend 2 classes in Java?

I have an integration test with the following signature:

@SpringBootTest(classes = Example.class)
@ActiveProfiles("test")
public class CoolTest_IntegrationTest extends MyTestFrameworkAbstractClass {

PrismIntegrationFramework is an abstract class with a bunch of setup methods to make testing easier for people on my team.

Now, in Spring I heavily rely on DynamicPropertySource for injection dynamic Ip addresses, etc coming from my test containers.

Ideally, I want a separate "test harness" that I could inject/invoke based on the type of test. IE - I would have a GraphDBHarness that would be invoked when I write graph database integration tests.

@SpringBootTest(classes = GraphClient.class)
@ActiveProfiles("test")
public class CoolTest_IntegrationTest extends MyTestFrameworkAbstractClass extends GraphDBHarness {

I know the above is invalid, but if this was valid then I would internally have something like :

public abstract class GremlinTestHarness {

  @DynamicPropertySource
  static void executeDynamicPropertyInjection(DynamicPropertyRegistry registry) {
    registry.add(
        GREMLIN_SERVER_KEY, () -> SupportedContainers.gremlinServer.getContainerIpAddress());
    registry.add(GREMLIN_SERVER_PORT, () -> SupportedContainers.gremlinServer.getFirstMappedPort());
  }
}

So in theory, if I had N of these, then I could inject the things I need or make the things I need available at will. Is this possible in Spring somehow?

Aucun commentaire:

Enregistrer un commentaire