Interface RestResourceProducer
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 configurationClient- REST client instanceWebTarget- WebTarget (optionally qualified with@RequestPath)UriBuilder- URI builderURI- 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 ClassesModifier and TypeInterfaceDescriptionstatic enumDefines the lifecycle scope for resources produced by aRestResourceProducer. -
Method Summary
Modifier and TypeMethodDescriptionbooleancanInject(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.default RestResourceProducer.Scopescope()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
trueonly 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 instanceclazz- the type being requested for injection (field type or parameter type)qualifiers- annotation qualifiers present on the injection point (e.g.,@RequestPath,@RestClientConfig)- Returns:
trueif this producer can create instances of the given type,falseotherwise
-
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...)returnstruefor the given type. It should create and return an appropriate instance. If the returned object implementsAutoCloseable, it will be automatically closed when the appropriate test context ends.Implementations should validate that they can produce the requested type and throw
IllegalArgumentExceptionif called with an unsupported type.- Parameters:
context- the current JUnit extension context, provides access to the test class and instanceclazz- the type of the field or parameter to injectqualifiers- annotation qualifiers present on the injection point (e.g.,@RequestPath,@RestClientConfig)- Returns:
- the newly created object to be injected, or
nullif it could not be produced - Throws:
IllegalArgumentException- if the type cannot be produced by this producer (implementations should validate viacanInject(ExtensionContext, Class, Annotation...)first)
-
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.CLASSfor expensive-to-create resources that should be shared across all test methods in a class.- Returns:
- the scope for resources produced by this producer
-