Skip to content

Commit 7e7de79

Browse files
authored
Merge pull request #42435 from geoand/#42406
Update javadoc and docs about `@WithTestResource`
2 parents 2c6d750 + 58c294f commit 7e7de79

File tree

3 files changed

+25
-7
lines changed

3 files changed

+25
-7
lines changed

docs/src/main/asciidoc/getting-started-testing.adoc

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1088,11 +1088,16 @@ If you are using Quarkus Security, check out the xref:security-testing.adoc[Test
10881088

10891089
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`.
10901090

1091-
By simply annotating any test in the test suite with `@WithTestResource`, Quarkus will run the corresponding `QuarkusTestResourceLifecycleManager` before any tests are run.
1092-
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)`.
1091+
When a test annotated with `@WithTestResource`, Quarkus will run the corresponding `QuarkusTestResourceLifecycleManager` before the test.
1092+
1093+
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
1094+
(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,
1095+
test execution speed will be impacted by these restarts.
10931096

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

1099+
NOTE: When using multiple test resources they can be started concurrently. For that you need to set `@WithTestResource(parallel = true)`.
1100+
10961101
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),
10971102
but it is common to create custom implementations to address specific application needs.
10981103
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]),

test-framework/common/src/main/java/io/quarkus/test/common/QuarkusTestResource.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212

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

test-framework/common/src/main/java/io/quarkus/test/common/WithTestResource.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,18 @@
1111
import io.quarkus.test.common.WithTestResource.List;
1212

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

0 commit comments

Comments
 (0)