Skip to content

Commit df64dfa

Browse files
authored
[4.x] - Add @RequestScoped support for testing (helidon-io#7916)
* Add @RequestScoped support Signed-off-by: Dmitry Aleksandrov <dmitry.aleksandrov@oracle.com>
1 parent 2a709f5 commit df64dfa

29 files changed

Lines changed: 798 additions & 48 deletions

File tree

docs/mp/testing-ng.adoc

Lines changed: 62 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -110,32 +110,86 @@ In addition to this simplification, the following annotations are supported:
110110
111111
|`@io.helidon.microprofile.testing.testng.DisableDiscovery`
112112
|Used to disable automated discovery of beans and extensions
113+
114+
|Used `@io.helidon.microprofile.testing.junit5.AddJaxRs`
115+
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:
116+
117+
* `ServerCdiExtension`
118+
* `JaxRsCdiExtension`
119+
* `CdiComponentProvider`
120+
* `org.glassfish.jersey.ext.cdi1x.internal.ProcessAllAnnotatedTypes`
121+
* `org.glassfish.jersey.weld.se.WeldRequestScope`
113122
|===
114123
115124
== Examples
116125
117-
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.
126+
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.
118127
119128
[source,java]
120129
.Code sample
121130
----
122-
@HelidonTest
123-
@DisableDiscovery
124-
@AddBean(MyBean.class)
125-
@AddExtension(ConfigCdiExtension.class)
126-
@AddConfig(key = "app.greeting", value = "TestHello")
131+
@HelidonTest <1>
132+
@DisableDiscovery <2>
133+
@AddBean(MyBean.class) <3>
134+
@AddExtension(ConfigCdiExtension.class) <4>
135+
@AddConfig(key = "app.greeting", value = "TestHello") <5>
127136
class TestExample {
128137
@Inject
129-
private MyBean myBean;
138+
private MyBean myBean; <6>
130139
131140
@Test
132-
void testGreeting() {
141+
void testGreeting() { <7>
133142
assertThat(myBean, notNullValue());
134143
assertThat(myBean.greeting(), is("TestHello"));
135144
}
136145
}
137146
----
147+
<1> Start the Helidon container.
148+
<2> Set disabled Bean Discovery for the current test class.
149+
<3> Add `MyBean` to current context.
150+
<4> Add a configuration CDI extension to the current test.
151+
<5> Add configuration properties.
152+
<6> Inject `MyBean` as it is available in the CDI context.
153+
<7> Run rests.
154+
155+
To test `@RequestScoped` bean with JaxRs support:
156+
157+
[source,java]
158+
.Test `RequestScoped` bean
159+
----
160+
161+
@HelidonTest <1>
162+
@DisableDiscovery <2>
163+
164+
@AddJaxRs <3>
165+
@AddBean(TestReqScopeDisabledDiscovery.MyController.class)
166+
public class TestReqScopeDisabledDiscovery {
138167
168+
@Inject
169+
private WebTarget target;
170+
171+
@Test
172+
void testGet() {
173+
assertEquals("Hallo!", target
174+
.path("/greeting")
175+
.request()
176+
.get(String.class));
177+
}
178+
179+
@Path("/greeting")
180+
@RequestScoped <4>
181+
public static class MyController {
182+
@GET
183+
public Response get() {
184+
return Response.ok("Hallo!").build();
185+
}
186+
}
187+
}
188+
----
189+
<1> Start the Helidon container.
190+
<2> Set disabled Bean discovery.
191+
<3> Add JaxRs support to the current test class.
192+
<4> Define a `RequestScoped` bean.
139193
140194
== Reference
141195

docs/mp/testing.adoc

Lines changed: 65 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -115,32 +115,89 @@ In addition to this simplification, the following annotations are supported:
115115
116116
|Used `@io.helidon.microprofile.testing.junit5.DisableDiscovery`
117117
|to disable automated discovery of beans and extensions
118+
119+
|Used `@io.helidon.microprofile.testing.junit5.DisableDiscovery`
120+
|to disable automated discovery of beans and extensions
121+
122+
|Used `@io.helidon.microprofile.testing.junit5.AddJaxRs`
123+
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:
124+
125+
* `ServerCdiExtension`
126+
* `JaxRsCdiExtension`
127+
* `CdiComponentProvider`
128+
* `org.glassfish.jersey.ext.cdi1x.internal.ProcessAllAnnotatedTypes`
129+
* `org.glassfish.jersey.weld.se.WeldRequestScope`
118130
|===
119131
120132
== Examples
121133
122-
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.
134+
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.
123135
124136
[source,java]
125137
.Code sample
126138
----
127-
@HelidonTest
128-
@DisableDiscovery
129-
@AddBean(MyBean.class)
130-
@AddExtension(ConfigCdiExtension.class)
131-
@AddConfig(key = "app.greeting", value = "TestHello")
139+
@HelidonTest <1>
140+
@DisableDiscovery <2>
141+
@AddBean(MyBean.class) <3>
142+
@AddExtension(ConfigCdiExtension.class) <4>
143+
@AddConfig(key = "app.greeting", value = "TestHello") <5>
132144
class TestExample {
133145
@Inject
134-
private MyBean myBean;
146+
private MyBean myBean; <6>
135147
136148
@Test
137-
void testGreeting() {
149+
void testGreeting() { <7>
138150
assertThat(myBean, notNullValue());
139151
assertThat(myBean.greeting(), is("TestHello"));
140152
}
141153
}
142154
----
155+
<1> Start the Helidon container.
156+
<2> Set disabled Bean Discovery for the current test class.
157+
<3> Add `MyBean` to current context.
158+
<4> Add a configuration CDI extension to the current test.
159+
<5> Add configuration properties.
160+
<6> Inject `MyBean` as it is available in the CDI context.
161+
<7> Run rests.
162+
163+
To test `@RequestScoped` bean with JaxRs support:
164+
165+
[source,java]
166+
.Test `RequestScoped` bean
167+
----
168+
169+
@HelidonTest <1>
170+
@DisableDiscovery <2>
143171
172+
@AddJaxRs <3>
173+
@AddBean(TestReqScopeDisabledDiscovery.MyController.class)
174+
public class TestReqScopeDisabledDiscovery {
175+
176+
@Inject
177+
private WebTarget target;
178+
179+
@Test
180+
void testGet() {
181+
assertEquals("Hallo!", target
182+
.path("/greeting")
183+
.request()
184+
.get(String.class));
185+
}
186+
187+
@Path("/greeting")
188+
@RequestScoped <4>
189+
public static class MyController {
190+
@GET
191+
public Response get() {
192+
return Response.ok("Hallo!").build();
193+
}
194+
}
195+
}
196+
----
197+
<1> Start the Helidon container.
198+
<2> Set disabled Bean discovery.
199+
<3> Add JaxRs support to the current test class.
200+
<4> Define a `RequestScoped` bean.
144201
145202
== Additional Information
146203

microprofile/config/pom.xml

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -74,31 +74,17 @@
7474
<artifactId>jakarta.inject-api</artifactId>
7575
<scope>provided</scope>
7676
</dependency>
77-
<dependency>
78-
<groupId>io.helidon.microprofile.cdi</groupId>
79-
<artifactId>helidon-microprofile-cdi</artifactId>
80-
<scope>test</scope>
81-
</dependency>
8277
<dependency>
8378
<groupId>org.junit.jupiter</groupId>
8479
<artifactId>junit-jupiter-api</artifactId>
8580
<scope>test</scope>
8681
</dependency>
87-
<dependency>
88-
<groupId>org.mockito</groupId>
89-
<artifactId>mockito-core</artifactId>
90-
<scope>test</scope>
91-
</dependency>
82+
9283
<dependency>
9384
<groupId>org.hamcrest</groupId>
9485
<artifactId>hamcrest-all</artifactId>
9586
<scope>test</scope>
9687
</dependency>
97-
<dependency>
98-
<groupId>io.helidon.microprofile.testing</groupId>
99-
<artifactId>helidon-microprofile-testing-junit5</artifactId>
100-
<scope>test</scope>
101-
</dependency>
10288
</dependencies>
10389

10490
<build>

microprofile/server/pom.xml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -156,11 +156,6 @@
156156
</exclusion>
157157
</exclusions>
158158
</dependency>
159-
<dependency>
160-
<groupId>io.helidon.microprofile.testing</groupId>
161-
<artifactId>helidon-microprofile-testing-junit5</artifactId>
162-
<scope>test</scope>
163-
</dependency>
164159
<dependency>
165160
<groupId>io.helidon.common</groupId>
166161
<artifactId>helidon-common-reactive</artifactId>

microprofile/testing/junit5/pom.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,21 @@
3434
</description>
3535

3636
<dependencies>
37+
<dependency>
38+
<groupId>io.helidon.microprofile.server</groupId>
39+
<artifactId>helidon-microprofile-server</artifactId>
40+
<optional>true</optional>
41+
</dependency>
3742
<dependency>
3843
<groupId>io.helidon.microprofile.cdi</groupId>
3944
<artifactId>helidon-microprofile-cdi</artifactId>
4045
<scope>provided</scope>
4146
</dependency>
47+
<dependency>
48+
<groupId>org.glassfish.jersey.ext.cdi</groupId>
49+
<artifactId>jersey-weld2-se</artifactId>
50+
<optional>true</optional>
51+
</dependency>
4252
<dependency>
4353
<groupId>org.junit.jupiter</groupId>
4454
<artifactId>junit-jupiter-api</artifactId>
@@ -57,6 +67,7 @@
5767
<artifactId>hamcrest-core</artifactId>
5868
<scope>test</scope>
5969
</dependency>
70+
6071
</dependencies>
6172

6273
</project>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright (c) 2023 Oracle and/or its affiliates.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.helidon.microprofile.testing.junit5;
17+
18+
import java.lang.annotation.ElementType;
19+
import java.lang.annotation.Retention;
20+
import java.lang.annotation.RetentionPolicy;
21+
import java.lang.annotation.Target;
22+
23+
/**
24+
* Add JaxRS support for Request-scoped beans.
25+
*/
26+
@Retention(RetentionPolicy.RUNTIME)
27+
@Target({ElementType.TYPE})
28+
public @interface AddJaxRs {
29+
}

0 commit comments

Comments
 (0)