generated from micronaut-projects/micronaut-project-template
-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement test resources property provider factory #231
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This commit introduces a new annotation, `@TestResourceProperties`, which can be applied on a test in order to tell that the test needs to resolve one or more test resource properties _before_ the application context is available. For example, with: ```java @MicronautTest @TestResourcesProperties( value = "redis.url" ) class SomeTest { ... } ``` Then the value of the `redis.url` property will automatically be fetched from the test resources service and made available as if it had been done in a `TestPropertyProvider`. The difference is that this without this annotation, it was required to call the test resources client directly in the provider, which was error prone. In particular, the `properties` argument of the `resolve` call are not easy to figure out. This annotation is therefore a convention to avoid having to call the client directly. However, in some cases it might be necessary to read a property in order to compute a different one which needs to be available in the test. This can be done by adding a `providers` argument to the annotation: ```java @TestResourcesProperties( value = "rabbitmq.uri", providers = RabbitTest.RabbitMQProvider.class ) class RabbitTest { // ... @ReflectiveAccess public static class RabbitMQProvider implements TestResourcesPropertyProvider { @OverRide public Map<String, String> provide(Map<String, Object> testProperties) { String uri = (String) testProperties.get("rabbitmq.uri"); return Map.of( "rabbitmq.servers.product-cluster.port", String.valueOf(URI.create(uri).getPort()) ); } } } ``` The `TestResourcesPropertyProvider` type has access to all properties which are already resolved at the moment it is called, which includes the properties asked in `@TestResourcesProperties`. This should make the implementation of micronaut-projects/micronaut-nats#321 easier.
graemerocher
requested changes
Apr 28, 2023
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Concept looks fine needs more javadoc and some reference documentation explaining how to use it
...main/java/io/micronaut/test/extensions/testresources/annotation/TestResourcesProperties.java
Show resolved
Hide resolved
...junit5/src/main/java/io/micronaut/test/extensions/testresources/annotation/TestProperty.java
Outdated
Show resolved
Hide resolved
.../src/main/java/io/micronaut/test/extensions/testresources/TestResourcesPropertyProvider.java
Show resolved
Hide resolved
...-resources-extensions/test-resources-extensions-core/src/test/resources/application-test.yml
Show resolved
Hide resolved
graemerocher
approved these changes
May 1, 2023
Some reference docs would be nice |
Yes, but I'd like to finish the improvements first (there should be another PR on this topic, with scope management). |
melix
added a commit
that referenced
this pull request
May 4, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This commit introduces a new annotation,
@TestResourceProperties
, which can be applied on a test in order to tell that the test needs to resolve one or more test resource properties before the application context is available.For example, with:
Then the value of the
redis.url
property will automatically be fetched from the test resources service and made available as if it had been done in aTestPropertyProvider
. The difference is that this without this annotation, it was required to call the test resources client directly in the provider, which was error prone. In particular, theproperties
argument of theresolve
call are not easy to figure out.This annotation is therefore a convention to avoid having to call the client directly. However, in some cases it might be necessary to read a property in order to compute a different one which needs to be available in the test.
This can be done by adding a
providers
argument to the annotation:The
TestResourcesPropertyProvider
type has access to all properties which are already resolved at the moment it is called, which includes the properties asked in@TestResourcesProperties
.This should make the implementation of micronaut-projects/micronaut-nats#321 easier.