Skip to main content

Overview

Service is the abstract base class for all TNB services. It provides a common structure for managing accounts, clients, and validation components. Services implement JUnit 5 lifecycle callbacks for setup and teardown.

Class signature

public abstract class Service<A extends Account, C, V extends Validation> 
    implements BeforeAllCallback, AfterAllCallback
Package: software.tnb.common.service Type parameters:
  • A - Account type that extends Account
  • C - Client type (service-specific)
  • V - Validation type that extends Validation

Properties

account
A
The account instance containing credentials and connection details for the service
client
C
The client instance used to interact with the service
validation
V
The validation instance providing test utilities and assertion methods

Methods

account()

Returns the account instance for this service. The account is lazily initialized using AccountFactory.
public A account()
return
A
The Account instance for this service
Example:
Kafka kafka = ServiceFactory.create(Kafka.class);
KafkaAccount account = kafka.account();
String bootstrapServers = account.bootstrapServers();

client()

Returns the client instance for this service.
protected C client()
return
C
The client instance for interacting with the service
This method is protected and typically used internally by service implementations.

validation()

Returns the validation instance for this service, which provides methods for testing and verification.
public V validation()
return
V
The Validation instance for this service
Example:
Kafka kafka = ServiceFactory.create(Kafka.class);
kafka.validation().createTopic("test-topic");

Lifecycle methods

Service implements JUnit 5 extension callbacks:

beforeAll(ExtensionContext)

Called before all tests. Override this method to set up the service (deploy containers, initialize clients, etc.).
public void beforeAll(ExtensionContext extensionContext) throws Exception

afterAll(ExtensionContext)

Called after all tests. Override this method to clean up the service (stop containers, close connections, etc.).
public void afterAll(ExtensionContext extensionContext) throws Exception

Implementation example

Here’s how to extend the Service class:
public class Webhook extends Service<WebhookAccount, NoClient, WebhookValidation> {
    
    @Override
    public void beforeAll(ExtensionContext extensionContext) throws Exception {
        // Initialize the service
        validation = new WebhookValidation();
    }
    
    @Override
    public void afterAll(ExtensionContext extensionContext) throws Exception {
        // Clean up resources
        if (validation != null) {
            validation.cleanup();
        }
    }
}

Generic type resolution

The Service class uses reflection to automatically determine the generic type parameters at runtime. This enables:
  • Automatic account creation via AccountFactory
  • Type-safe client initialization
  • Proper validation instance creation
Account instances are created lazily on first access using the AccountFactory.

Common service patterns

public class SimpleService 
    extends Service<SimpleAccount, SimpleClient, SimpleValidation> {
    
    @Override
    protected SimpleClient client() {
        if (client == null) {
            client = new SimpleClient(account());
        }
        return client;
    }
    
    @Override
    public void beforeAll(ExtensionContext context) throws Exception {
        validation = new SimpleValidation(client());
    }
}

Build docs developers (and LLMs) love