Skip to content
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

Update javadoc and docs about @WithTestResource #42435

Merged
merged 1 commit into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions docs/src/main/asciidoc/getting-started-testing.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -1088,11 +1088,16 @@

A very common need is to start some services on which your Quarkus application depends, before the Quarkus application starts for testing. To address this need, Quarkus provides `@io.quarkus.test.common.WithTestResource` and `io.quarkus.test.common.QuarkusTestResourceLifecycleManager`.

By simply annotating any test in the test suite with `@WithTestResource`, Quarkus will run the corresponding `QuarkusTestResourceLifecycleManager` before any tests are run.
A test suite is also free to utilize multiple `@WithTestResource` annotations, in which case all the corresponding `QuarkusTestResourceLifecycleManager` objects will be run before the tests. When using multiple test resources they can be started concurrently. For that you need to set `@WithTestResource(parallel = true)`.
When a test annotated with `@WithTestResource`, Quarkus will run the corresponding `QuarkusTestResourceLifecycleManager` before the test.

IMPORTANT: By default, `@WithTestResource` applies only to the test on which the annotation is placed. Each test that is annotated with `@WithTestResource` will result in the application being re-augmented and restarted
(in a similar fashion as happens in dev-mode when a change is detected) in order to incorporate the settings configured by the annotation. This means that if there are many instances of the annotation used throughout the testsuite,

Check warning on line 1094 in docs/src/main/asciidoc/getting-started-testing.adoc

View workflow job for this annotation

GitHub Actions / Linting with Vale

[vale] reported by reviewdog 🐶 [Quarkus.Fluff] Depending on the context, consider using 'Be concise: use 'to' rather than' rather than 'in order to'. Raw Output: {"message": "[Quarkus.Fluff] Depending on the context, consider using 'Be concise: use 'to' rather than' rather than 'in order to'.", "location": {"path": "docs/src/main/asciidoc/getting-started-testing.adoc", "range": {"start": {"line": 1094, "column": 58}}}, "severity": "INFO"}

Check warning on line 1094 in docs/src/main/asciidoc/getting-started-testing.adoc

View workflow job for this annotation

GitHub Actions / Linting with Vale

[vale] reported by reviewdog 🐶 [Quarkus.TermsWarnings] Consider using 'to' rather than 'in order to' unless updating existing content that uses the term. Raw Output: {"message": "[Quarkus.TermsWarnings] Consider using 'to' rather than 'in order to' unless updating existing content that uses the term.", "location": {"path": "docs/src/main/asciidoc/getting-started-testing.adoc", "range": {"start": {"line": 1094, "column": 58}}}, "severity": "WARNING"}
test execution speed will be impacted by these restarts.

NOTE: Test resources are applied for a given test class or custom profile. To activate for all tests you can use `@WithTestResource(restrictToAnnotatedClass = false)`.

NOTE: When using multiple test resources they can be started concurrently. For that you need to set `@WithTestResource(parallel = true)`.

Check warning on line 1099 in docs/src/main/asciidoc/getting-started-testing.adoc

View workflow job for this annotation

GitHub Actions / Linting with Vale

[vale] reported by reviewdog 🐶 [Quarkus.Fluff] Depending on the context, consider using 'Rewrite the sentence, or use 'must', instead of' rather than 'need to'. Raw Output: {"message": "[Quarkus.Fluff] Depending on the context, consider using 'Rewrite the sentence, or use 'must', instead of' rather than 'need to'.", "location": {"path": "docs/src/main/asciidoc/getting-started-testing.adoc", "range": {"start": {"line": 1099, "column": 74}}}, "severity": "INFO"}

Check warning on line 1099 in docs/src/main/asciidoc/getting-started-testing.adoc

View workflow job for this annotation

GitHub Actions / Linting with Vale

[vale] reported by reviewdog 🐶 [Quarkus.SentenceLength] Try to keep sentences to an average of 32 words or fewer. Raw Output: {"message": "[Quarkus.SentenceLength] Try to keep sentences to an average of 32 words or fewer.", "location": {"path": "docs/src/main/asciidoc/getting-started-testing.adoc", "range": {"start": {"line": 1099, "column": 125}}}, "severity": "INFO"}

Quarkus provides a few implementations of `QuarkusTestResourceLifecycleManager` out of the box (see `io.quarkus.test.h2.H2DatabaseTestResource` which starts an H2 database, or `io.quarkus.test.kubernetes.client.KubernetesServerTestResource` which starts a mock Kubernetes API server),
but it is common to create custom implementations to address specific application needs.
Common cases include starting docker containers using https://www.testcontainers.org/[Testcontainers] (an example of which can be found https://github.com/quarkusio/quarkus/blob/main/test-framework/keycloak-server/src/main/java/io/quarkus/test/keycloak/server/KeycloakTestResourceLifecycleManager.java[here]),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@

/**
* Used to define a test resource.
*
* <p>
* <b>All</b> {@code QuarkusTestResource} annotations in the test module
* are discovered (regardless of the test which contains the annotation)
* and their corresponding {@code QuarkusTestResourceLifecycleManager}
* started <b>before</b> <b>any</b> test is run.
*
* <p>
* Note that test resources are never restarted when running {@code @Nested} test classes.
*
* @deprecated Use the new {@link WithTestResource} instead. It will be a long while before this is removed, but better to move
Expand Down Expand Up @@ -51,6 +51,10 @@
* Whether this annotation should only be enabled if it is placed on the currently running test class or test profile.
* Note that this defaults to true for meta-annotations since meta-annotations are only considered
* for the current test class or test profile.
* <p>
* Note: When this is set to {@code true} (which is the default), the annotation {@code @WithTestResource} will result
* in the application being re-augmented and restarted (in a similar fashion as happens in dev-mode when a change is
* detected).
*/
boolean restrictToAnnotatedClass() default false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,18 @@
import io.quarkus.test.common.WithTestResource.List;

/**
* Used to define a test resource.
*
* Used to define a test resource, which can affect various aspects of the application lifecycle.
* <p>
* Note: When using the {@code restrictToAnnotatedClass=true} (which is the default), each test that is annotated
* with {@code @WithTestResource} will result in the application being re-augmented and restarted (in a similar fashion
* as happens in dev-mode when a change is detected) in order to incorporate the settings configured by the annotation.
* If there are many instances of the annotation used throughout the testsuite, this could result in slow test execution.
* <p>
* <b>All</b> {@code WithTestResource} annotations in the test module
* are discovered (regardless of the test which contains the annotation)
* and their corresponding {@code QuarkusTestResourceLifecycleManager}
* started <b>before</b> <b>any</b> test is run.
*
* <p>
* Note that test resources are never restarted when running {@code @Nested} test classes.
* <p>
* This replaces {@link QuarkusTestResource}. The only difference is that the default value for
Expand Down Expand Up @@ -55,6 +60,10 @@
* Whether this annotation should only be enabled if it is placed on the currently running test class or test profile.
* Note that this defaults to true for meta-annotations since meta-annotations are only considered
* for the current test class or test profile.
* <p>
* Note: When this is set to {@code true} (which is the default), the annotation {@code @WithTestResource} will result
* in the application being re-augmented and restarted (in a similar fashion as happens in dev-mode when a change is
* detected) in order to incorporate the settings configured by the annotation.
*/
boolean restrictToAnnotatedClass() default true;

Expand Down
Loading