Skip to content

Commit

Permalink
Revert "Adding new WithTesResource annotation"
Browse files Browse the repository at this point in the history
This reverts commit 5717408.

Revert "Removed missed deprecated `@QuarkusTestResource` from docs"
This reverts commit 6588ea3.

And more as it was more involved than that, since some tests were
removed, modified, added since then.
  • Loading branch information
gsmet committed Aug 27, 2024
1 parent 6fb7703 commit e079ab0
Show file tree
Hide file tree
Showing 235 changed files with 539 additions and 536 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.acme;

import io.quarkus.test.common.WithTestResource;
import io.quarkus.test.common.QuarkusTestResource;
import io.quarkus.test.common.QuarkusTestResourceLifecycleManager;
import io.quarkus.test.junit.QuarkusTest;

Expand Down
8 changes: 4 additions & 4 deletions docs/src/main/asciidoc/datasource.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -700,8 +700,8 @@ This will allow you to test your application even when it is compiled into a nat

. Add the following specific annotation on any class in your integration tests for running integration tests in both JVM or native executables:
+
* `@WithTestResource(H2DatabaseTestResource.class)`
* `@WithTestResource(DerbyDatabaseTestResource.class)`
* `@QuarkusTestResource(H2DatabaseTestResource.class)`
* `@QuarkusTestResource(DerbyDatabaseTestResource.class)`
+
This ensures that the test suite starts and terminates the managed database in a separate process as required for test execution.
+
Expand All @@ -710,10 +710,10 @@ This ensures that the test suite starts and terminates the managed database in a
----
package my.app.integrationtests.db;
import io.quarkus.test.common.WithTestResource;
import io.quarkus.test.common.QuarkusTestResource;
import io.quarkus.test.h2.H2DatabaseTestResource;
@WithTestResource(H2DatabaseTestResource.class)
@QuarkusTestResource(H2DatabaseTestResource.class)
public class TestResources {
}
----
Expand Down
32 changes: 15 additions & 17 deletions docs/src/main/asciidoc/getting-started-testing.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ public class MockGreetingProfile implements QuarkusTestProfile { <1>
* Additional {@link QuarkusTestResourceLifecycleManager} classes (along with their init params) to be used from this
* specific test profile.
*
* If this method is not overridden, then only the {@link QuarkusTestResourceLifecycleManager} classes enabled via the {@link io.quarkus.test.common.WithTestResource} class
* If this method is not overridden, then only the {@link QuarkusTestResourceLifecycleManager} classes enabled via the {@link io.quarkus.test.common.QuarkusTestResource} class
* annotation will be used for the tests using this profile (which is the same behavior as tests that don't use a profile at all).
*/
@Override
Expand Down Expand Up @@ -603,7 +603,7 @@ public class MockGreetingProfile implements QuarkusTestProfile { <1>
public boolean disableApplicationLifecycleObservers() {
return false;
}
@Produces <2>
public ExternalService mockExternalService() {
return new ExternalService("mock");
Expand Down Expand Up @@ -1086,17 +1086,15 @@ If you are using Quarkus Security, check out the xref:security-testing.adoc[Test
[[quarkus-test-resource]]
== Starting services before the Quarkus application starts

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

When a test annotated with `@WithTestResource`, Quarkus will run the corresponding `QuarkusTestResourceLifecycleManager` before the test.
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.QuarkusTestResource` and `io.quarkus.test.common.QuarkusTestResourceLifecycleManager`.

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,
test execution speed will be impacted by these restarts.
By simply annotating any test in the test suite with `@QuarkusTestResource`, Quarkus will run the corresponding `QuarkusTestResourceLifecycleManager` before any tests are run.
A test suite is also free to utilize multiple `@QuarkusTestResource` annotations, in which case all the corresponding `QuarkusTestResourceLifecycleManager` objects will be run before the tests.

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: Test resources are global, even if they are defined on a test class or custom profile, which means they will all be activated for all tests, even though we do
remove duplicates. If you want to only enable a test resource on a single test class or test profile, you can use `@QuarkusTestResource(restrictToAnnotatedClass = true)`.

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

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.
Expand All @@ -1113,7 +1111,7 @@ If for example you have a test like the following:
[source,java]
----
@QuarkusTest
@WithTestResource(MyWireMockResource.class)
@QuarkusTestResource(MyWireMockResource.class)
public class MyTest {
@InjectWireMock // this a custom annotation you are defining in your own application
Expand Down Expand Up @@ -1164,15 +1162,15 @@ any necessary injections into the test class.

=== Annotation-based test resources

It is possible to write test resources that are enabled and configured using annotations. This is enabled by placing the `@WithTestResource`
It is possible to write test resources that are enabled and configured using annotations. This is enabled by placing the `@QuarkusTestResource`
on an annotation which will be used to enable and configure the test resource.

For example, this defines the `@WithKubernetesTestServer` annotation, which you can use on your tests to activate the `KubernetesServerTestResource`,
but only for the annotated test class. You can also place them on your `QuarkusTestProfile` test profiles.

[source,java]
----
@WithTestResource(KubernetesServerTestResource.class)
@QuarkusTestResource(KubernetesServerTestResource.class)
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface WithKubernetesTestServer {
Expand Down Expand Up @@ -1216,12 +1214,12 @@ public class KubernetesServerTestResource
}
----

If you want to make the annotation repeatable, the containing annotation type must be annotated with `@WithTestResourceRepeatable`.
If you want to make the annotation repeatable, the containing annotation type must be annotated with `@QuarkusTestResourceRepeatable`.
For example, this would define a repeatable `@WithRepeatableTestResource` annotation.

[source,java]
----
@WithTestResource(KubernetesServerTestResource.class)
@QuarkusTestResource(KubernetesServerTestResource.class)
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Repeatable(WithRepeatableTestResource.List.class)
Expand All @@ -1231,7 +1229,7 @@ public @interface WithRepeatableTestResource {
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@WithTestResourceRepeatable(WithRepeatableTestResource.class)
@QuarkusTestResourceRepeatable(WithRepeatableTestResource.class)
@interface List {
WithRepeatableTestResource[] value();
}
Expand Down Expand Up @@ -1416,7 +1414,7 @@ public class CustomResource implements QuarkusTestResourceLifecycleManager, DevS
}
----

`CustomResource` would be activated on a `@QuarkusIntegrationTest` using `@WithTestResource` as is described in the corresponding section of this doc.
`CustomResource` would be activated on a `@QuarkusIntegrationTest` using `@QuarkusTestResource` as is described in the corresponding section of this doc.

=== Executing against a running application

Expand Down
4 changes: 2 additions & 2 deletions docs/src/main/asciidoc/kafka-schema-registry-avro.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ what we send.
----
package org.acme.kafka;
import io.quarkus.test.common.WithTestResource;
import io.quarkus.test.common.QuarkusTestResource;
import io.quarkus.test.common.http.TestHTTPResource;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.http.ContentType;
Expand Down Expand Up @@ -607,7 +607,7 @@ public class KafkaAndSchemaRegistryTestResource implements QuarkusTestResourceLi
[source,java]
----
@QuarkusTest
@WithTestResource(KafkaAndSchemaRegistryTestResource.class)
@QuarkusTestResource(KafkaAndSchemaRegistryTestResource.class)
public class MovieResourceTest {
...
}
Expand Down
4 changes: 2 additions & 2 deletions docs/src/main/asciidoc/kafka-schema-registry-json-schema.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ what we send.
----
package org.acme.kafka;
import io.quarkus.test.common.WithTestResource;
import io.quarkus.test.common.QuarkusTestResource;
import io.quarkus.test.common.http.TestHTTPResource;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.http.ContentType;
Expand Down Expand Up @@ -635,7 +635,7 @@ public class KafkaAndSchemaRegistryTestResource implements QuarkusTestResourceLi
[source,java]
----
@QuarkusTest
@WithTestResource(KafkaAndSchemaRegistryTestResource.class)
@QuarkusTestResource(KafkaAndSchemaRegistryTestResource.class)
public class MovieResourceTest {
...
}
Expand Down
12 changes: 6 additions & 6 deletions docs/src/main/asciidoc/kafka.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -2236,7 +2236,7 @@ Create a Quarkus Test using the test resource created above:
import static org.awaitility.Awaitility.await;
@QuarkusTest
@WithTestResource(KafkaTestResourceLifecycleManager.class)
@QuarkusTestResource(KafkaTestResourceLifecycleManager.class)
class BaristaTest {
@Inject
Expand Down Expand Up @@ -2295,7 +2295,7 @@ public class BeverageProcessor {
import static org.awaitility.Awaitility.await;
@QuarkusTest
@WithTestResource(KafkaTestResourceLifecycleManager.class)
@QuarkusTestResource(KafkaTestResourceLifecycleManager.class)
class BaristaTest {
@Inject
Expand Down Expand Up @@ -2380,7 +2380,7 @@ For using `KafkaCompanion` API in tests, start by adding the following dependenc

which provides `io.quarkus.test.kafka.KafkaCompanionResource` - an implementation of `io.quarkus.test.common.QuarkusTestResourceLifecycleManager`.

Then use `@WithTestResource` to configure the Kafka Companion in tests, for example:
Then use `@QuarkusTestResource` to configure the Kafka Companion in tests, for example:

[source, java]
----
Expand All @@ -2391,15 +2391,15 @@ import java.util.UUID;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.junit.jupiter.api.Test;
import io.quarkus.test.common.WithTestResource;
import io.quarkus.test.common.QuarkusTestResource;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.kafka.InjectKafkaCompanion;
import io.quarkus.test.kafka.KafkaCompanionResource;
import io.smallrye.reactive.messaging.kafka.companion.ConsumerTask;
import io.smallrye.reactive.messaging.kafka.companion.KafkaCompanion;
@QuarkusTest
@WithTestResource(KafkaCompanionResource.class)
@QuarkusTestResource(KafkaCompanionResource.class)
public class OrderProcessorTest {
@InjectKafkaCompanion // <1>
Expand Down Expand Up @@ -2430,7 +2430,7 @@ If the Kafka Dev Service is available during tests, `KafkaCompanionResource` use
The configuration of the created Kafka broker can be customized using `@ResourceArg`, for example:
[source,java]
----
@WithTestResource(value = KafkaCompanionResource.class, initArgs = {
@QuarkusTestResource(value = KafkaCompanionResource.class, initArgs = {
@ResourceArg(name = "strimzi.kafka.image", value = "quay.io/strimzi-test-container/test-container:0.106.0-kafka-3.7.0"), // Image name
@ResourceArg(name = "kafka.port", value = "9092"), // Fixed port for kafka, by default it will be exposed on a random port
@ResourceArg(name = "kraft", value = "true"), // Enable Kraft mode
Expand Down
6 changes: 3 additions & 3 deletions docs/src/main/asciidoc/kubernetes-client.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ public class MyTest {
}
----

Alternately, you can create an extension of the `KubernetesServerTestResource` class to ensure all your `@QuarkusTest` enabled test classes share the same mock server setup via the `WithTestResource` annotation:
Alternately, you can create an extension of the `KubernetesServerTestResource` class to ensure all your `@QuarkusTest` enabled test classes share the same mock server setup via the `QuarkusTestResource` annotation:

[source%nowrap,java]
----
Expand All @@ -278,7 +278,7 @@ public class CustomKubernetesMockServerTestResource extends KubernetesServerTest
and use this in your other test classes as follows:
[source%nowrap,java]
----
@WithTestResource(CustomKubernetesMockServerTestResource.class)
@QuarkusTestResource(CustomKubernetesMockServerTestResource.class)
@QuarkusTest
public class KubernetesClientTest {
Expand Down Expand Up @@ -478,7 +478,7 @@ Mock support is also provided in a similar fashion:

[source, java]
----
@WithTestResource(OpenShiftMockServerTestResource.class)
@QuarkusTestResource(OpenShiftMockServerTestResource.class)
@QuarkusTest
public class OpenShiftClientTest {
Expand Down
4 changes: 2 additions & 2 deletions docs/src/main/asciidoc/messaging.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -703,7 +703,7 @@ Create a `@QuarkusTest` using the test resource created above:

[source, java]
----
import io.quarkus.test.common.WithTestResource;
import io.quarkus.test.common.QuarkusTestResource;
import io.quarkus.test.junit.QuarkusTest;
import io.smallrye.reactive.messaging.memory.InMemoryConnector;
import io.smallrye.reactive.messaging.memory.InMemorySink;
Expand All @@ -718,7 +718,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.awaitility.Awaitility.await;
@QuarkusTest
@WithTestResource(InMemoryConnectorLifecycleManager.class)
@QuarkusTestResource(InMemoryConnectorLifecycleManager.class)
class MyMessagingApplicationTest {
@Inject
Expand Down
4 changes: 2 additions & 2 deletions docs/src/main/asciidoc/mongodb.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -639,14 +639,14 @@ To set the desired port MongoDB will listen to when it is launched, the followin
[source,java]
----
@WithTestResource(value = MongoTestResource.class, initArgs = @ResourceArg(name = MongoTestResource.PORT, value = "27017"))
@QuarkusTestResource(value = MongoTestResource.class, initArgs = @ResourceArg(name = MongoTestResource.PORT, value = "27017"))
----
To set the desired MongoDB version that will be launched, the following code should be used:
[source,java]
----
@WithTestResource(value = MongoTestResource.class, initArgs = @ResourceArg(name = MongoTestResource.VERSION, value = "V5_0"))
@QuarkusTestResource(value = MongoTestResource.class, initArgs = @ResourceArg(name = MongoTestResource.VERSION, value = "V5_0"))
----
The string value used can be any of one of the `de.flapdoodle.embed.mongo.distribution.Version` or `de.flapdoodle.embed.mongo.distribution.Version.Main` enums.
Expand Down
2 changes: 1 addition & 1 deletion docs/src/main/asciidoc/native-and-ssl.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ which configures our REST client to connect to an SSL REST service.
For the purposes of this guide, we also need to remove the configuration that starts the embedded WireMock server that stubs REST client responses so the tests actually propagate calls to the https://stage.code.quarkus.io/api. Update the test file `src/test/java/org/acme/rest/client/ExtensionsResourceTest.java` and remove the line:
[source,java]
----
@WithTestResource(WireMockExtensions.class)
@QuarkusTestResource(WireMockExtensions.class)
----
from the `ExtensionsResourceTest` class.

Expand Down
4 changes: 2 additions & 2 deletions docs/src/main/asciidoc/observability-devservices-lgtm.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,11 @@ And for the least 'auto-magical' usage in the tests, simply disable both (Dev Re
quarkus.observability.enabled=false
----

And then explicitly list LGTM Dev Resource in the test as a `@WithTestResource` resource:
And then explicitly list LGTM Dev Resource in the test as a `@QuarkusTestResource` resource:
[source, java]
----
@QuarkusTest
@WithTestResource(LgtmResource.class)
@QuarkusTestResource(value = LgtmResource.class, restrictToAnnotatedClass = true)
@TestProfile(QuarkusTestResourceTestProfile.class)
public class LgtmLifecycleTest extends LgtmTestBase {
}
Expand Down
2 changes: 1 addition & 1 deletion docs/src/main/asciidoc/observability-devservices.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ NOTE: Each Dev Resource implementation is an `@QuarkusTestResourceLifecycleManag
* explicitly disable Dev Services and enable Dev Resources and use less-heavy concept of starting and stopping Dev Resources
* explicitly disable both Dev Services and Dev Resources, and use Quarkus' `@WithTestResource` testing concept (see Note)
* explicitly disable both Dev Services and Dev Resources, and use Quarkus' `@QuarkusTestResource` testing concept (see Note)
You can either add Observability extension dependency along with needed Dev Resources dependencies, or you use existing `sinks` - pom.xml files which add Observability extension dependency along with other required dependencies for certain technology stacks; e.g. `victoriametrics` sink would have `quarkus-observability-devresource-victoriametrics` and `quarkus-victoriametrics-client` dependencies already included in the `pom.xml`.

Expand Down
11 changes: 8 additions & 3 deletions docs/src/main/asciidoc/rest-client.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -1884,13 +1884,13 @@ testImplementation("org.wiremock:wiremock:$wiremockVersion") <1>
----
<1> Use a proper Wiremock version. All available versions can be found link:https://search.maven.org/artifact/org.wiremock/wiremock[here].

In Quarkus tests when some service needs to be started before the Quarkus tests are ran, we utilize the `@io.quarkus.test.common.WithTestResource`
In Quarkus tests when some service needs to be started before the Quarkus tests are ran, we utilize the `@io.quarkus.test.common.QuarkusTestResource`
annotation to specify a `io.quarkus.test.common.QuarkusTestResourceLifecycleManager` which can start the service and supply configuration
values that Quarkus will use.

[NOTE]
====
For more details about `@WithTestResource` refer to xref:getting-started-testing.adoc#quarkus-test-resource[this part of the documentation].
For more details about `@QuarkusTestResource` refer to xref:getting-started-testing.adoc#quarkus-test-resource[this part of the documentation].
====

Let's create an implementation of `QuarkusTestResourceLifecycleManager` called `WiremockExtensions` like so:
Expand Down Expand Up @@ -1954,12 +1954,17 @@ The `ExtensionsResourceTest` test class needs to be annotated like so:
[source,java]
----
@QuarkusTest
@WithTestResource(WireMockExtensions.class)
@QuarkusTestResource(WireMockExtensions.class)
public class ExtensionsResourceTest {
}
----

[WARNING]
====
`@QuarkusTestResource` applies to all tests, not just `ExtensionsResourceTest`.
====

== Known limitations

While the REST Client extension aims to be a drop-in replacement for the RESTEasy Client extension, there are some differences
Expand Down
4 changes: 2 additions & 2 deletions docs/src/main/asciidoc/resteasy-client.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -317,11 +317,11 @@ import static org.hamcrest.Matchers.greaterThan;
import org.acme.rest.client.resources.WireMockExtensionsResource;
import org.junit.jupiter.api.Test;
import io.quarkus.test.common.WithTestResource;
import io.quarkus.test.common.QuarkusTestResource;
import io.quarkus.test.junit.QuarkusTest;
@QuarkusTest
@WithTestResource(WireMockExtensionsResource.class)
@QuarkusTestResource(WireMockExtensionsResource.class)
public class ExtensionsResourceTest {
@Test
Expand Down
Loading

0 comments on commit e079ab0

Please sign in to comment.