Skip to content

Commit

Permalink
TestNG support (helidon-io#3263) (helidon-io#3672)
Browse files Browse the repository at this point in the history
* TestNG Support

* Fix jsr305 version conflict.

* Fix service generation.

* Fix GroupId and formatting.
  • Loading branch information
dalexandrov authored Feb 2, 2022
1 parent 692c210 commit c9d3dc3
Show file tree
Hide file tree
Showing 34 changed files with 1,860 additions and 7 deletions.
7 changes: 6 additions & 1 deletion bom/pom.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2017, 2021 Oracle and/or its affiliates.
Copyright (c) 2017, 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.
Expand Down Expand Up @@ -1107,6 +1107,11 @@
<artifactId>helidon-microprofile-tests-junit5</artifactId>
<version>${helidon.version}</version>
</dependency>
<dependency>
<groupId>io.helidon.microprofile.tests</groupId>
<artifactId>helidon-microprofile-tests-testng</artifactId>
<version>${helidon.version}</version>
</dependency>
<!-- Logging -->
<dependency>
<groupId>io.helidon.logging</groupId>
Expand Down
6 changes: 3 additions & 3 deletions docs/mp/testing/01_testing.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
= Testing with JUnit5
:h1Prefix: MP
:pagename: testing
:description: Helidon Testing
:keywords: helidon, mp, test, testing
:feature-name: Testing
:description: Helidon Testing with JUnit5
:keywords: helidon, mp, test, testing, junit
:feature-name: Testing with JUnit
:common-deps-page-prefix-inc: ../../shared/dependencies/common_shared.adoc
Helidon provides built-in test support for CDI testing in JUnit5.
Expand Down
107 changes: 107 additions & 0 deletions docs/mp/testing/02_testing_ng.adoc
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.
4 changes: 3 additions & 1 deletion microprofile/tests/pom.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2018, 2021 Oracle and/or its affiliates.
Copyright (c) 2018, 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.
Expand Down Expand Up @@ -44,6 +44,8 @@
<module>arquillian</module>
<module>junit5</module>
<module>junit5-tests</module>
<module>testng</module>
<module>testng-tests</module>
</modules>

<profiles>
Expand Down
60 changes: 60 additions & 0 deletions microprofile/tests/testng-tests/pom.xml
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>
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"));
}
}
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;
}
}
}
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"));
}
}
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"));
}
}
Loading

0 comments on commit c9d3dc3

Please sign in to comment.