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

[4.x] - Add @RequestScoped support for testing #7916

Merged
merged 29 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
ec3b0db
Add @RequestScoped support
dalexandrov Oct 30, 2023
3c28cdf
Add JaxRs support for Request-scoped beans.
dalexandrov Oct 31, 2023
a7f196d
Refactoring to separate module
dalexandrov Nov 13, 2023
836e6bf
Add Bean support
dalexandrov Nov 13, 2023
d8a7f18
Refactor. Reuse in TestNG.
dalexandrov Nov 14, 2023
38c50db
Add validation support. First iteration.
dalexandrov Nov 15, 2023
f6f00f5
extract validation
dalexandrov Nov 17, 2023
e319e70
Move validation back to common
dalexandrov Nov 20, 2023
c903648
Naming cleanup
dalexandrov Nov 20, 2023
c51dbc2
Should be only on class
dalexandrov Nov 20, 2023
ea9af2a
Minor clean up
dalexandrov Nov 21, 2023
7eed160
Dependency fix
dalexandrov Nov 21, 2023
bc91012
Update microprofile/testing/common/src/main/java/io/helidon/microprof…
dalexandrov Nov 29, 2023
5704bae
Update microprofile/tests/testing/testng/src/test/java/io/helidon/mic…
dalexandrov Nov 29, 2023
efd0ebc
Update microprofile/testing/testng/pom.xml
dalexandrov Nov 29, 2023
fef4450
Update microprofile/testing/testng/src/main/java/io/helidon/microprof…
dalexandrov Nov 29, 2023
19074bf
Update microprofile/testing/testng/src/main/java/module-info.java
dalexandrov Nov 29, 2023
808fff8
Update microprofile/testing/testng/src/main/java/io/helidon/microprof…
dalexandrov Nov 29, 2023
20b25be
Update microprofile/testing/testng/src/main/java/io/helidon/microprof…
dalexandrov Nov 29, 2023
e62a48c
Move validator to modules
dalexandrov Nov 29, 2023
480757e
Rollback to simple design
dalexandrov Nov 30, 2023
22132ca
Rollback to simple design
dalexandrov Nov 30, 2023
67362f9
Mino clean up
dalexandrov Nov 30, 2023
8484dc3
Clean dependency tree
dalexandrov Dec 1, 2023
a1d4cc7
Copyright fix
dalexandrov Dec 1, 2023
c82f046
Apply comments
dalexandrov Dec 5, 2023
b56a6fb
Merge branch 'main' into 7564_add_JaxRs-annotation
dalexandrov Dec 5, 2023
722c95f
Fix copyright
dalexandrov Dec 5, 2023
d3ccf55
Fix dependencies
dalexandrov Dec 6, 2023
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
70 changes: 62 additions & 8 deletions docs/mp/testing-ng.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -110,32 +110,86 @@ In addition to this simplification, the following annotations are supported:

|`@io.helidon.microprofile.testing.testng.DisableDiscovery`
|Used to disable automated discovery of beans and extensions

|Used `@io.helidon.microprofile.testing.junit5.AddJaxRs`
a|add JaxRs support to the test class. Only used with `@DisableDiscovery` annotation, otherwise an exception will be thrown. Automatically adds the following Beans and Extensions to the test class:

* `ServerCdiExtension`
* `JaxRsCdiExtension`
* `CdiComponentProvider`
* `org.glassfish.jersey.ext.cdi1x.internal.ProcessAllAnnotatedTypes`
* `org.glassfish.jersey.weld.se.WeldRequestScope`
|===

== Examples

In current example Helidon container will be launched prior test. The _Bean Discovery_ will be disabled. _MyBean_ will be added to the test, so that it can be injected. _ConfigCdiExtension_ will be enabled for this test. And finally, a configuration property will be added using `@AddConfig` annotation.
In the current example, Helidon container will be launched prior test. The _Bean Discovery_ will be disabled. _MyBean_ will be added to the test, so that it can be injected. _ConfigCdiExtension_ will be enabled for this test. And finally, a configuration property will be added using `@AddConfig` annotation.

[source,java]
.Code sample
----
@HelidonTest
@DisableDiscovery
@AddBean(MyBean.class)
@AddExtension(ConfigCdiExtension.class)
@AddConfig(key = "app.greeting", value = "TestHello")
@HelidonTest <1>
@DisableDiscovery <2>
@AddBean(MyBean.class) <3>
@AddExtension(ConfigCdiExtension.class) <4>
@AddConfig(key = "app.greeting", value = "TestHello") <5>
class TestExample {
@Inject
private MyBean myBean;
private MyBean myBean; <6>

@Test
void testGreeting() {
void testGreeting() { <7>
assertThat(myBean, notNullValue());
assertThat(myBean.greeting(), is("TestHello"));
}
}
----
<1> Start the Helidon container.
<2> Set disabled Bean Discovery for the current test class.
<3> Add `MyBean` to current context.
<4> Add a configuration CDI extension to the current test.
<5> Add configuration properties.
<6> Inject `MyBean` as it is available in the CDI context.
<7> Run rests.

To test `@RequestScoped` bean with JaxRs support:

[source,java]
.Test `RequestScoped` bean
----

@HelidonTest <1>
@DisableDiscovery <2>

@AddJaxRs <3>
@AddBean(TestReqScopeDisabledDiscovery.MyController.class)
public class TestReqScopeDisabledDiscovery {

@Inject
private WebTarget target;

@Test
void testGet() {
assertEquals("Hallo!", target
.path("/greeting")
.request()
.get(String.class));
}

@Path("/greeting")
@RequestScoped <4>
public static class MyController {
@GET
public Response get() {
return Response.ok("Hallo!").build();
}
}
}
----
<1> Start the Helidon container.
<2> Set disabled Bean discovery.
<3> Add JaxRs support to the current test class.
<4> Define a `RequestScoped` bean.

== Reference

Expand Down
73 changes: 65 additions & 8 deletions docs/mp/testing.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -115,32 +115,89 @@ In addition to this simplification, the following annotations are supported:

|Used `@io.helidon.microprofile.testing.junit5.DisableDiscovery`
|to disable automated discovery of beans and extensions

|Used `@io.helidon.microprofile.testing.junit5.DisableDiscovery`
|to disable automated discovery of beans and extensions

|Used `@io.helidon.microprofile.testing.junit5.AddJaxRs`
a|add JaxRs support to the test class. Only used with `@DisableDiscovery` annotation, otherwise an exception will be thrown. Automatically adds the following Beans and Extensions to the test class:

* `ServerCdiExtension`
* `JaxRsCdiExtension`
* `CdiComponentProvider`
* `org.glassfish.jersey.ext.cdi1x.internal.ProcessAllAnnotatedTypes`
* `org.glassfish.jersey.weld.se.WeldRequestScope`
|===

== Examples

In current example Helidon container will be launched prior test. The _Bean Discovery_ will be disabled. _MyBean_ will be added to the test, so that it can be injected. _ConfigCdiExtension_ will be enabled for this test. And finally, a configuration property will be added using `@AddConfig` annotation.
In the current example, Helidon container will be launched prior test. The _Bean Discovery_ will be disabled. _MyBean_ will be added to the test, so that it can be injected. _ConfigCdiExtension_ will be enabled for this test. And finally, a configuration property will be added using `@AddConfig` annotation.

[source,java]
.Code sample
----
@HelidonTest
@DisableDiscovery
@AddBean(MyBean.class)
@AddExtension(ConfigCdiExtension.class)
@AddConfig(key = "app.greeting", value = "TestHello")
@HelidonTest <1>
@DisableDiscovery <2>
@AddBean(MyBean.class) <3>
@AddExtension(ConfigCdiExtension.class) <4>
@AddConfig(key = "app.greeting", value = "TestHello") <5>
class TestExample {
@Inject
private MyBean myBean;
private MyBean myBean; <6>

@Test
void testGreeting() {
void testGreeting() { <7>
assertThat(myBean, notNullValue());
assertThat(myBean.greeting(), is("TestHello"));
}
}
----
<1> Start the Helidon container.
<2> Set disabled Bean Discovery for the current test class.
<3> Add `MyBean` to current context.
<4> Add a configuration CDI extension to the current test.
<5> Add configuration properties.
<6> Inject `MyBean` as it is available in the CDI context.
<7> Run rests.

To test `@RequestScoped` bean with JaxRs support:

[source,java]
.Test `RequestScoped` bean
----

@HelidonTest <1>
@DisableDiscovery <2>

@AddJaxRs <3>
@AddBean(TestReqScopeDisabledDiscovery.MyController.class)
public class TestReqScopeDisabledDiscovery {

@Inject
private WebTarget target;

@Test
void testGet() {
assertEquals("Hallo!", target
.path("/greeting")
.request()
.get(String.class));
}

@Path("/greeting")
@RequestScoped <4>
public static class MyController {
@GET
public Response get() {
return Response.ok("Hallo!").build();
}
}
}
----
<1> Start the Helidon container.
<2> Set disabled Bean discovery.
<3> Add JaxRs support to the current test class.
<4> Define a `RequestScoped` bean.

== Additional Information

Expand Down
16 changes: 1 addition & 15 deletions microprofile/config/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -74,31 +74,17 @@
<artifactId>jakarta.inject-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.helidon.microprofile.cdi</groupId>
<artifactId>helidon-microprofile-cdi</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.helidon.microprofile.testing</groupId>
<artifactId>helidon-microprofile-testing-junit5</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
5 changes: 0 additions & 5 deletions microprofile/server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,6 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.helidon.microprofile.testing</groupId>
<artifactId>helidon-microprofile-testing-junit5</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.helidon.common</groupId>
<artifactId>helidon-common-reactive</artifactId>
Expand Down
11 changes: 11 additions & 0 deletions microprofile/testing/junit5/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,21 @@
</description>

<dependencies>
<dependency>
<groupId>io.helidon.microprofile.server</groupId>
dalexandrov marked this conversation as resolved.
Show resolved Hide resolved
<artifactId>helidon-microprofile-server</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>io.helidon.microprofile.cdi</groupId>
<artifactId>helidon-microprofile-cdi</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.ext.cdi</groupId>
<artifactId>jersey-weld2-se</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
Expand All @@ -57,6 +67,7 @@
<artifactId>hamcrest-core</artifactId>
<scope>test</scope>
</dependency>

</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright (c) 2023 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.helidon.microprofile.testing.junit5;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Add JaxRS support for Request-scoped beans.
dalexandrov marked this conversation as resolved.
Show resolved Hide resolved
dalexandrov marked this conversation as resolved.
Show resolved Hide resolved
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
public @interface AddJaxRs {
}
Loading