Skip to content

Commit

Permalink
[4.x] - Add @RequestScoped support for testing (helidon-io#7916)
Browse files Browse the repository at this point in the history
* Add @RequestScoped support

Signed-off-by: Dmitry Aleksandrov <dmitry.aleksandrov@oracle.com>
  • Loading branch information
dalexandrov committed Dec 7, 2023
1 parent 2a709f5 commit df64dfa
Show file tree
Hide file tree
Showing 29 changed files with 798 additions and 48 deletions.
70 changes: 62 additions & 8 deletions docs/mp/testing-ng.adoc
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
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
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
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
Expand Up @@ -34,11 +34,21 @@
</description>

<dependencies>
<dependency>
<groupId>io.helidon.microprofile.server</groupId>
<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>
@@ -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.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
public @interface AddJaxRs {
}

0 comments on commit df64dfa

Please sign in to comment.