forked from helidon-io/helidon
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TestNG support (helidon-io#3263) (helidon-io#3672)
* TestNG Support * Fix jsr305 version conflict. * Fix service generation. * Fix GroupId and formatting.
- Loading branch information
1 parent
692c210
commit c9d3dc3
Showing
34 changed files
with
1,860 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/////////////////////////////////////////////////////////////////////////////// | ||
|
||
Copyright (c) 2022 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. | ||
|
||
/////////////////////////////////////////////////////////////////////////////// | ||
= Testing with Test NG | ||
:h1Prefix: MP | ||
:pagename: testing_ng | ||
:description: Helidon Testing with TestNG | ||
:keywords: helidon, mp, test, testing, testng | ||
:feature-name: Testing with TestNG | ||
:common-deps-page-prefix-inc: ../../shared/dependencies/common_shared.adoc | ||
Helidon provides built-in test support for CDI testing in TestNG. | ||
include::{common-deps-page-prefix-inc}[tag=maven-dependency] | ||
[source,xml] | ||
---- | ||
<dependency> | ||
<groupId>io.helidon.microprofile.tests</groupId> | ||
<artifactId>helidon-microprofile-tests-testng</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
---- | ||
== Usage - default | ||
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. | ||
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 | ||
[source,java] | ||
.Code sample | ||
---- | ||
@HelidonTest | ||
@DisableDiscovery | ||
@AddBean(MyBean.class) | ||
@AddExtension(ConfigCdiExtension.class) | ||
@AddConfig(key = "app.greeting", value = "TestHello") | ||
class TestNoDiscovery { | ||
@Inject | ||
private MyBean myBean; | ||
@Test | ||
void testGreeting() { | ||
assertThat(myBean, notNullValue()); | ||
assertThat(myBean.greeting(), is("TestHello")); | ||
} | ||
} | ||
---- | ||
== 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 | ||
Test method parameters are currently not supported. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
Copyright (c) 2022 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. | ||
--> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>io.helidon.microprofile.tests</groupId> | ||
<artifactId>tests-project</artifactId> | ||
<version>3.0.0-SNAPSHOT</version> | ||
</parent> | ||
|
||
<artifactId>helidon-microprofile-tests-testng-tests</artifactId> | ||
<name>Helidon Microprofile Tests TestNG unit tests</name> | ||
|
||
<description> | ||
Test for TestNG integration to prevent cyclic dependencies, | ||
so the module can be used in MP config implementation | ||
</description> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>io.helidon.microprofile.server</groupId> | ||
<artifactId>helidon-microprofile-server</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.helidon.microprofile.tests</groupId> | ||
<artifactId>helidon-microprofile-tests-testng</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.helidon.jersey</groupId> | ||
<artifactId>helidon-jersey-client</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.helidon.config</groupId> | ||
<artifactId>helidon-config-yaml-mp</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.hamcrest</groupId> | ||
<artifactId>hamcrest-core</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
</project> |
38 changes: 38 additions & 0 deletions
38
...e/tests/testng-tests/src/test/java/io/helidon/microprofile/tests/testng/AbstractTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Copyright (c) 2022 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.tests.testng; | ||
|
||
import jakarta.inject.Inject; | ||
import org.eclipse.microprofile.config.inject.ConfigProperty; | ||
import org.testng.annotations.Test; | ||
|
||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
|
||
@HelidonTest | ||
@AddConfig(key = "key", value = "value") | ||
abstract class AbstractTest { | ||
|
||
@Inject | ||
@ConfigProperty(name = "key") | ||
private String key; | ||
|
||
@Test | ||
void testKey() { | ||
assertThat(key, is("value")); | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
...le/tests/testng-tests/src/test/java/io/helidon/microprofile/tests/testng/TestAddBean.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* Copyright (c) 2022 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.tests.testng; | ||
|
||
import jakarta.inject.Inject; | ||
import org.eclipse.microprofile.config.inject.ConfigProperty; | ||
import org.testng.annotations.Test; | ||
|
||
import static io.helidon.microprofile.tests.testng.TestDefaults.DEFAULT_VALUE; | ||
import static io.helidon.microprofile.tests.testng.TestDefaults.PROPERTY_NAME; | ||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.hamcrest.CoreMatchers.notNullValue; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
|
||
@HelidonTest | ||
@AddBean(TestAddBean.MyBean.class) | ||
public class TestAddBean { | ||
|
||
@Inject | ||
private MyBean myBean; | ||
|
||
@Test | ||
void testIt() { | ||
assertThat(myBean, notNullValue()); | ||
assertThat(myBean.configured(), is(DEFAULT_VALUE)); | ||
} | ||
|
||
static class MyBean { | ||
private final String configured; | ||
|
||
@Inject | ||
MyBean(@ConfigProperty(name = PROPERTY_NAME, defaultValue = DEFAULT_VALUE) String configured) { | ||
this.configured = configured; | ||
} | ||
|
||
String configured() { | ||
return configured; | ||
} | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
...ile/tests/testng-tests/src/test/java/io/helidon/microprofile/tests/testng/TestChild1.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* Copyright (c) 2022 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.tests.testng; | ||
|
||
import jakarta.inject.Inject; | ||
import org.eclipse.microprofile.config.inject.ConfigProperty; | ||
import org.testng.annotations.Test; | ||
|
||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
|
||
@AddConfig(key = "key1", value = "value1") | ||
public class TestChild1 extends AbstractTest { | ||
|
||
@Inject | ||
@ConfigProperty(name = "key1") | ||
private String childKey; | ||
|
||
@Test | ||
void testChildKey() { | ||
assertThat(childKey, is("value1")); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
...ile/tests/testng-tests/src/test/java/io/helidon/microprofile/tests/testng/TestChild2.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Copyright (c) 2022 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.tests.testng; | ||
|
||
import jakarta.inject.Inject; | ||
import org.eclipse.microprofile.config.inject.ConfigProperty; | ||
import org.testng.annotations.Test; | ||
|
||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
|
||
@AddConfig(key = "key2", value = "value2") | ||
public class TestChild2 extends AbstractTest { | ||
@Inject | ||
@ConfigProperty(name = "key2") | ||
private String childKey; | ||
|
||
@Test | ||
void testChildKey() { | ||
assertThat(childKey, is("value2")); | ||
} | ||
} |
Oops, something went wrong.