Interface RestResourceProducer


public interface RestResourceProducer
Enables injection of custom types into test classes.

Implementations of this interface can provide custom objects for injection into test fields (static or instance) and method parameters (constructor, lifecycle methods, or test methods). All injectable fields and parameters must be annotated with @RestResource.

Built-in Producers

The extension provides built-in producers for:

  • SeBootstrap.Configuration - The SeBootstrap configuration
  • Client - REST client instance
  • WebTarget - WebTarget (optionally qualified with @RequestPath)
  • UriBuilder - URI builder
  • URI - Base URI (optionally qualified with @RequestPath)

Custom Producer Example

To inject custom types, implement this interface and register via ServiceLoader:

public class DataSourceProducer implements RestResourceProducer {
    @Override
    public boolean canInject(ExtensionContext context, Class<?> clazz, Annotation... qualifiers) {
        return DataSource.class.isAssignableFrom(clazz);
    }

    @Override
    public Object produce(ExtensionContext context, Class<?> clazz, Annotation... qualifiers) {
        // Create and return your DataSource instance
        return createDataSource();
    }

    private DataSource createDataSource() {
        // Implementation details...
    }
}

// Now you can inject DataSource in tests:
@RestBootstrap(MyApp.class)
public class MyTest {
    @RestResource
    private DataSource dataSource;

    @Test
    public void testDatabase() {
        // Use dataSource...
    }
}

Registration

Custom producers must be registered via ServiceLoader by creating a file META-INF/services/dev.resteasy.junit.extension.api.RestResourceProducer containing the fully-qualified class name of your implementation.

Lifecycle

If the produced object implements AutoCloseable, it will be automatically closed when the test context ends.

Since:
1.0
Author:
James R. Perkins
See Also:
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Interface
    Description
    static enum 
    Defines the lifecycle scope for resources produced by a RestResourceProducer.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    canInject(org.junit.jupiter.api.extension.ExtensionContext context, Class<?> clazz, Annotation... qualifiers)
    Determines whether this producer can inject the specified type.
    produce(org.junit.jupiter.api.extension.ExtensionContext context, Class<?> clazz, Annotation... qualifiers)
    Produces an instance of the requested type for injection.
    Returns the lifecycle scope for resources produced by this producer.
  • Method Details

    • canInject

      boolean canInject(org.junit.jupiter.api.extension.ExtensionContext context, Class<?> clazz, Annotation... qualifiers)
      Determines whether this producer can inject the specified type.

      This method is called by the extension to find the appropriate producer for each injection point. It should return true only if this producer can create instances of the given type, optionally considering any qualifiers present.

      Parameters:
      context - the current JUnit extension context, provides access to the test class and instance
      clazz - the type being requested for injection (field type or parameter type)
      qualifiers - annotation qualifiers present on the injection point (e.g., @RequestPath, @RestClientConfig)
      Returns:
      true if this producer can create instances of the given type, false otherwise
    • produce

      Object produce(org.junit.jupiter.api.extension.ExtensionContext context, Class<?> clazz, Annotation... qualifiers) throws IllegalArgumentException
      Produces an instance of the requested type for injection.

      This method is called when canInject(ExtensionContext, Class, Annotation...) returns true for the given type. It should create and return an appropriate instance. If the returned object implements AutoCloseable, it will be automatically closed when the appropriate test context ends.

      Implementations should validate that they can produce the requested type and throw IllegalArgumentException if called with an unsupported type.

      Parameters:
      context - the current JUnit extension context, provides access to the test class and instance
      clazz - the type of the field or parameter to inject
      qualifiers - annotation qualifiers present on the injection point (e.g., @RequestPath, @RestClientConfig)
      Returns:
      the newly created object to be injected, or null if it could not be produced
      Throws:
      IllegalArgumentException - if the type cannot be produced by this producer (implementations should validate via canInject(ExtensionContext, Class, Annotation...) first)
    • scope

      default RestResourceProducer.Scope scope()
      Returns the lifecycle scope for resources produced by this producer.

      The default implementation returns RestResourceProducer.Scope.DEFAULT, which uses the natural scope of the injection point: method parameters are cleaned up after each test method, while fields are cleaned up when the test class completes.

      Producers can override this method to return RestResourceProducer.Scope.CLASS for expensive-to-create resources that should be shared across all test methods in a class.

      Returns:
      the scope for resources produced by this producer