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

[New Doc PR] JUnit5 Testing #4300 #4505

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion docs/mp/guides/testing-junit5.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ This guide demonstrated how to create tests for MicroProfile applications in a J
Refer to the following references for additional information:

* https://junit.org/junit5/docs/current/user-guide/[JUnit 5 User Guide]
* xref:../testing/testing.adoc[Testing with JUnit 5]
* xref:../testing.adoc[Testing with JUnit 5]



106 changes: 69 additions & 37 deletions docs/mp/testing/testing-ng.adoc → docs/mp/testing-ng.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,18 @@
:description: Helidon Testing with TestNG
:keywords: helidon, mp, test, testing, testng
:feature-name: Testing with TestNG
:rootdir: {docdir}/../..
:rootdir: {docdir}/..

include::{rootdir}/includes/mp.adoc[]
== ToC
m0mus marked this conversation as resolved.
Show resolved Hide resolved

- <<Overview, Overview>>
- <<Maven Coordinates, Maven Coordinates>>
- <<Usage, Usage>>
- <<API, API>>
- <<Examples, Examples>>
- <<Reference, Reference>>

== Overview

Helidon provides built-in test support for CDI testing in TestNG.

Expand All @@ -42,20 +51,68 @@ A test can be annotated with `io.helidon.microprofile.tests.testng.HelidonTest`
CDI test. This annotation will start the CDI container before any test method is invoked, and stop it after
the last method is invoked. This annotation also enables injection into the test class itself.

== Usage

A test can be annotated with `io.helidon.microprofile.tests.testng.HelidonTest` annotation to mark it as a
CDI test. This annotation will start the CDI container before any test method is invoked, and stop it after
the last method is invoked. This annotation also enables injection into the test class itself.

=== Usage - per method CDI container
A test can be annotated as follows:

`@HelidonTest(resetPerTest = true)`

This will change the behavior as follows:

- A new CDI container is created for each test method invocation
- annotations to add config, beans and extension can be added for each method in addition to the class
- you cannot inject fields or constructor parameters of the test class itself (as a single instance is shared by more containers)

=== Usage - configuration
In addition to the `@AddConfig` annotation, you can also use
`@Configuration` to configure additional classpath properties config sources using `configSources`, and to
mark that a custom configuration is desired.
If `@Configuration(useExisting=true)`, the existing (or default) MicroProfile configuration would be used. In this case
it is important to set property `mp.initializer.allow=true` in order CDI container to start, when used with
`@HelidonTest`.
You can set up config in `@BeforeAll` method and register it with `ConfigProviderResolver` using MP Config APIs, and declare
`@Configuration(useExisting=true)`.
Note that this is not compatible with repeatable tests that use method sources that access CDI, as we must delay the CDI
startup to the test class instantiation (which is too late, as the method sources are already invoked by this time).

*If you want to use method sources that use CDI with repeatable tests, please do not use `@Configuration(useExisting=true)`*

NOTE: Test method parameters are currently not supported.

== API

The annotations described in this section are inherited (for the non-repeatable ones), and additive (for repeatable).
So if you declare `@DisableDiscovery` on abstract class, all implementations will have discovery disabled, unless you
annotate the implementation class with `@DisableDiscovery(false)`.
If you declare `@AddBean` on both abstract class and implementation class, both beans will be added.

In addition to this simplification, the following annotations are supported:

- `io.helidon.microprofile.tests.testng.AddBean` - to add one or more beans to the container
(if not part of a bean archive, or when discovery is disabled)
- `io.helidon.microprofile.tests.testng.AddExtension` - to add one or more CDI extensions to the container
(if not added through service loader, or when discovery is disabled)
- `io.helidon.microprofile.tests.testng.AddConfig` - to add one or more configuration properties to MicroProfile config
without the need of creating a `microprofile-config.properties` file
- `io.helidon.microprofile.tests.testng.DisableDiscovery` - to disable automated discovery of beans and extensions

|===
|Annotation | Usage

|`@io.helidon.microprofile.tests.testng.AddBean`
|Used to add one or more beans to the container (if not part of a bean archive, or when discovery is disabled)

|`@io.helidon.microprofile.tests.testng.AddExtension`
|Used to add one or more CDI extensions to the container (if not added through service loader, or when discovery is disabled)

|`@io.helidon.microprofile.tests.testng.AddConfig`
|Used to add one or more configuration properties to MicroProfile config without the need of creating a `microprofile-config.properties` file

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

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

[source,java]
.Code sample
Expand All @@ -65,7 +122,7 @@ In addition to this simplification, the following annotations are supported:
@AddBean(MyBean.class)
@AddExtension(ConfigCdiExtension.class)
@AddConfig(key = "app.greeting", value = "TestHello")
class TestNoDiscovery {
class TestExample {
@Inject
private MyBean myBean;

Expand All @@ -77,32 +134,7 @@ class TestNoDiscovery {
}
----

== Usage - per method CDI container
A test can be annotated as follows:

`@HelidonTest(resetPerTest = true)`

This will change the behavior as follows:

- A new CDI container is created for each test method invocation
- annotations to add config, beans and extension can be added for each method in addition to the class
- you cannot inject fields or constructor parameters of the test class itself (as a single instance is shared by more containers)


== Usage - configuration
In addition to the `@AddConfig` annotation, you can also use
`@Configuration` to configure additional classpath properties config sources using `configSources`, and to
mark that a custom configuration is desired.
If `@Configuration(useExisting=true)`, the existing (or default) MicroProfile configuration would be used. In this case
it is important to set property `mp.initializer.allow=true` in order CDI container to start, when used with
`@HelidonTest`.
You can set up config in `@BeforeAll` method and register it with `ConfigProviderResolver` using MP Config APIs, and declare
`@Configuration(useExisting=true)`.
Note that this is not compatible with repeatable tests that use method sources that access CDI, as we must delay the CDI
startup to the test class instantiation (which is too late, as the method sources are already invoked by this time).

*If you want to use method sources that use CDI with repeatable tests, please do not use `@Configuration(useExisting=true)`*

== Usage - added parameters and injection types
== Reference

Test method parameters are currently not supported.
* https://testng.org/doc/documentation-main.html[TestNG User Guide]
124 changes: 79 additions & 45 deletions docs/mp/testing/testing.adoc → docs/mp/testing.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,23 @@
///////////////////////////////////////////////////////////////////////////////

= Testing with JUnit5
:description: Helidon Testing with JUnit15
:h1Prefix: MP
:pagename: testing
:description: Helidon Testing with JUnit5
:keywords: helidon, mp, test, testing, junit
:feature-name: Testing with JUnit
:rootdir: {docdir}/../..
:rootdir: {docdir}/..

include::{rootdir}/includes/mp.adoc[]
== Contents

- <<Overview, Overview>>
- <<Maven Coordinates, Maven Coordinates>>
- <<Usage, Usage>>
- <<Examples, Examples>>
- <<Additional Information, Additional Information>>
- <<Reference, Reference>>

== Overview

Helidon provides built-in test support for CDI testing in JUnit5.

Expand All @@ -36,25 +47,77 @@ include::{rootdir}/includes/dependencies.adoc[]
</dependency>
----

== Usage - default
== Usage
A test can be annotated with `io.helidon.microprofile.tests.junit5.HelidonTest` annotation to mark it as a
dalexandrov marked this conversation as resolved.
Show resolved Hide resolved
CDI test. This annotation will start the CDI container before any test method is invoked, and stop it after
the last method is invoked. This annotation also enables injection into the test class itself.

=== Usage - per method CDI container
A test can be annotated as follows:
dalexandrov marked this conversation as resolved.
Show resolved Hide resolved

`@HelidonTest(resetPerTest = true)`

This will change the behavior as follows:

- A new CDI container is created for each test method invocation
- annotations to add config, beans and extension can be added for each method in addition to the class
- you cannot inject fields or constructor parameters of the test class itself (as a single instance is shared by more containers)
- you can add `SeContainer` as a method parameter of any test method and you will get the current container

=== Usage - configuration
In addition to the `@AddConfig` annotation, you can also use
`@Configuration` to configure additional classpath properties config sources using `configSources`, and to
dalexandrov marked this conversation as resolved.
Show resolved Hide resolved
mark that a custom configuration is desired.
If `@Configuration(useExisting=true)`, the existing (or default) MicroProfile configuration would be used. In this case
it is important to set property `mp.initializer.allow=true` in order CDI container to start, when used with
dalexandrov marked this conversation as resolved.
Show resolved Hide resolved
`@HelidonTest`.
You can set up config in `@BeforeAll` method and register it with `ConfigProviderResolver` using MP Config APIs, and declare
`@Configuration(useExisting=true)`.
Note that this is not compatible with repeatable tests that use method sources that access CDI, as we must delay the CDI
startup to the test class instantiation (which is too late, as the method sources are already invoked by this time).

*If you want to use method sources that use CDI with repeatable tests, please do not use `@Configuration(useExisting=true)`*

=== Usage - added parameters and injection types
The following types are available for injection (when a single CDI container is used per test class):

- `WebTarget` - a JAX-RS client's target configured for the current hostname and port when `helidon-micorprofile-server` is on
the classpath

The following types are available as method parameters (in any type of Helidon tests):

- `WebTarget` - a JAX-RS client's target configured for the current hostname and port when `helidon-micorprofile-server` is on
the classpath
- `SeContainer` - the current container instance

== API

The annotations described in this section are inherited (for the non-repeatable ones), and additive (for repeatable).
So if you declare `@DisableDiscovery` on abstract class, all implementations will have discovery disabled, unless you
annotate the implementation class with `@DisableDiscovery(false)`.
If you declare `@AddBean` on both abstract class and implementation class, both beans will be added.

In addition to this simplification, the following annotations are supported:

- `io.helidon.microprofile.tests.junit5.AddBean` - to add one or more beans to the container
(if not part of a bean archive, or when discovery is disabled)
- `io.helidon.microprofile.tests.junit5.AddExtension` - to add one or more CDI extensions to the container
(if not added through service loader, or when discovery is disabled)
- `io.helidon.microprofile.tests.junit5.AddConfig` - to add one or more configuration properties to MicroProfile config
without the need of creating a `microprofile-config.properties` file
- `io.helidon.microprofile.tests.junit5.DisableDiscovery` - to disable automated discovery of beans and extensions
|===
|Annotation | Usage

|`@io.helidon.microprofile.tests.junit5.AddBean`
|Used to add one or more beans to the container (if not part of a bean archive, or when discovery is disabled)

|`@io.helidon.microprofile.tests.junit5.AddExtension`
|Used to add one or more CDI extensions to the container (if not added through service loader, or when discovery is disabled)

|`@io.helidon.microprofile.tests.junit5.AddConfig`
|Used to add one or more configuration properties to MicroProfile config without the need of creating a `microprofile-config.properties` file

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

== 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.
dalexandrov marked this conversation as resolved.
Show resolved Hide resolved

[source,java]
.Code sample
Expand All @@ -64,7 +127,7 @@ In addition to this simplification, the following annotations are supported:
@AddBean(MyBean.class)
@AddExtension(ConfigCdiExtension.class)
@AddConfig(key = "app.greeting", value = "TestHello")
class TestNoDiscovery {
class TestExample {
@Inject
private MyBean myBean;

Expand All @@ -76,40 +139,11 @@ class TestNoDiscovery {
}
----

== Usage - per method CDI container
A test can be annotated as follows:

`@HelidonTest(resetPerTest = true)`
== Additional Information

This will change the behavior as follows:
* https://medium.com/helidon/testing-helidon-9df2ea14e22[Official blog article about Helidon and JUnit usage]

- A new CDI container is created for each test method invocation
- annotations to add config, beans and extension can be added for each method in addition to the class
- you cannot inject fields or constructor parameters of the test class itself (as a single instance is shared by more containers)
- you can add `SeContainer` as a method parameter of any test method and you will get the current container
== Reference

== Usage - configuration
In addition to the `@AddConfig` annotation, you can also use
`@Configuration` to configure additional classpath properties config sources using `configSources`, and to
mark that a custom configuration is desired.
If `@Configuration(useExisting=true)`, the existing (or default) MicroProfile configuration would be used. In this case
it is important to set property `mp.initializer.allow=true` in order CDI container to start, when used with
`@HelidonTest`.
You can set up config in `@BeforeAll` method and register it with `ConfigProviderResolver` using MP Config APIs, and declare
`@Configuration(useExisting=true)`.
Note that this is not compatible with repeatable tests that use method sources that access CDI, as we must delay the CDI
startup to the test class instantiation (which is too late, as the method sources are already invoked by this time).

*If you want to use method sources that use CDI with repeatable tests, please do not use `@Configuration(useExisting=true)`*

== Usage - added parameters and injection types
The following types are available for injection (when a single CDI container is used per test class):

- `WebTarget` - a JAX-RS client's target configured for the current hostname and port when `helidon-micorprofile-server` is on
the classpath

The following types are available as method parameters (in any type of Helidon tests):

- `WebTarget` - a JAX-RS client's target configured for the current hostname and port when `helidon-micorprofile-server` is on
the classpath
- `SeContainer` - the current container instance
* https://junit.org/junit5/docs/current/user-guide/[JUnit 5 User Guide]
1 change: 0 additions & 1 deletion docs/sitegen.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,6 @@ backend:
value: "save"
- type: "MENU"
title: "Testing"
dir: "testing"
glyph:
type: "icon"
value: "thumbs_up_down"
Expand Down