Dependency injection

Dependency injection through interface default methods

interface AuthenticationMixin {

  class Private {
    private static AuthComponent authComponent;
  }

  @Autowired
  default void setDependencies(AuthComponent authComponent) {
    Private.authComponent = authComponent;
  }

  default void authenticate() {
    Private.authComponent.authenticate();
  }
}

@SpringBootTest(webEnvironment = RANDOM_PORT, classes = Application.class)
abstract class IntegrationTest implements AuthenticationMixin { }

class ExampleTest extends IntegrationTest {

  @Test
  void shouldTestSomething() {
    authenticate();
    // rest of the code
  }
}